Stap 10: codering
Let op: deze code is zonder de subprojecten!
/* Arduino is connected with USB to Raspberry PI Two programs are running1 Rasberry PI is the master Sending lines: - line01 : date - line02 : time - line03 : water temperature - line04 : when is the sunrise - line05 : when is the sundown - line06 : when is the time/manual/off and result of feeding of the fish 1 - line07 : when is the time/manual/off and result of feeding of the fish 2 - line08 : when is the time/manual/off and result of feeding of the fish 3 - line09 : when is the time/manual/off and resukt of feeding of the fish 4 - line10 : optional Sending times: - time01 : minutes until next feed - time02 : minutes until sunrise - time03 : minutes until sundown Receiving: - resullt of the feeding - request for sending line 01 to 10 2 Arduino is the slave Sending & Receiving to Raspberry PI Pins - LiquidCrystal (8, 9, 4, 5, 6, 7) & A0 - Softwareserial (10, 11) RX, TX - ReadingCds (A1, A2, A3, A4, A5) - Servo 1 (2) - Servo 2 (3) - Servo aan/uit (12) - Led aan/uit (13) Looping 1 Request & Read line 01 to 10 Request & Read time 01 to 03 If time 01 < 1 Display a countdown & Feed If time 02 or 03 < 1 Display a countdown Dispay current setting line01 to line10 1 per second */ /* Power consumption General power consumption Arduino: 50-60mA LCD screen Checking the USB-port, Arduino only, Arduino with LCD lsusb -v|egrep "^Bus|MaxPower" Bus 001 Device 006: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter MaxPower 96mA USB devices must tell the controller how much current they draw Using a multimeter reports 23mA Servo 1 Library: Servo, Delay 10ms, angle When detached the multimeter reports 5mA When attached the multimeter reports 150mA 1 Library: Servo, Delay 30ms, angle When detached the multimeter reports 5mA When attached the multimeter reports 125mA 2 Library: VarSpeedServo, speed 30 When detached the multimeter reports 5mA When attached the multimeter reports 150mA 3 Library: Servo, writeMicroseconds When detached the multimeter reports 5mA When attached the multimeter reports 110mA For servo please note: Keep friction low Total Servo1 Servo2 Arduino 50mA 50mA LCD 25mA 25mA Servo1 110mA 5mA Servo2 5mA 110mA LED 20mA 20mA NPN 20mA 20mA --- + --- + 230mA 230mA */ // Setting up the Keypas LCD #include LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 ); /* initialize the library with the numbers of the interface pins Arduino : LiquidCrystal lcd(12, 11, 5, 4, 3, 2); Clone : LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 ); A note about the Pin 10 bug Just one thing to mention, they all happen to have a flaw in the design, so don’t use pin 10 when using this shield. The library has functions to enable and disable the backlight. But in the design, a flaw exists, that when pin 10 is set to output, and set high, it could cause the IO pin of the MCU (the main Atmel chip) to deliver too much current (over the datasheet’s maximum rating) to the backlight LED. This may cause the over stressing of the MCU and may reduce its life, or maybe even blow it up. - To set the backlight on, set pin 10 as input. - To set the backlight off, set pin 10 as output, and immediately set output low (never high!) Clip off pin 10 */ // Setting up the serial link with Raspberry pi // #include // SoftwareSerial mySerial(10, 11); // RX, TX // * RX is digital pin 10 (connect to TX of other device) // * TX is digital pin 11 (connect to RX of other device) // Setting up the servo #include Servo servo1; // create servo1 object to control servo feeder Servo servo2; // create servo2 object to control servo hatchh int servo1_pos = 0; // variable to store the servo1 position int servo2_pos = 0; // variable to store the servo2 position // Setting up the Led & LDR int var_blink_led = 0; // Setting up the array for A1-A5 int var_A1[99]; int var_A2[99]; int var_A3[99]; int var_A4[99]; int var_A5[99]; int led_pos=0; int wacht=100; void setup() { // - Servo aan/uit (12) pinMode(12, OUTPUT); // - Led aan/uit (13) pinMode(13, OUTPUT); } void servo_on(){ digitalWrite(12, HIGH); // power on to the servo delay (1000); // wait until the capacitor is filled } void servo_off(){ digitalWrite(12, LOW); // power off to the servo delay (1000); // wait until the capacitor is empty } void blink_led(){ // just a simple test when feeding starts or stops for (var_blink_led=0; var_blink_led<5; var_blink_led +=1) { digitalWrite(13, HIGH); // Turn the led on delay (1000); digitalWrite(13, LOW); // Turn the led off delay (1000); } } void led_on(){ // turn on the led digitalWrite(13, HIGH); // Turn the led on } void led_off(){ // turn on the led digitalWrite(13, LOW); // Turn the led off } void feeding() { /* Normally a servo is attached at the setup void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } The servo receives commands through the program and works to keep the right angle In this program the servos runs for a few seconds and keeps idle until next command To keep power consumption and noise low the feeding loop has an internal attach en detach Attaching and detaching affects the power consumtion of the servo-pin The second step is using the power circuit that turns on/off the 5V Position start | 75% servo position 6:00 1100 Move to loading position / 100% servo position 7:30 700 Move to horizontal position - 25% servo position 3:00 1900 Move to empty position / 0% servo position 1:30 2300 Return to start position | 75% servo position 6:00 1100 */ for (led_pos = 1; led_pos <= 99; led_pos += 1) { // 1 to 99 clear analog array var_A1[led_pos]=0; var_A2[led_pos]=0; var_A3[led_pos]=0; var_A4[led_pos]=0; var_A5[led_pos]=0; } servo1.attach(2); // attaches the servo on pin 2 to the servo object // Move to loading postion for (servo1_pos = 1100; servo1_pos >= 710; servo1_pos -= 1) { // goes from 6:00 to 7:30 servo1.writeMicroseconds(servo1_pos); // tell servo to go to position in variable 'pos' delay(5); // waits 5ms for the servo to reach the position } // Move to horizontal position for (servo1_pos = 710; servo1_pos <= 1900; servo1_pos += 1) { // goes from 7:30 to 3:00 servo1.writeMicroseconds(servo1_pos); // tell servo to go to position in variable 'pos' delay(5); // waits 5ms for the servo to reach the position } // Move to empty position led_on(); for (servo1_pos = 1900; servo1_pos <= 2290; servo1_pos += 10) { // goes from 3:00 to 1:30 servo1.writeMicroseconds(servo1_pos); // tell servo to go to position in variable 'pos' delay(5); // ((2300-1900)/10)*5 = 200ms desceend time 100-150ms } // Measure quantity for (led_pos = 1; led_pos <= 99; led_pos +=1) { //A1 analogRead(A1); // read analog input A1 delay(10); var_A1[led_pos]=analogRead(A1); //A2 analogRead(A2); delay(10); var_A2[led_pos]=analogRead(A2); //A3 analogRead(A3); delay(10); var_A3[led_pos]=analogRead(A3); //A4 analogRead(A4); delay(10); var_A4[led_pos]=analogRead(A4); //A5 analogRead(A5); delay(10); var_A5[led_pos]=analogRead(A5); } led_off(); // Return to start position for (servo1_pos = 2290; servo1_pos >= 1100; servo1_pos -= 1) { // goes from 1:30 to 6:00 servo1.writeMicroseconds(servo1_pos); // tell servo to go to position in variable 'pos' delay(5); // waits 5ms for the servo to reach the position } servo1.detach(); // detaches the servo on pin 2 to the servo object } void openhatch() { /* See also void feeding Position start closed | 100% servo position 12:00 2300 Position end open | 0% servo position 6:00 700 */ servo2.attach(3); // attaches the servo on pin 3 to the servo object for (servo2_pos = 2290; servo2_pos >= 710; servo2_pos -= 1) { // goes from 12:00 to 6:00 servo2.writeMicroseconds(servo2_pos); // tell servo to go to position in variable 'pos' delay(5); // waits 5ms for the servo to reach the position } servo2.detach(); // detaches the servo on pin 2 to the servo object } void closehatch() { /* See also void feeding Position start closed | 100% servo position 12:00 2300 Position end open | 0% servo position 6:00 700 */ servo2.attach(3); // attaches the servo on pin 3 to the servo object for (servo2_pos = 710; servo2_pos <= 2290; servo2_pos += 1) { // goes from 6:00 to 12:00 servo2.writeMicroseconds(servo2_pos); // tell servo to go to position in variable 'pos' delay(5); // waits 5ms for the servo to reach the position } servo2.detach(); // detaches the servo on pin 2 to the servo object } void loop() { servo_on(); // power on to the servo blink_led(); openhatch(); // open the hatch blink_led(); feeding(); // turn the feeding tube blink_led(); closehatch(); // close the hatch blink_led(); servo_off(); // power off to the servo }