I am fairly new to the world of Arduino and electronics. I am trying to control a mini servo with 2 pushbuttons, one to turn it left and one for right. I want the motor arm to increment/decrement by 10 degrees according to which button is pressed. The problem I am having is that the arm swings all the way to 0 or 180, rather than going by 10s. I tried to fix this several times in the code and I'm pretty sure I wired everything correctly. Code is posted below. Any help is greatly appreciated.
#include <Servo.h> // servo library
Servo servo1;
const int buttonLPin = 2;
const int buttonRPin = 3;
int buttonLState;
int buttonRState;
const int ledPin = 13;
void setup()
{
servo1.attach(9);
pinMode(buttonLPin, INPUT);
pinMode(buttonRPin, INPUT);
pinMode(13, OUTPUT);
servo1.write(90);
}
void loop()
{
int position = servo1.read();
int buttonLState;
int buttonRState;
buttonLState = digitalRead(buttonLPin);
buttonRState = digitalRead(buttonRPin);
if((buttonLState == LOW) && (buttonRState == HIGH))
{
servo1.write(position + 10);
}
if((buttonRState == LOW) && (buttonLState == HIGH))
{
servo1.write(position - 10);
}
}