Go Down

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

Boffin1

If I get some time in the night I will run through it and see whats wrong
With my mobile phone I can call people and talk to them -  how smart can you get ?

CrossRoads

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. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Boffin1

Thats working fine Crossroads,   is the idea of checking every hundredth of a second more accurate than milllis? 
With my mobile phone I can call people and talk to them -  how smart can you get ?

CrossRoads

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. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Boffin1

Right, I think I will change some of my stock codes to include that way.
With my mobile phone I can call people and talk to them -  how smart can you get ?

mberghi

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

CrossRoads

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
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

mberghi

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

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

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

CrossRoads

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.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

mberghi

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

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?

CrossRoads

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 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
}
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

mberghi

Havnt been working on  it as the whole fam has been sick.  I will try experimenting8  with that code. Thanks CrossRoads.

Go Up