Often when I ask a question it ends up being something obvious, but right now it is not so obvious to me. I am trying to increment a servo motor up one degree (0-180) every time a button is pushed (buttonPin). I have an LED (ledPin) attached just as an indicator that tells me that the program is seeing the button pushed. There are no errors in the program but it is not working the way I want. Instead of increment one degree per button push the servo motor moves 180 degrees and then resets itself to 0.
below is the code.
const int buttonPin = 2;
const int ledPin = 4;
int buttonState = 0;
int buttonPushCounter = 0;
int lastButtonState = 0;
#include <Servo.h>;
Servo myServo;
void setup() {
pinMode (buttonPin, INPUT);
pinMode (ledPin, OUTPUT);
myServo.attach(9);
}
void loop() {
buttonState = digitalRead (buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
digitalWrite (ledPin, HIGH);
buttonPushCounter++;}
for (buttonPushCounter>-1;buttonPushCounter<180;buttonPushCounter +=1){
myServo.write(buttonPushCounter);
delay(10);}}
else {
digitalWrite (ledPin, LOW);
myServo.write(buttonPushCounter);
delay (10);
}
lastButtonState = buttonState;
}
Thank you.