简体   繁体   中英

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.

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.

You can change interval_for_2 value:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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