按下按钮后伺服延迟移动-Arduino



最近我一直在做一个项目,主要目标是移动模型火箭上的伺服装置来部署降落伞。我想做的是,在我按下按钮10秒后,降落伞就会释放。我已经有了一些代码,但它不起作用,因为它完全停止了代码。有人知道怎么解决这个问题吗?


#include <Servo.h>
// constants won't change
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int SERVO_PIN  = 9; // Arduino pin connected to servo motor's pin
Servo servo; // create servo object to control a servo
// variables will change:
int angle = 0;          // the current angle of servo motor
int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button
void setup() {
Serial.begin(9600);                // initialize serial
pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
servo.attach(SERVO_PIN);           // attaches the servo on pin 9 to the servo object
servo.write(angle);
currentButtonState = digitalRead(BUTTON_PIN);
}
void loop() {
lastButtonState    = currentButtonState;      // save the last state
currentButtonState = digitalRead(BUTTON_PIN); // read new state
if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("The button is pressed");
time.delay(10000)

// change angle of servo motor
if(angle == 0)
angle = 90;
else
if(angle == 90)
angle = 0;
// control servo motor arccoding to the angle
servo.write(angle);
}
}

在Arduino中,延迟通常使用delay(10000);(10秒(。我从未见过或听说过time.delay()函数,所以这可能就是问题所在。否则,代码看起来应该按您的意愿工作。

这也可能取决于你运行它的板。我记得在ESP8266上长时间使用delay()时遇到了问题,尽管我很确定它在几十秒内都很好。

最新更新