I’m just starting the journey of arduino so please be patient with me. I have some code that was posted in the arduino forum that I tweaked a bit to try to use two push buttons to rotate a servo +90 and -90 degrees. I will be using two SPDT push on/off switches to get the servo to rotate, but as the code stands now, the servo will continue to rotate if either switch is left in the on position, and I don’t know how to go about fixing it. If I were to use momentary switches it would work, but I need to use push on/off ones.
Also, how do I get the servo to stop humming once it gets to the desired position? Its going to be inside of a helmet, so the noise would get annoying very quickly.
Thank you for your help.
//zoomkat servo button test 12-29-2011
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;
int pos = 0;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
servo1.attach(9);
digitalWrite(4, HIGH); //enable pullups to make pin high
digitalWrite(5, HIGH); //enable pullups to make pin high
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
for (pos =0; pos <= 90; pos +=1)
{
servo1.write(pos);
delay(5);
}
}
press2 = digitalRead(button2);
if (press2 == LOW)
{
for (pos =90; pos >= 0; pos-=1)
{
servo1.write(pos);
delay(5);
}
}
}