Go Down

Topic: millis() push button LED timer demo example coding (Read 2889 times) previous topic - next topic

Ashton

Mar 18, 2013, 02:49 pm Last Edit: Mar 18, 2013, 02:59 pm by Ashton Reason: 1
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: [Select]
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

PaulS

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: [Select]
if(onTime > 0 && millis() - onTime > 5000)
{
}

In that block, turn the LED off and set onTime to 0.

Arrch

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.

Ashton

#3
Mar 18, 2013, 03:56 pm Last Edit: Mar 18, 2013, 04:02 pm by Ashton Reason: 1
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: [Select]
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: [Select]
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;
           }
}

mixania

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?
Arduino Uno R3
Mac OSX Lion

Arrch

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

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

Ashton

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

Arrch

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


All of this is pointless, get rid of it.

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

PaulS

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.

Tmaster

#9
Mar 27, 2014, 11:29 pm Last Edit: Mar 27, 2014, 11:32 pm by Tmaster Reason: 1
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(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;
            }
}


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?


vaj4088

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

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

digitalWrite(12, HIGH); 


is not good.  Better would be:

Code: [Select]

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!


Tmaster

#11
Mar 28, 2014, 10:43 am Last Edit: Mar 28, 2014, 10:45 am by Tmaster Reason: 1
hi. this is not my code . is on this page ,before mine post. i just copy, in fact i remove that
Code: [Select]
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: [Select]
pinMode(8, INPUT_PULLUP);    // Our button pin

About the numbers on the code, i  will correct that,. tanks

I know this concerns an old post but I have a question.

I ran the code and it works somewhat. When I boot it, the LED lights and stays lighted. I push the button and it stays lighted for 5 more seconds, then turns off.

I push the button again when the led is now off and it lights up for 5 seconds and then turns off.

I push the button again and the same thing happens.

It only appears different when I initially boot it up.

Any ideas.


unsigned long elapsedTime;
unsigned long onTime;
const int Enter_Button =42;
const int LED=47;

void setup()
{
  onTime = millis();
  onTime = elapsedTime;
  pinMode(LED, OUTPUT);  //Initialize pin 47 as status LED
  pinMode(Enter_Button, INPUT);    // PIN 42 Enter_Button
 
}

void loop()
{
   
  if (digitalRead(Enter_Button) == LOW)   // ON- STARTS TIMER
       {
         digitalWrite(LED, LOW);  // LED comes On 
         onTime = millis(); 
       }
  //if(onTime > 0 && millis() - onTime > 5000) //5000=5 SEC, 10000=6 SEC,15000=16 SEC 
  if(millis() - onTime > 5000) //5000=5 SEC, 10000=6 SEC,15000=16 SEC 
       {     
         digitalWrite(LED, HIGH);  // LED goes off
         //onTime = 0;
       
       }
}



PaulS

Code: [Select]
  onTime = millis();
  onTime = elapsedTime;

Which value do you want to assign to onTime? Delete the other line.

Code: [Select]
  pinMode(Enter_Button, INPUT);    // PIN 42 Enter_Button
So, you have an external resistor wired with the switch? Why? Why not use the internal pullup resistors? Makes wiring the switch simple. How IS your switch wired?

Code: [Select]
         digitalWrite(LED, HIGH);  // LED goes off
That's a weird way to wire LEDs. Why are you doing THAT?

Go Up