【问题标题】:Arduino turn the LED on and off but not for equal timelapses without using delayArduino打开和关闭LED,但不使用延迟时间间隔相等
【发布时间】:2016-04-29 04:20:06
【问题描述】:

我正在尝试为 Arduino 编写代码,它将打开 LED 1 秒,然后将其关闭 5 秒,然后将其重新打开 1 秒,依此类推,我需要在不使用的情况下执行此操作延迟()函数。

我找到了以下代码

if( (currentMils - prevMils_for_2) > interval_for_2 )
  {
    prevMils_for_2 = currentMils;



    if(state_for_2 == LOW)
      state_for_2 = HIGH;
    else
      state_for_2 = LOW;


      digitalWrite(2, state_for_2);
  }

这使得 LED 闪烁而不使用 delay() 但我不知道如果开和关时间不同,我该如何应用这种技术。

【问题讨论】:

    标签: arduino arduino-uno


    【解决方案1】:

    您可以更改 interval_for_2 值:

    if((currentMils - prevMils_for_2) > interval_for_2 ){
        prevMils_for_2 = currentMils;
        if(state_for_2 == LOW){
          state_for_2 = HIGH;
          interval_for_2 = 1000;// duration for high
        }
        else{
          state_for_2 = LOW;
          interval_for_2 = 2000;// duration for low
        }
        digitalWrite(2, state_for_2);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多