简体   繁体   English

Arduino开启和关闭LED,但没有延时但没有相等的延时

[英]Arduino turn the LED on and off but not for equal timelapses without using delay

I am trying to write a code for Arduino which will turn on the LED for 1 second, then keep it off for 5 seconds and then turn it back on for 1 second and so on, and I need to do this without using delay() function. 我正在尝试为Arduino写一个代码,该代码将打开LED 1秒钟,然后将其关闭5秒钟,然后再将其打开1秒钟,依此类推,我需要在不使用delay()的情况下执行此操作功能。

I found the following code 我发现以下代码

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

that makes the LED blink without using delay() but I cant figure out how can I apply this technique if on and off times are not the same. 这使得LED在不使用delay()的情况下闪烁,但是我无法弄清楚如果打开和关闭时间不同,如何应用此技术。

You can change interval_for_2 value: 您可以更改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);
}

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

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