简体   繁体   English

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

[英]Delaying Movement of Servo after button press - Arduino

Recently I have been working on a project where the main goal is to move a servo on a model rocket to deploy a parachute.最近我一直在做一个项目,主要目标是移动 model 火箭上的伺服系统以部署降落伞。 I want to make is so that 10 seconds after I press the button the parachute is released.我想做的是在我按下按钮 10 秒后降落伞被释放。 I have some code for this already but It is not working, as it stops the code completely.我已经为此编写了一些代码,但它不起作用,因为它完全停止了代码。 Does anyone know how to fix this?有谁知道如何解决这一问题?


#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);
  }
}

In Arduino, a delay is usually done with delay(10000);在 Arduino 中,通常使用delay(10000); (for 10 seconds). (持续 10 秒)。 I've never seen or heard of the time.delay() function, so that might be the issue.我从未见过或听说过time.delay() function,所以这可能是问题所在。 Otherwise the code looks like it should work as you want.否则,代码看起来应该可以按您的意愿工作。

It also might depend on the board you're running it on.它还可能取决于您运行它的电路板。 I remember having issues using delay() for long periods on an ESP8266 although I'm pretty sure that it is fine for tens of seconds.我记得在 ESP8266 上长时间使用delay()有问题,尽管我很确定它可以持续数十秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM