So, it sounds simple, right…? Yeah, that’s what I thought.
The first step I’m trying to figure out is how to get a continuous servo to run in one direction after a button press for 10 seconds.
Then next step is getting the servo to run in the opposite direction for 10 seconds with a different button press.
And then finally, have two sets of LED’s blink on/off alternating while the motor is running for the duration.
I’ve done a couple tutorials on turning the LED on the Arduino UNO on, blinking it, using a switch to turn it on, etc.
I’ve also done tutorials on getting the servo to run at a set speed for a duration as well.
The part I’m having trouble with is combining the tutorials to get what I need from the code and figuring out exactly how to wire it up.
My first focus is just getting a pushbutton to make the servo go for X seconds and stop.
And then I’ll move on to getting a pushbutton to make it go the opposite direction.
Then add in the LED’s after that. But, right now I hit send and it just runs without needing the button press to start.
So, at the moment my code looks like this and is giving an error.
Servo servo; // create servo object to control a servo;
const int FWDbuttonPin = 3;
const int servoPin = 9;
int buttonState = 0;
int counter = 0;
servo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode (FWDbuttonPin, INPUT);
buttonState = digitalRead(FWDbuttonPin);
if (buttonState == HIGH)
if(counter == 0)
servo.write (90); // zero degrees
else if(counter == 1)
counter = 0;
I have the Arduino pins as follows.
Servo Power to 5V
Pushbutton Switch to 3.3V
Servo Ground to GND
Servo Signal to 9
Other side of switch to 3.
Thanks in advance. This is the first Arduino project I’m trying to do.