I am using a servo to operate a linkage sliding rail mechanism that holds a cup to pour a drink into. I am trying to have the servo move to different positions based on which button I push and if a limit switch is triggered by the cup in the loading mechanism (to avoid forgetting a cup and causing drink to spill everywhere). When I run the code, I can tell the "if" loop in question starts running, because the LED shuts off like it should and I can hear the valve activate and deactivate, but the servo shows no signs of life, even though other test code I've run gets it to move.
#include <Servo.h>
#define valve1o A3
#define valve1c A4 /* valve "o" means open, valve "c" means closed for h-bridge operation */
// H-bridge 1 ECB A5
#define button2 2
#define button3 3
#define button1 4
#define cup 5
// linkage servo 6
#define LED 7
// H-bridge 2 ECA 8
#define valve2o 9
#define valve2c 10
#define valve3o 11
#define valve3c 12
// H-bridge 2 ECB 13
void setup() {
pinMode(valve1o, OUTPUT); /* define all pins as inputs and outputs appropriately */
pinMode(valve1c, OUTPUT);
pinMode(valve2o, OUTPUT);
pinMode(valve2c, OUTPUT);
pinMode(valve3o, OUTPUT);
pinMode(valve3c, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(LED, OUTPUT);
pinMode(cup, INPUT);
int delivery = 155; /* defines servo angle outputs for postitions of linkage */
int soda1 = 82;
int soda2 = 111;
int soda3 = 132;
Servo link; /* attach servos to pins with servo library commands */
link.attach(6);
digitalWrite(LED, HIGH);
delay(250);
digitalWrite(LED, LOW);
delay(750);
link.write(delivery); /* primes the linkage to starting position */
}
void loop() {
Servo link; /* declare servo pin for void loop scope */
link.attach(6);
int valve = 2000; /* defines the time a valve stays open */
int servo = 7000; /* defines time delay after servo moves */
int delivery = 155; /* defines servo angle outputs for delivery postition of linkage */
int soda1 = 82; /* defines linkage positions under each valve */
int soda2 = 111;
int soda3 = 132;
if (digitalRead(cup) == HIGH && digitalRead(button1) == HIGH && digitalRead(button2) == HIGH && digitalRead(button3) == HIGH){ /* when no sensors are triggered, waiting for cup to be loaded */
link.write(delivery); /* sets linkage in delivery position if it isn't already*/
digitalWrite(valve1o, LOW); /* turns everything off while waiting for limit switch to be triggered */
digitalWrite(valve1c, LOW);
digitalWrite(valve2o, LOW);
digitalWrite(valve2c, LOW);
digitalWrite(valve3o, LOW);
digitalWrite(valve3c, LOW);
digitalWrite(LED, LOW);
}
if(digitalRead(cup) == LOW && digitalRead(button1) == HIGH && digitalRead(button2) == HIGH && digitalRead(button3) == HIGH){ /* when limit switch is triggered by cup and nothing else */
digitalWrite(LED, HIGH); /* watchdog LED activates, blinks until button 1 2 or 3 is pressed */
delay(250);
digitalWrite(LED, LOW);
delay(750);
}
if (digitalRead(cup) == LOW && digitalRead(button1) == LOW && digitalRead(button2) == HIGH && digitalRead(button3) == HIGH){ /* cup loaded and button 1 triggered */
digitalWrite(LED, HIGH); /* turns LED on and off to notify user of iminent activity */
delay(250);
digitalWrite(LED, LOW);
delay(750);
link.write(soda1); /* moves linkage to under valve 1 */
delay(servo); /* waits for servo to get into position */
digitalWrite(valve1o, HIGH); /* opens valve */
digitalWrite(valve1c, LOW);
delay(valve);
digitalWrite(valve1o, LOW); /* turns off valve after set amount of time */
digitalWrite(valve1c, LOW);
link.write(delivery); /* moves linkage back to delivery position */
delay(servo);
Note: the buttons and limit switch operate with "LOW" meaning they are being pressed and "HIGH" is not being pressed.
I have the servo running off the Arduino 5V output and GND (recommended by manufacturer is 4.8V to 7V). The buttons and limit switch are wired with external pull up resistors (1100 ohms each) in parallel. They also all operate properly in test code. Servo link