Pages: [1]   Go Down
Author Topic: millis() push button LED timer demo example coding  (Read 1278 times)
0 Members and 1 Guest are viewing this topic.
Illinois, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to use millis() to replace a few one-shot delay uses in my code, but first I need to grasp this simple concept.
A couple posters keep pointing users to the Blink Without Delay sketch with Any question about millis(), but blink without delay doesn't equate (to me) to a one-shot application.

To power an LED from push button trigger for 5 seconds I'm testing the below code, but there is no delay:

Code:
unsigned long currentTime;
unsigned long loopTime;

void setup()
{
  currentTime = millis();
  loopTime = currentTime;
  pinMode(12, OUTPUT);  //Initialize pin 12 as status LED
  pinMode(8, INPUT);    // Our button pin
}

void loop()
{
  if (digitalRead(8) == LOW)   // Switch is closed to start LED timer
          {
          digitalWrite(12, HIGH);  // LED comes On  
          currentTime = millis();  
          }
  else   //Hits each pass that switch is not pressed
           {
          if (currentTime >= (loopTime + 5000)); {
          digitalWrite(12, LOW);  //  LED goes off
          loopTime = currentTime;  // Updates loopTime
           }
}
}

Suggestions appreciated
« Last Edit: March 18, 2013, 08:59:28 am by Ashton » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45998
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
To power an LED from push button trigger for 5 seconds I'm testing the below code, but there is no delay
Of course not. You need to record WHEN an event occurred, and later test whether that even was long enough ago to warrant having another event occur.

currentTime is a lousy name for when the LED was turned on.

loopTime is only set in setup, and is a meaningless name.

Try using some names that mean something, like onTime for when the LED was turned on. Ditch loopTime, which is completely meaningless.

You can determine that the LED is on because onTime will be non-zero. So, the else part is not needed.

After the if(digitalRead(8) == LOW) block, have another if statement:
Code:
if(onTime > 0 && millis() - onTime > 5000)
{
}
In that block, turn the LED off and set onTime to 0.
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In the last thread, I named the variable lastLowReading for a reason. Going back, its probably better to call it lastLowReadingTime. Having meaningful variable names helps; trust me.
Logged

Illinois, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Try using some names that mean something, like onTime for when the LED was turned on. Ditch loopTime, which is completely meaningless.
You can determine that the LED is on because onTime will be non-zero. So, the else part is not needed.
After the if(digitalRead( 8 ) == LOW) block, have another if statement:
Code:
if(onTime > 0 && millis() - onTime > 5000)
{
}

I made the suggested changes as I understood them, but its not timing.  It looks like it should be working.

Code:
unsigned long elapsedTime;
unsigned long onTime;

void setup()
{
  onTime = millis();
  onTime = elapsedTime;
  pinMode(12, OUTPUT);  //Initialize pin 12 as status LED
  pinMode(8, INPUT);    // Our button pin
}

void loop()
{
  if (digitalRead(8) == LOW)   // Switch is closed to start LED timer
         {
          digitalWrite(12, HIGH);  // LED comes On  
          onTime = millis();  
          }
      if(onTime > 0 && millis() - onTime > 5000);  
            {      
            digitalWrite(12, LOW);  // LED goes off
             onTime = 0;
            }
}
« Last Edit: March 18, 2013, 10:02:16 am by Ashton » Logged

Westbrook, CT
Offline Offline
Full Member
***
Karma: 2
Posts: 139
"Why should I bother with made-up games when there are so many real ones going on." (c) Kurt Vonnegut
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In your code the millis() function keeps on counting and doesn't reset. Is that what you want? Or you want it to reset when you press the button?
Logged

Arduino Uno R3
Mac OSX Lion


California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(onTime > 0 && millis() - onTime > 5000);  

Why is there a semicolon at the end of this if statement?
Logged

Illinois, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(onTime > 0 && millis() - onTime > 5000);

Ooops.

Removing the end semicolon solved the problem.

Quote
In your code the millis() function keeps on counting and doesn't reset. Is that what you want? Or you want it to reset when you press the button?
I wanted the (12, OUTPUT) LED to remain on for 5 seconds after the switch closure to GND.

Thanks
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
unsigned long elapsedTime;
...
  onTime = millis();
  onTime = elapsedTime;

All of this is pointless, get rid of it.

 
Code:
if (digitalRead(8) == LOW)   // Switch is closed to start LED timer
         {
          digitalWrite(12, HIGH);  // LED comes On 
          onTime = millis(); 
          }
      if(onTime > 0 && millis() - onTime > 5000); 
            {     
            digitalWrite(12, LOW);  // LED goes off
             onTime = 0;
            }

Fix your indentation

     
Code:
if(onTime > 0 && millis() - onTime > 5000); 
            {     
            digitalWrite(12, LOW);  // LED goes off
             onTime = 0;
            }

If all you are doing is turning the LED off and it is not controlled by any other means, the onTime > 0 and onTime = 0 aren't necessary.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45998
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If all you are doing is turning the LED off and it is not controlled by any other means, the onTime > 0 and onTime = 0 aren't necessary.
It just avoids turning off a pin that isn't on. I like it, to make it clear that one is aware that the pin is on, and needs to be turned off at the right time.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
unsigned long elapsedTime;
unsigned long onTime;

void setup()
{
  onTime = millis();
  onTime = elapsedTime;
  pinMode(12, OUTPUT);  //Initialize pin 12 as status LED
  pinMode(8, INPUT);    // Our button pin
}

void loop()
{
  if (digitalRead(smiley-cool == LOW)   // Switch is closed to start LED timer
         {
          digitalWrite(12, HIGH);  // LED comes On 
          onTime = millis(); 
          }
      if(onTime > 0 && millis() - onTime > 5000); 
            {     
            digitalWrite(12, LOW);  // LED goes off
             onTime = 0;
            }
}

That code ,in fact works good, but that part is not true :
Quote
"  if ...  // Switch is closed to start LED timer"
..
timer only start AFTER i release the botton,not when pressed down.  and if i want left the botton down?it never start the timer  : in my case i really need start the counter with botton down.

i try ,but i don't understand why the code do that?anyone know?

« Last Edit: March 27, 2014, 05:32:09 pm by Tmaster » Logged

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

1)  Please post your code using the "#" button to provide the appropriate tags.  Seeing your code with a smiley in it is confusing.
2)  You say that your code works well.  I am not convinced.  What do you think that this does?

Code:
onTime = millis();
onTime = elapsedTime;

It may be that onTime ends up with a zero, but I don't think that it is guaranteed.

3)  How do you have things wired?  There are many ways of wiring a pushbutton.  Some of them are right but produce different results.  Many are wrong.

4)  Using literal numbers in your code is not a good programming practice. For example,

Code:
digitalWrite(12, HIGH); 

is not good.  Better would be:

Code:
const int STATUS = 12 ;
:
:
:
digitalWrite(STATUS, HIGH); 

Then, if you ever need to change the pin number, you only have to change it in a single place.  Any "magic" numbers in your code (such as 5000) should be treated similarly.

Thanks!

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi. this is not my code . is on this page ,before mine post. i just copy, in fact i remove that
Code:
onTime = millis();
onTime = elapsedTime;
like other member sayd but i forgot to correct here.

the code work. and the other code with delay() work the same way. it only start counting after i RELEASE the button. all codes i tryed works the same way. is that i want undestand why it hapens. if you try it you will see.

my button is wired beetween ground a pin 2 with internal pullup activated  (i add this on my code aswell: )
Code:
pinMode(8, INPUT_PULLUP);    // Our button pin

About the numbers on the code, i  will correct that,. tanks
« Last Edit: March 28, 2014, 04:45:48 am by Tmaster » Logged

Pages: [1]   Go Up
Jump to: