Switch On led for n time then make it off all the time using millis()

Hi All, I am trying to switch on the led for 8 seconds then I need to make it off for all the time using millis() but it doesn't work with me (Note that the led will on after 2 s from compilation) see the code bellow


const unsigned long event1=2000;
const unsigned long event2=10000;

void setup() {
  pinMode(13,OUTPUT);
 
}

void loop() {
  unsigned long currenttime=millis();
  static bool state=true;

  if ((state==true) && (currenttime >= event1)){
    digitalWrite(13,HIGH);
    state=!state;
  }
  
  if ((state==!state) && (currenttime >= event2)) {
    digitalWrite(D7,LOW);
  }
}
1 Like

Does the LED stay on for some seconds (about 8), using the above code, then shut off not to come on again?

I try your code but nothing is work.
I want to switch on the led for 8 second after 2 second from compiling the code
then after 10 s make it off

Hello
Post a timing diagram to clarify your nebulised needs.

for the first 2 second nothing will happen > then after 2 second led will ON for 8 second > then led will OFF for all the time.

int EightOn = 8000;
unsigned long pastMillis = millis();
void setup() {
  pinMode(13, OUTPUT);
digitalWrite(13, LOW);
delay(2000);
pastMillis = millis();
digitalWrite(13,HIGH);
}
void loop() {
     if ( millis()-pastMillis) >= EightOn )
{
digitalWrite(13, LOW );
}
               
}
1 Like

Hello
try this small sketch.

void setup() {
  pinMode (LED_BUILTIN,OUTPUT); 
  while (millis()<2000);
  digitalWrite (LED_BUILTIN,HIGH);
  while (millis()<(2000+8000));
  digitalWrite (LED_BUILTIN,LOW);
}
void loop() {}
1 Like

The sketch by @paulpaulson is the same as this:

void setup() {
  pinMode (LED_BUILTIN,OUTPUT); 
  delay(2000);
  digitalWrite (LED_BUILTIN,HIGH);
  delay(8000);
  digitalWrite (LED_BUILTIN,LOW);
}
void loop() {}

@mo7ammed-saleh your idea with the state and using the millis() value which starts at zero after power up is good. It only needs some work:

const unsigned long event1 = 2000UL;
const unsigned long event2 = 10000UL;

// state:
//   0 = idle, do nothing, there is even no code for that.
//   1 = led is off for 2 seconds, after startup
//   2 = led is on for 8 seconds.
int state = 0;

void setup() {
  pinMode(LED_BUILTIN,OUTPUT);
  state = 1;     // go to first state (wait two seconds with led off)
}

void loop() {
  unsigned long currentMillis = millis();

  if (state == 1 && currentMillis >= event1) {
    digitalWrite(LED_BUILTIN,HIGH);
    state = 2;   // go to next state (wait 8 seconds with led on)
  }
  else if (state == 2 && currentMillis >= event2) {
    digitalWrite(LED_BUILTIN,LOW);
    state = 0;   // go to idle state (there is not even code for it)
  }
}

The sketch in Wokwi:

A normal millis-timer can be started at any moment. It can also be started in setup().
This is with a normal millis-timer:

unsigned long previousMillis;
unsigned long interval;
bool enable;
int ledState;

void setup() 
{
  pinMode( LED_BUILTIN,OUTPUT);
  
  // start millis timer
  interval = 2000UL;
  ledState = LOW;
  enable = true;
}

void loop() 
{
  unsigned long currentMillis = millis();

  if( enable)
  {
    if( currentMillis - previousMillis >= interval)
    {
      previousMillis = currentMillis;

      if( ledState == LOW)
      {
        ledState = HIGH;   // set new led state
        interval = 8000UL; // set new interval
      }
      else
      {
        ledState = LOW;    // set new led state
        enable = false;    // stop own millis-timer
      }
      digitalWrite( LED_BUILTIN, ledState);
    }
  }
}

The sketch in Wokwi:

1 Like

I give you the Heart point for effort.

I will modify your code and try to explain. This is not tested but it's way too simple to need much debugging. Just remember Murphy's Law and that Murphy was an optimist!

const byte events = 2;  // I use lots of space to make reading easier 
byte nextEvent = 0;

const unsigned long event[ 2 ] = { 2000, 10000 };  // it becomes easy to add events
// now all events have the same name and a different number; event[0] and event[1]

const unsigned long ledOnDuration = 8000;

unsigned long currenttime;  // allocate once, use often


void setup() 
{      // braces lined up on the page are easier to check balance { to }

  pinMode( 13, OUTPUT ); // default LOW
}

void loop() 
{
  currentTime = millis();
  static bool pinState = false;

// Arduino clock is a big round clock with over 4 billion ticks before rollover.
// To make Time Calculations ALWAYS WORK THE SAME, unsigned subtraction
// of End minus Start always gives the time between.
// This arrangement watches for time from event begin and end, it's unusual.
// Note that when currentTime is event time -1 the subtract yields 4294967295ms,
// next tick the difference is zero.

// Also note that an if ( now - start == interval ) has a chance to miss the read
// and then next time may be 49.7-some days.  
// So you test >= or <= or > or < with unsigned values, it takes care to be right
// but you learn more logic and math and arranging the problem to be easiest possible.

  if ( currentTime - event[  nextEvent ] <= ledOnDuration )
  {
    if ( pinState == false ) 
    {
      pinState = true;
      digitalWrite( 13, HIGH );
    }
  } 
  else if ( pinState == true )
  {
    pinState = false;
    digitalWrite( 13, LOW );
    if ( ++nextEvent > events )    nextEvent = 0;  // will run again in 49 days
  }

// it is a little clunky but see how easy to add events?
}

It would code cleaner as a series of OFF and ON periods. Then on even-number events the state to write is OFF, ON for odd number events, no longer need a pinState variable or a constant duration but do need twice the events.

You are writing without delays. The true power of Arduino will open to you, you can write code as independent tasks that run together and share data through variables.
The principles are simple, learn them with simple examples that you modify to test your understanding of going from general principle to specific application.

1 Like

thanks sr , worked

Thanks for support Sr :heart_eyes:

As a learner, you are welcome.

Have you debounced a button yet? A button can be as simple as a jumper you ground to "press". But Arduino is so fast it can read the button bounce 12 times in 2 milliseconds so it gets discussed, done and redone here in 20 ways or so. There's good conversations in the archives. It's like a rite of passage to write or choose a debounce.