Pages: 1 [2]   Go Down
Author Topic: How to use millis function to turn led on for 5s then turn it off indefinitely?  (Read 2599 times)
0 Members and 1 Guest are viewing this topic.
Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
« Last Edit: February 27, 2011, 12:41:51 pm by pluggy » Logged


Offline Offline
Jr. Member
**
Karma: 1
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I dont want to use the delay function i want to use the Millis function?
Doesn't "delay()" use "millis()"?
Logged

Per Arduino ad Astra

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Remove all the other references to ledEven. 
Logged


Offline Offline
Jr. Member
**
Karma: 1
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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??
« Last Edit: February 28, 2011, 05:04:20 am by swifftyb » Logged

California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26243
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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")
Logged

"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.

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
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.
Logged


Offline Offline
Jr. Member
**
Karma: 1
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
 }
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 619
Posts: 33989
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



Code:
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:
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
}

}
« Last Edit: March 02, 2011, 01:29:48 pm by swifftyb » Logged

Pages: 1 [2]   Go Up
Jump to: