I have built a slave clock driver- a device to send a 24 pulse to a slave clock (one of those old institutional clocks that we all saw in school run by a maste clock in the office). I cannot get it to keep accurate time!! (off 1 min. 22 sec. in 12 hours) I have read about the counter timeout at 9 hours and 32 minutes. I tried using the millis rollover function to compensate for this, but I'm afraid I just am not that good at this- I'm just a beginner. Here's what I have. The clock should be n for 2s and off for 58s. This seems to me to be a very simple request of the Arduino. I am really frustrated that I/it cannot accomplish it. To whoever sends me a working fix, I'll send you a free old skool clock!
/* Millis Rollover
*
- Example of counting how many times the millis() function has rolled over
- by Rob Faludi http://www.faludi.com
- for the ATMEGA168 with Arduino 0010 the max value is 34359737 or about 9 hours and 32 mintues
- for more information see: millis() - Arduino Reference
*/
#define ledPin 13 // light for status output
void setup() {
Serial.begin(9600); // start serial output
digitalWrite(ledPin, HIGH); // startup blink
delay(250); // waits
digitalWrite(ledPin, LOW); // sets the LED off
delay(250); // startup blink
digitalWrite(ledPin, HIGH); // startup blink
delay(250); // waits
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // wait
}
void loop() {
int rollovers = millisRollover(); // get the number of rollovers so far
Serial.print("Rollovers: "); // show the number of rollovers so far
Serial.println(rollovers,DEC); //
digitalWrite(ledPin, HIGH); // sets the LED on
delay(2000); // waits
digitalWrite(ledPin, LOW); // sets the LED off
delay(58000); // waits
}
int millisRollover() {
// get the current millis() value for how long the microcontroller has been running
//
// To avoid any possiblity of missing the rollover, we use a boolean toggle that gets flipped
// off any time during the first half of the total millis period and
// then on during the second half of the total millis period.
// This would work even if the function were only run once every 4.5 hours, though typically,
// the function should be called as frequently as possible to capture the actual moment of rollover.
// The rollover counter is good for over 35 years of runtime. --Rob Faludi http://rob.faludi.com
//
static int numRollovers=0; // variable that permanently holds the number of rollovers since startup
static boolean readyToRoll = false; // tracks whether we've made it halfway to rollover
unsigned long now = millis(); // the time right now
unsigned long halfwayMillis = 17179868; // this is halfway to the max millis value (17179868)
if (now > halfwayMillis) { // as long as the value is greater than halfway to the max
readyToRoll = true; // you are ready to roll over
}
if (readyToRoll == true && now < halfwayMillis) {
// if we've previously made it to halfway
// and the current millis() value is now less than the halfway mark
// then we have rolled over
numRollovers = numRollovers++; // add one to the count the number of rollovers
readyToRoll = false; // we're no longer past halfway
}
return numRollovers;
}