Hi,
I’m pretty new to arduino and I’m working on a code to move a servo. I have everything running properly but, there is one problem.
In my code, I try to accomplish making a servo go all the way clockwise, counterclockwise, and in the middle with 3 buttons. Each does one of those things when I press it. Button two makes the servo go all the way clockwise, and the problem is that when I press button two for two long, it goes back in the center. I have no idea why.
Here’s my code:
//Drive a servo
#include <Servo.h> //includes servo library
#define Button1 0
#define Button2 1
#define Button3 2
Servo motor;
int value = 90;
int buttoni = 0;
int buttonii = 0;
int buttoniii = 0;
void setup()
{
motor.attach(22);
pinMode(Button1, INPUT);
pinMode(Button2, INPUT);
pinMode(Button3, INPUT);
}
void loop()
{
buttoni = digitalRead(Button1);
buttonii = digitalRead(Button2);
buttoniii = digitalRead(Button3);
motor.write(value);
if (buttoni == LOW)
{
value = 179;
}
if (buttonii == LOW)
{
value = 0;
}
if (buttoniii == LOW)
{
value = 90;
}
}
Can anyone help me?
Thanks,
-Dylan