Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Timer LED闪烁前5秒,按下按钮(Arduino)后保持稳定5秒_Timer_Arduino_Led - Fatal编程技术网

Timer LED闪烁前5秒,按下按钮(Arduino)后保持稳定5秒

Timer LED闪烁前5秒,按下按钮(Arduino)后保持稳定5秒,timer,arduino,led,Timer,Arduino,Led,所以这可能没那么难,但我有点被难倒了。我有一个按钮,它是一个较大的控制面板中的“拍照”按钮。现在,LED亮起10秒钟,然后熄灭。我希望它在前5秒闪烁,然后在最后5秒保持稳定。我不知道该怎么做,我试过猜测,但没有成功。到目前为止,我得到的是,它目前很长很难看: // take a picture button const int shoot_pin = 5; const int shoot_led = 13; int shoot_state = 0; int last_shoot_stat

所以这可能没那么难,但我有点被难倒了。我有一个按钮,它是一个较大的控制面板中的“拍照”按钮。现在,LED亮起10秒钟,然后熄灭。我希望它在前5秒闪烁,然后在最后5秒保持稳定。我不知道该怎么做,我试过猜测,但没有成功。到目前为止,我得到的是,它目前很长很难看:

// take a picture button


const int shoot_pin = 5;
const int shoot_led = 13; 

int shoot_state = 0;
int last_shoot_state = 0;


long shoot_timer;
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 10;    // the debounce time; increase if the output flickers


void setup(){
   Serial.begin(9600);
   pinMode(shoot_pin, INPUT);
   pinMode(shoot_led, OUTPUT);

}

void loop() {
  int shoot_reading = digitalRead(shoot_pin);

  if (shoot_reading != last_shoot_state) { lastDebounceTime = millis(); } 

  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (shoot_reading != shoot_state) {
      shoot_state = shoot_reading;

      if (shoot_state == HIGH) {
          digitalWrite(shoot_led, HIGH);
          shoot_timer = millis();
          Serial.println("Counting down...5 seconds"); // for tracking
          delay(5000);
          Serial.println("Shooting Picture"); // for tracking - eventually will be a keypress

      } // end of high

    } // end of reading

  }// end of that giant nested debaounce

  last_shoot_state = shoot_reading;

  // right now just stays lit for 10 seconds
  if (millis() - shoot_timer >= 10000){ digitalWrite(shoot_led, LOW);}

} // end of loop

将延迟替换为调用为您闪烁的自定义函数(在本例中为闪烁),然后将接通时间从10秒减少到5秒

// take a picture button


const int shoot_pin = 5;
const int shoot_led = 13; 

int shoot_state = 0;
int last_shoot_state = 0;


long shoot_timer;
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 10;    // the debounce time; increase if the output flickers


void setup(){
   Serial.begin(9600);
   pinMode(shoot_pin, INPUT);
   pinMode(shoot_led, OUTPUT);

}

void loop() {
  int shoot_reading = digitalRead(shoot_pin);

  if (shoot_reading != last_shoot_state) { lastDebounceTime = millis(); } 

  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (shoot_reading != shoot_state) {
      shoot_state = shoot_reading;

      if (shoot_state == HIGH) {
          digitalWrite(shoot_led, HIGH);
          shoot_timer = millis();
          Serial.println("Counting down...5 seconds"); // for tracking
          delay(5000);
          blinkLED(13, 500, 500, 5);
          Serial.println("Shooting Picture"); // for tracking - eventually will be a keypress

      } // end of high

    } // end of reading

  }// end of that giant nested debounce

  last_shoot_state = shoot_reading;

  // turn on LED for 5 seconds
  digitalWrite(shoot_led, LOW);
  delay(5000);
  digitalWrite(shoot_led, HIGH);

} // end of loop

void blinkLED(int pinNumber, int onLength, int offLength, int repetitions){
    for(int i=0; i < repetitions; i++){    
        digitalWrite(pinNumber, LOW); //on
        delay(onLength);
        digitalWrite(pinNumber, HIGH); //off
        delay(offLength);
    }
}
//拍照按钮
常量int shoot_pin=5;
常数int SHOT_led=13;
int_state=0;
int last_shot_state=0;
长拍定时器;
long lastDebounceTime=0;//上次切换输出引脚的时间
长去弹性=10;//去盎司时间;如果输出闪烁,则增加
无效设置(){
Serial.begin(9600);
pinMode(shoot_pin,输入);
pinMode(发射发光二极管,输出);
}
void循环(){
int shot_reading=数字读取(shot_pin);
如果(shot_reading!=上次shot_state){lastDebounceTime=millis();}
如果((毫秒()-lastDebounceTime)>debounceDelay){
如果(射击读数!=射击状态){
拍摄状态=拍摄读数;
如果(发射状态==高){
数码写入(发光二极管,高电平);
射击计时器=毫秒();
Serial.println(“倒计时…5秒”);//用于跟踪
延迟(5000);
闪烁(1350500,5);
Serial.println(“拍摄图片”);//用于跟踪-最终将是一个按键
}//高潮结束
}//阅读结束
}//那个巨大的嵌套去盎司结束了
最后一次拍摄状态=拍摄读数;
//打开LED灯5秒钟
数码写入(发光二极管,低电平);
延迟(5000);
数码写入(发光二极管,高电平);
}//循环结束
虚空闪烁(整数编号、整数长度、整数长度、整数重复){
对于(int i=0;i<重复;i++){
digitalWrite(pinNumber,低);//打开
延迟(仅限长度);
数字写入(pinNumber,高);//关闭
延迟(offLength);
}
}

这给了我一个“变量或字段‘blinkLED’声明为无效错误。我不在我的编译器前面,让我看一下。我想我发现了自定义函数的问题并更新了代码,我没有声明以前的每个变量都是int,当你用一组语言编程时会发生什么!”杜克:那太棒了。:)我不得不将最后一个延迟移动到按钮上,并在循环开始时宣布引脚处于低位,以处理去盎司。非常感谢。没问题!很高兴为您提供帮助,如果答案对您来说是正确的,请标记为正确。当你能帮助某人时,请支付帮助费