Go Down

### Topic: 4 Digit Seven Segment Display Countdown Timer - Minutes to (Read 28185 times)previous topic - next topic

#### Boffin1

#30
##### Nov 07, 2012, 08:39 pm
If I get some time in the night I will run through it and see whats wrong

#31
##### Nov 08, 2012, 01:53 am
Try this simple sketch - I have found it to track the official US time quite well running on a Duemilanove
http://www.time.gov/timezone.cgi?Eastern/d/-5/java
Time shows up on the serial monitor, set to 57600
Once you are convinced it handles time okay, is straightforward to  incorporate into your countdown sketch.

Code: [Select]
`unsigned long currentmillis = 0;unsigned long previousmillis = 0;unsigned long interval = 10000;byte ones_seconds = 0;byte prior_seconds = 0;byte tens_seconds = 0;byte ones_minutes = 0;byte tens_minutes = 0;byte tenths = 0;byte hundredths= 0;void setup(){Serial.begin(57600);}void loop(){  currentmillis = micros(); // read the time.  while (currentmillis - previousmillis >= interval) // 10 milliseconds have gone by  {    hundredths = hundredths +1;    if (hundredths == 10){      hundredths = 0;      tenths = tenths +1;    }    if (tenths == 10){      tenths = 0;      ones_seconds = ones_seconds +1;    }    if (ones_seconds == 10){      ones_seconds = 0;      tens_seconds = tens_seconds +1;    }    if (tens_seconds == 6){      tens_seconds = 0;      ones_minutes = ones_minutes +1;    }    if (ones_minutes == 10){      ones_minutes = 0;      tens_minutes = tens_minutes +1;    }    if (tens_minutes == 6){      tens_minutes = 0;    }    previousmillis = previousmillis + interval; // save the time for the next comparison  }  // counters are all updated now,if (prior_seconds != ones_seconds){  Serial.print (tens_minutes, DEC);  Serial.print (" ");  Serial.print (ones_minutes, DEC);  Serial.print (" : ");  Serial.print (tens_seconds, DEC);  Serial.print (" ");  Serial.println (ones_seconds, DEC);prior_seconds = ones_seconds;}} // end void loop`
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### Boffin1

#32
##### Nov 08, 2012, 07:15 am
Thats working fine Crossroads,   is the idea of checking every hundredth of a second more accurate than milllis?

#33
##### Nov 08, 2012, 07:35 am
Partially, and partially from these

(currentmillis - previousmillis >= interval)

previousmillis = previousmillis + interval; // save the time for the next comparison

So the time test interval is always a fixed amount away, and not drifting around based on when micros() got captured.
If the stuff to do happens to take too long, then the next time around it gets a chance to complete it sooner and get back on track.

I think CodingBadly turned me onto that back in 2010 when I started coding for time intervals.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### Boffin1

#34
##### Nov 08, 2012, 07:48 am
Right, I think I will change some of my stock codes to include that way.

#### mberghi

#35
##### Jan 10, 2013, 06:44 pm
Hi all,
I am very new to Arduino( only done the blink sketch so far  ) but I have worked a little with stamp controllers and basic language. I am looking to make a countdown timer that starts at about 4 minutes and counts down to 0 and when it hits 0 I would like it to do a contact closure. I would eventually like to be able to increase or decrease the time with a rotary encoder but I want to start with the basics for now. The application will be for a train simulator that I have built for a museum.  I want to limit the time that the museum patrons can spend as to give others a chance to use the simulator. The contact closure tells my keyboard encoder to send the reset signal to the train simulator.  Not that it is needed  but I have posted a link to my blog of the train sim build below.
Any help would be greatly appreciated.
Thanks,
Mike

http://trainsimulatorcab.blogspot.com/

#36
##### Jan 10, 2013, 10:00 pm
Do you need to display anything? Or just track the time.

Tracking the time, you just need to capture
startTime = millis();
and set a flag:
timeRunning = 1;
and then with every pass thru void loop you capture the time again
currentMillis = millis();
and see if your elapsed time has gone by
if (timeRunning == 1 && (currentMillis - startMillis) >= interval)
{
// close the contact  action
timeRunning = 0;  } // clear the flag
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### mberghi

#37
##### Jan 10, 2013, 11:25 pm
So I received my el cheapo 7 segment display from amazon today http://www.amazon.com/microtivity-7-segment-Display-Dynamic-Common/dp/B007LN7SSQ
and I have already assembled the circuit and I copied and pasted this code from http://www.hobbytronics.co.uk/arduino-countdown-timer
and it worked! So here are my questions.  How do I change it to seconds format and not counting in 100's format? Crossroads, I do need to display it on the display, I guess I don't understand how to make it close a contact. I assume I will hook my ground from my encoder to my ground on the arduino and then I hook my contact to be closed wire to whatever input I designate in the code.  I don't understand the code though. Could you dumbify it a bit for me?
Thanks,
Mike

#### mberghi

#38
##### Jan 10, 2013, 11:41 pm
Heres a little video counting down from 15 seconds. I can change the starting time in the code for now, but I would like to be able to change the start time with a rotary encoder or a button.
http://youtu.be/TWJMiK9GAGo

#### mberghi

#39
##### Jan 13, 2013, 07:23 pm
Ok we'll I bought a book because I figured rather than leech the code from others I should start to learn the code on my own.  I bought the kindle version of this book
http://www.amazon.com/Programming-Arduino-Getting-Started-Sketches/dp/0071784225

I'm on chapter 4 but am struggling a bit with grasping the concepts.
Any suggestions on good learning material/techniques for learning programing?
Thanks,
Mike

#40
##### Jan 14, 2013, 02:36 am
Write some code that does the time thing you want, use the serial monitor to see if its working, then add the LED driving part to it.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### mberghi

#41
##### Jan 14, 2013, 02:49 am
Thanks. I feel really dumb as I am just realizing this whole post originated about switching to the min sec format  .  I have been studying the code and it is finally starting to make sense.  Once I get that aspect completely figured out i will move onto the buttons or rotary encoder.   Sounds like it may be easier to have buttons add or subtract time to my countdown rather than a rotary encoder.  Although maybe I need a good challenge
Thanks again

#### sqyare

#42
##### Jan 16, 2013, 12:42 am
hey mberghi, i am also trying to do this. I'm using the mega 2560 and so far i have only gotten it to count with 5 digits instead of 4. i cant figure out how to get it to count with 60 seconds instead of 100. if i use 6 digits, the code will put 2 extra zeros there instead. i have also been trying to implement interrupts to alter the speed of the timer and far it just stops everything haha. have you had any luck figuring out how to do this?

#43
##### Jan 16, 2013, 03:28 am
I've done counters, but without using Modulo http://arduino.cc/en/Reference/Modulo to do it.
Instead, I have a variable for each digit.
Say 6 digits, HH:MM:SS
During loop, I see if a second has gone by
Code: [Select]
`if ( (current_time - previous_time) >=interval){ previous_time = previous_time + interval;  // set up for next pass thru loopincrement ones-seconds,if that == 10, I reset to 0 and increment tens-seconds,if that == 6, I reset to 0 and increment ones-minutesif that == 10, I reset to 0 and increment tens-minutesif that == 6, I reset to 0 and increment ones-hoursif that == 10, I reset to 0 and increment tens-hoursif that == 1 and ones-hours == 3, I reset both to 0.  (or 2 & 5 for 24 hr time).and lastly, update the display}`
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### mberghi

#44
##### Jan 16, 2013, 03:24 pm
Havnt been working on  it as the whole fam has been sick.  I will try experimenting8  with that code. Thanks CrossRoads.

Go Up

Please enter a valid email to subscribe