Go Down

Topic: How to use millis function to turn led on for 5s then turn it off indefinitely? (Read 2 times) previous topic - next topic

pluggy

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

Code: [Select]

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.
http://pluggy.is-a-geek.com/index.html

jstone

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?

Code: [Select]

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

Groove

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

Doesn't "delay()" use "millis()"?
Per Arduino ad Astra

pluggy

http://pluggy.is-a-geek.com/index.html

jstone

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.

Code: [Select]
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??

stoopkid


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

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


perhaps he want to continue running code while it waits?

AWOL

Quote
perhaps he want to continue running code while it waits?

Perhaps s/he should have stated that.
(And no, delay doesn't use "millis", it uses "micros")
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

pluggy

Code: [Select]
if (time2 >= 11000 && time2 < 6020){digitalWrite(ledEven,LOW);};  // turn it off when its been running for 6 seconds

The second proviso in the if statement  is to give a window to turn off the LED. If you knew the loop would catch every millis() you could just use a single '==' statement.  The if will never flag because The first part says that time2 has to be more than 11 seconds and the second part says it has to be less than 6.02 seconds which can't happen. 

This should work :

Code: [Select]
if (time2 >= 11000 && time2 < 11020){digitalWrite(ledEven,LOW);};  // turn it off when its been running for 11 seconds

You're going to need to start understanding what code does, rather than depending on other people to write it for you.
http://pluggy.is-a-geek.com/index.html

jstone

Sorry, i have a strange way of learning. Its not that im trying to get others to write the code for me its just i can get a better explanation for "how and why"..

I tottaly understand what your saying about the "time2" statement. It makes sence now 11020, got it.

Thanks for the code but i haven't been clear. If you look at my "if(odd)" statement does anything there immediately incorrect? As when im pressing my push button the led will not activate..

Code: [Select]
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
}

Grumpy_Mike

If you want the LEDs to stay off then don't reset the timer after turning it off, in other words don't do this

// save the last time you blinked the LED
    previousMillis = currentMillis;

jstone



Code: [Select]

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'
}




When i add a button press to this it no longer works?
What am i missing?

Code: [Select]

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

int buttonState = 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(ledPin, OUTPUT);
 digitalWrite(ledPin, LOW);
}

void loop(){
 buttonState = digitalRead(buttonPin);
 time= millis();
 
 if(buttonState == HIGH) {
  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
}

}

Go Up