I'm fairly new to arduino, and on my own I'm trying to move a servo to the left and right using 2 buttons. I've got it working for the most part, however when I hold the button down nothing happens, and when I finally release the servo moves a certain amount based on how long I was holding the button down. I would like the servo to move when the button is being held down though. The servo also occasionally moves oddly and sporadically. I'm thinking its most likely a logic error in the code, but I can't figure it out. Could someone please tell me what they think I've done wrong? Thank you.
code:
#include <Servo.h>
Servo servo;
#define right 9
#define left 8
int angle = 90;
int angStep = 5;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(right, INPUT_PULLUP);
pinMode(left, INPUT_PULLUP);
servo.attach(2);
servo.write(angle);
Serial.print("Begin");
Serial.print('\n');
Serial.print(angle);
Serial.print(angle);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(left) == HIGH && angle < 180){
angle += angStep;
Serial.print(angle);
Serial.print('\n');
servo.write(angle);
delay(50);
}
if (digitalRead(right) == HIGH && angle > 0){
angle -= angStep;
Serial.print(angle);
Serial.print('\n');
servo.write(angle);
delay(50);
}
}