Hello.
So I have this code, which makes a servo spin clockwise or counterclockwise, depending on the POT value. And then there is a button, which, when pressed, stops the servo. When the button is not pressed, the servo should keep spinning, but the issue is that HIGH value for the buttons somehow comes on at a constant rate of one second (1 on and 1 off) and stops the servo. So the servo goes on/off/on/off...
Can someone, please have a look and help me find the reason for the HIGH value of the button even when it's not pressed? Here is the code and wiring.
#include <Servo.h>
Servo myservo;
int potpin = A0;
int button = 7;
int potread, servospeed, buttonread;
void setup()
{
myservo.attach(9);
pinMode(button, INPUT);
Serial.begin(9600);
}
void loop()
{
potread = analogRead(potpin);
servospeed = map(potread, 0, 1023, 0, 179);
buttonread = digitalRead(button);
if (buttonread == LOW) {
myservo.write(servospeed);
delay(100);
}else {
myservo.write(96);
delay(100);
}
Serial.print("POT = ");
Serial.print(potread);
Serial.print("\t Mapped POT = ");
Serial.print(servospeed);
Serial.print("\t Pin = ");
Serial.println(buttonread);
//delay(100);
}