Hello everybody!
I am attempting to have three servos run in a loop only when a button is pressed (and held).
I am having some difficulty implementing this feature and would like some assistance.
When the code is run the servos simply move as if the button were not there, regardless of if its pressed or not.
I know the button works, I have tested it with the sample button code.
#include <Servo.h>
Servo servoBase;
Servo servoLowerarm;
Servo servoUp;
const int buttonPin = 2;
int buttonState = 1;
void setup(){
servoBase.attach(7); // attaches the servo on pin 9 to the servo object
servoLowerarm.attach(13);
servoUp.attach(11);
pinMode(7,OUTPUT);
pinMode(11,OUTPUT);
pinMode(13,OUTPUT);
pinMode(buttonPin,INPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
servoBase.write(180); //0 left
servoUp.write(180); //180 all up lowest servo
servoLowerarm.write(180); //180 all up higherservo
delay(2000); //rotates into grabbing position
servoBase.write(150);
delay(1000); // lowers arm into grabbing position
servoUp.write(125);
delay(900);
servoBase.write(180);
servoUp.write(120);
delay(900);
servoBase.write(180);
delay(1000);
servoUp.write(180);
delay(1000);
servoLowerarm.write(90);
delay(2000);
}
else {
digitalWrite(7,LOW);
}
}
I probably made a simple mistake and cant see it, any help would be greatly appreciated.
EDIT: Now it appears that when the button is pushed once, it runs through its cycle.