When I press the button, the servo keeps running..., but if I press again, it will still run one time of the loop.
Have a look the " Video "
I don't know what's going on.
Can anyone help me??
My code is in below.
Thanks!
#include <Servo.h>
int buttonPin = 9;
int servoPin = 8;
int buttonState;
Servo myservo;
int pos = 0;
void setup()
{
myservo.attach(servoPin);
pinMode(buttonPin, INPUT);
}
void loop()
{
int counter=0;
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
counter=1;
}
if(counter==1){
for (pos = 0; pos <= 180; pos += 1) {
// in steps of 1 degree
myservo.write(pos);
delay(5);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(5);
}
}
}