i have a servo and 2 push buttons.
i would like the servo to rotate clockwise with one button and anti-clockwise with the other at increments of around 10 degres i have this following code however the servo moves randomly. any help will be much appreciated
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int buttonPin = 2;
int buttonState = 0; // variable for reading the pushbutton status
int buttonPin2 = 3;
int buttonState2 = 0; // variable for reading the pushbutton status
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
myservo.write(pos);
}
void loop()
{
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
if (buttonState == LOW) {
myservo.write(pos);
delay(15);
pos = pos + 18;
}
else {
}
if (buttonState2 == LOW) {
myservo.write(pos);
delay(15);
pos = pos - 18;
}
else {
}
}