4 Digit Seven Segment Display Countdown Timer - Minutes to

change these kind of things to

  if(currentdotMillis - previousdotMillis >= 500) {
    previousdotMillis = previousdotMillis+500;

thus previousdotMilils is always at the 1/2 second interval.
If prior interval ran a little long (say 520ms)
then the next will just occur a little earler, say after 480mS.

Sorry Dave

I havn't been checking this thread, I must admit I didn't check the timing when I threw that code together :slight_smile:

I didnt realise you needed accuracy, the bomb timers on James Bond always take 3 minutes to count the last 5 seconds ! :slight_smile:

I will have a look over the weekend,

John

The bomb bit just seemed to creep in (maybe because of the original thread). It is actually a timer for my FIL's train club to do efficiency comps (30 mins + set amount of coal kind of thing).

In one respect it has been a good thing as it has made me investigate the code in more detail, understanding what each piece does and what affects what, so it is a good learning experience and improving my code skills immeasurably. Far better than just chucking the code in and letting it run. XD

Dave

if(currentdotMillis - previousdotMillis >= 500) {
previousdotMillis = previousdotMillis+500;
thus previousdotMilils is always at the 1/2 second interval.
If prior interval ran a little long (say 520ms)
then the next will just occur a little earler, say after 480mS.

Thats a good idea Crossroads, I will remember to do that in future.

I will see if I can have a look at the timing Dave, just got a few fires to stamp out first.

Any luck Dave, ?

I still havnt had a chance to plug in an Arduino yet ...

Not yet John.

I have tried quite a few things now and can't seem to get it any better. I've even tried different crystals and AT chip (just in case!).

I think it might be an issue with the time it takes to do a digitalwrite so have been looking at adding a condition into the code so that it only attempts a write on digit change to see if that makes a difference (although I would lose the flashing colons - not too big a deal). So far it doesn't seem to make too much difference but I will see where I can go with it.

Dave

If I get some time in the night I will run through it and see whats wrong

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.

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

Thats working fine Crossroads, is the idea of checking every hundredth of a second more accurate than milllis?

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.

Right, I think I will change some of my stock codes to include that way.

Hi all,
I am very new to Arduino( only done the blink sketch so far :blush: ) 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

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;
when your event starts,
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

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

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.

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

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

Start with simple things, expand your program as you learn more.
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.

Thanks. I feel really dumb as I am just realizing this whole post originated about switching to the min sec format :blush:. 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 :slight_smile:
Thanks again

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?

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

if ( (current_time - previous_time) >=interval)
{ 
previous_time = previous_time + interval;  // set up for next pass thru loop
increment ones-seconds,
if that == 10, I reset to 0 and increment tens-seconds,
if that == 6, I reset to 0 and increment ones-minutes
if that == 10, I reset to 0 and increment tens-minutes
if that == 6, I reset to 0 and increment ones-hours
if that == 10, I reset to 0 and increment tens-hours
if that == 1 and ones-hours == 3, I reset both to 0.  (or 2 & 5 for 24 hr time).
and lastly, update the display
}