Pages: [1]   Go Down
Author Topic: Help With Simple Countdown  (Read 587 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Alright here's my problem. I have an arduino uno that I just bought and I need to have a sequence of five LED's turn on with a press of a button (first press turns on first LED, second turns on the second LED, etc). However, I also need to put in a time control where after I press the button to turn on the first LED, a timer begins to count down from one minute twenty seconds. After the time gets to one minute, I need the LED's to remain in their current state whether it be on or off, and then after an additional twenty seconds they turn off. So far, I have the LED's working, but I've spent hours trying to find some way to start a countdown with no progress whatsoever  smiley-mad . Please help.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8966
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have a 1:20 time interval.  The timer starts when you press the button.  You track the number of button presses for the first 20 seconds.  You display that number for 20 seconds, then you turn off the LED's for the remaining 40 seconds?

State 0:  All LED's are off.  No buttons are pressed.  Timer is not running.
    On button press, Start timer, increment number of lights, got to State 1

State 1: Some number of LED's are lit and timer is running.
    On button press, increment number of lights.
    On timer reaches 20 seconds (1:20->1:00):  Re-start timer. Go to State 2

State 2: Some number of LED's are lit and timer is running.
    On timer reaches 20 seconds (1:00->0:40): Turn off all LED's. Re-start timer.  Go to State 3

State 3: Lights are off.  Timer is running.
    On timer reaches 40 seconds (0:40->0:00):  Go to State 0


Normally one 'starts a timer' by taking note of the current value of millis() (number of milliseconds since the Arduino started) and then checks for millis() >= (start_time + millisecondsToWait.


Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8594
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

When the first button is pressed you save the time

unsigned long start_time = millis();

then somewhere in your existing loop that checks the buttons you check that time

if (millis() - start_time > 1000 * 60) {
    // set a flag that stops any further button-pressing action
}

if (millis() - start_time > 1000 * 80) {
    // turn all LEDs off
}

That's the gist of it. See if you can incorporate that into your existing code.

______
Rob

« Last Edit: March 15, 2011, 11:00:47 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1]   Go Up
Jump to: