Currently I am working on a project which consists of two servo motors and two push buttons. Here is what I am trying to do, I want the 1st push button (input 9) to stop the motors from moving when pushed and then when I push it again they will go in a clockwise motion, I want this to repeat it self continuously. The 2nd push button(input8) will act as a bumper sensor(I am building a car robot) so the bumper sensor will be at the front of robot. When pushed I want the servos to move in an anti clockwise motion and eventually be able to turn left or right. But for the time being I just want it to go anticlockwise when the button is pressed. When pressed again I want it to go in a clockwise motion again. I have been working on some code but I just cant seem to get it working correctly, I am new to this so I was hoping could someone look at the code and tell me where I am going wrong?
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo2; // create servo object to control a second servo
int button1 = 9; // Upper CW Button (UCW)
int button2 = 8; // Upper CCW Button (UCCW)
void setup()
{
myservo.attach(3); // attaches the servo on pin 9 to the servo object
myservo2.attach(11); // attaches the servo on pin 10 to the servo object
pinMode (button1, INPUT);
pinMode (button2, INPUT);
}
void loop()
{
if (digitalRead(button1) == HIGH)
myservo.write(0);
else if (digitalRead(button1) == LOW)
myservo.write(90);
if (digitalRead(button1) == HIGH)
myservo2.write(0);
else if (digitalRead(button1) == LOW)
myservo2.write(90);
if (digitalRead(button2) == HIGH)
myservo2.write(0);
else if (digitalRead(button2) == LOW)
myservo2.write(90);
if (digitalRead(button2) == HIGH)
myservo.write(0);
else if (digitalRead(button2) == LOW)
myservo.write(90);
}