我是 Arduino 的新手,并试图创建一个简单的应用程序,因此当按下按钮(不连续)时伺服会前进 50 度,而松开按钮时会返回 50 度。出于某种原因,我的伺服系统一直在运行。我应该怎么做才能调试这个。
#include <Servo.h>
Servo myservo; // creating myservo object
int buttonPin = 2;
int servoPin = 3;
int buttonState = 0; // set buttonState
void setup()
{
myservo.attach(servoPin);
pinMode(buttonPin, INPUT);
}
void loop()
{ buttonState = digitalRead(buttonPin); // read and save to the variable "buttonState" the actual state of button
if (buttonState == HIGH)
myservo.write(50); else
myservo.write(0);
}