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:
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
}
}
}
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.
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:
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.
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;
}
}
unsigned long elapsedTime;
...
onTime = millis();
onTime = elapsedTime;
All of this is pointless, get rid of it.
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
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.
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.
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 :
" 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?
Please post your code using the "#" button to provide the appropriate tags. Seeing your code with a smiley in it is confusing.
You say that your code works well. I am not convinced. What do you think that this does?
onTime = millis();
onTime = elapsedTime;
It may be that onTime ends up with a zero, but I don't think that it is guaranteed.
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.
Using literal numbers in your code is not a good programming practice. For example,
digitalWrite(12, HIGH);
is not good. Better would be:
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.
hi. this is not my code . is on this page ,before mine post. i just copy, in fact i remove that
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: )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;
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?
digitalWrite(LED, HIGH); // LED goes off
That's a weird way to wire LEDs. Why are you doing THAT?