Cape Town South Africa
Offline
Edison Member
Karma: 16
Posts: 1103
A newbie with loads of posts, and still so much to learn !
|
 |
« Reply #30 on: November 07, 2012, 02:39:27 pm » |
If I get some time in the night I will run through it and see whats wrong
|
|
|
|
|
Logged
|
We live in the era of the smart phones and stupid people.
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 242
Posts: 16504
Available for Design & Build services
|
 |
« Reply #31 on: November 07, 2012, 07:53:43 pm » |
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/javaTime 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
|
|
|
|
|
Logged
|
|
|
|
|
Cape Town South Africa
Offline
Edison Member
Karma: 16
Posts: 1103
A newbie with loads of posts, and still so much to learn !
|
 |
« Reply #32 on: November 08, 2012, 01:15:31 am » |
Thats working fine Crossroads, is the idea of checking every hundredth of a second more accurate than milllis?
|
|
|
|
|
Logged
|
We live in the era of the smart phones and stupid people.
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 242
Posts: 16504
Available for Design & Build services
|
 |
« Reply #33 on: November 08, 2012, 01:35:25 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.
|
|
|
|
|
Logged
|
|
|
|
|
Cape Town South Africa
Offline
Edison Member
Karma: 16
Posts: 1103
A newbie with loads of posts, and still so much to learn !
|
 |
« Reply #34 on: November 08, 2012, 01:48:15 am » |
Right, I think I will change some of my stock codes to include that way.
|
|
|
|
|
Logged
|
We live in the era of the smart phones and stupid people.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #35 on: January 10, 2013, 12:44:12 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/
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 242
Posts: 16504
Available for Design & Build services
|
 |
« Reply #36 on: January 10, 2013, 04:00:27 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; 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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #37 on: January 10, 2013, 05:25:39 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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #38 on: January 10, 2013, 05:41:52 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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #39 on: January 13, 2013, 01:23:56 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
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 242
Posts: 16504
Available for Design & Build services
|
 |
« Reply #40 on: January 13, 2013, 08:36:37 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #41 on: January 13, 2013, 08:49:48 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 1
|
 |
« Reply #42 on: January 15, 2013, 06:42:46 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 242
Posts: 16504
Available for Design & Build services
|
 |
« Reply #43 on: January 15, 2013, 09:28:56 pm » |
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 }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #44 on: January 16, 2013, 09:24:04 am » |
Havnt been working on it as the whole fam has been sick. I will try experimenting8 with that code. Thanks CrossRoads.
|
|
|
|
|
Logged
|
|
|
|
|
|