How to Control a Servo With a Single Pushbutton
by Avengers3648 in Circuits > Arduino
542 Views, 6 Favorites, 0 Comments
How to Control a Servo With a Single Pushbutton

hi, this is my first instructable it is on how to control a single servo with a single pushbutton. I am using this circuit to animate a spiderman mask so it has moving lens. Please comment if you have any advice or uses for this circuit.
Supplies
Servo
Jumper Wires
Arduino Uno
Arduino IDE
Wiring the Servo

Brown wire from servo to ground
Red to 5v
orange/yellow to digital 2
one wire from the button should go to ground and the other to digital 3
Arduino Code

#include
const int buttonPin = 3;
int servoPin=2; Servo Servo1; i
nt buttonState = 0; int flag=0;
void setup(){
pinMode(buttonPin, INPUT_PULLUP);
Servo1.attach(servoPin); }
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
if ( flag == 0)
{ Servo1.write(0);
delay(500); flag=1; //change flag variable }
else if ( flag == 1){
Servo1.write(70);
delay(500);
flag=0; //change flag variable again } } }