How to use millis function to turn led on for 5s then turn it off indefinitely?

How to use millis function to turn led on for 5s then turn it off indefinitely?

Have a look at the blink without delay example.

Hi i have looked at that but i cant get the led to turn LOW indefinitely.

Any suggestions?

Put all your code in setup, then when when your LED goes LOW, there'll be no code to send it high.

You can set a variable in setup that you change when the led is turned off. Then in the main loop the origional variable condition will never be met again. Like setting a "flag".

Can anyone give me an example?

int ledPin = 13;
int leddone = 0;

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

void loop() {
  if (leddone == 0) {
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(5000);              
  leddone = 1;
  }
}
void loop() {
  if (leddone == 0) {
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(5000);              
  leddone = 1;
  }
}

Close...very close.

How about:

int ledPin = 13;
int leddone = 0;

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

void loop() {
  if (leddone == 0) {
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(5000);              
  leddone = 1;
  digitalWrite(ledPin, LOW);   // set the LED off
  }
}

Why is everyone making it look so hard?

const int ledPin = 13;

void setup()
 {                
    pinMode(ledPin, OUTPUT);     
    digitalWrite(ledPin, HIGH); 
    delay(5000);
    digitalWrite(ledPin, LOW); 
}

void loop() {}

(if your LED is wired to +5V, you need to swap the order of the digitalWrites.)

Why is everyone making it look so hard?

Its all just a warmup for the “…but what I really want to do…” post. :slight_smile:

Ahh... one problem..... I dont want to use the delay function i want to use the Millis function? Any ideas?

swifftyb:
Ahh... one problem..... I dont want to use the delay function i want to use the Millis function? Any ideas?

So we have circled around again to the AWOL suggestion:

Have a look at the blink without delay example.

Any ideas?

Sure, post your Millis code that isn't working so we can see what the problem might be. I'd hate to think you are just waiting for somebody to do all the work and effort for you.

sorry. here is the code. basically its a push button circuit and im trying to get the leds to switch off after 5 and 10 seconds. so far i have both the leds blinking but i am trying to get ledOdd to turn off after 5 seconds and ledEven to turn off after ten seconds.

here's the code..

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledOdd =  13;      // the number of the LED pin
const int ledEven = 50;
// variables will change:
int buttonstate = 0;   
// variable for reading the pushbutton status
int buttonPress = 0;

int buttonPresses = 0;

int ledState = LOW;   
int ledEoff = LOW;
 // ledState used to set the LED
long previousMillis = 0;
long interval = 1000;
long intervalTen = 10000;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledOdd, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   
  pinMode(ledEven, OUTPUT);
}

void loop(){
  // read the state of the pushbutton value:
  buttonstate = digitalRead(buttonPin);
buttonPresses = buttonPresses;
  if(buttonstate == HIGH) buttonPresses ++;
  boolean odd = buttonPresses % 2;
 
  if(odd){
//this will only happen if buttonPresses is odd
unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    ledState = !ledState;

    // set the LED with the ledState of the variable:
    digitalWrite(ledOdd, ledState);}
}

else{
//this will only happen if buttonPresses is even
unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    ledState = !ledState;

    // set the LED with the ledState of the variable:
    digitalWrite(ledEven, ledState);}
    
    unsigned long  currentMillis2 = millis();
    if(currentMillis2 - previousMillis > intervalTen){
      previousMillis = currentMillis2;
      
      ledEoff = ledEoff;
      
      digitalWrite(ledEven, ledEoff);}
  }
}

It would need to be done in the main loop to use millis().

const int ledPin =  13;      // the number of the LED pin

unsigned long time;      

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);  
  digitalWrite(ledPin,LOW);  
}

void loop()
{  time= millis();
   if (time >= 1000 && time < 1020){digitalWrite(ledPin,HIGH);};  // turn it on when its been running for a second 
   if (time >= 6000 && time < 6020){digitalWrite(ledPin,LOW);};  // turn it off when its been running for 6 seconds
  
// write rest of code here . as written it will repeat about every 49 days when millis() 'goes round the clock'
}

Tested code…

The 1020 and 6020 figures will need to be tweaked if the loop time exceeds 20 milliseconds.

For some reason im still not getting the result i hoped for…

I entered the millis code you just quoted but still nothing. Am i missing something obvious?

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledOdd =  13;      // the number of the LED pin
const int ledEven = 50;
// variables will change:
int buttonstate = 0;   
// variable for reading the pushbutton status
int buttonPress = 0;

int buttonPresses = 0;

int ledState = LOW;   
int ledEoff = LOW;
 // ledState used to set the LED
long previousMillis = 0;
long interval = 1000;
long intervalTen = 10000;
unsigned long time;   

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledOdd, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   
  pinMode(ledEven, OUTPUT);
}

void loop(){
  
    time= millis();
   if (time >= 1000 && time < 1020){digitalWrite(ledEven,HIGH);};  // turn it on when its been running for a second 
   if (time >= 6000 && time < 6020){digitalWrite(ledEven,LOW);};  // turn it off when its been running for 6 seconds
  
// write rest of code here . as written it will repeat about every 49 days when millis() 'goes round the clock'
  
  // read the state of the pushbutton value:
  buttonstate = digitalRead(buttonPin);
buttonPresses = buttonPresses;
  if(buttonstate == HIGH) buttonPresses ++;
  boolean odd = buttonPresses % 2;
 
  if(odd){
//this will only happen if buttonPresses is odd
unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    ledState = !ledState;

    // set the LED with the ledState of the variable:
    digitalWrite(ledOdd, ledState);}
}

else{
//this will only happen if buttonPresses is even
unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    ledState = !ledState;

    // set the LED with the ledState of the variable:
    digitalWrite(ledEven, ledState);}
    
    unsigned long  currentMillis2 = millis();
    if(currentMillis2 - previousMillis > intervalTen){
      previousMillis = currentMillis2;
      
      ledEoff = ledEoff;
      
      digitalWrite(ledEven, ledEoff);}
  }
}

I dont want to use the delay function i want to use the Millis function?

Doesn't "delay()" use "millis()"?

Remove all the other references to ledEven.

Hmmm still having trouble.
I dont want to use the delay function as it stopps running the other code.
I have moved all the code about and tried this. I have used some of your code again Pluggy.

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledOdd =  13;      // the number of the LED pin
const int ledEven = 50;

// variables will change:
int buttonstate = 0;   
// variable for reading the pushbutton status
int buttonPress = 0;
int buttonPresses = 0;
int ledState = LOW;

unsigned long time;
unsigned long time2;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledOdd, OUTPUT);
  pinMode(ledEven, OUTPUT);
  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   

}

void loop()
{   
  // read the state of the pushbutton value:  
  buttonstate = digitalRead(buttonPin);
  if(buttonstate == HIGH) buttonPresses ++;
  boolean odd = buttonPresses % 2;
  
  if(odd)
  {
    time= millis();
    if (time >= 1000 && time < 1020){digitalWrite(ledOdd,HIGH);};  // turn it on when its been running for a second 
    if (time >= 6000 && time < 6020){digitalWrite(ledOdd,LOW);};  // turn it off when its been running for 6 seconds
 }
 
 if(!odd)
 {
 time2= millis();
    if (time2 >= 1000 && time2 < 1020){digitalWrite(ledEven,HIGH);};  // turn it on when its been running for a second 
    if (time2 >= 11000 && time2 < 6020){digitalWrite(ledEven,LOW);};  // turn it off when its been running for 6 seconds
 }
}

Can anyone see why its not working??