Hi! I built a sprint timer that uses millis to track the time. When you press the button to start the race, the Arduino does some subtraction to figure out the time, and when a light gate is tripped at the finish, the time and place are printed on the serial monitor. Currently, I can have the timer start and stop as many races as I want, and run for as long as I want. That all works properly. My only question is for when the timer goes over one minute, how should I print that on the serial monitor and increment that properly? I don't know if that would work with the way I store the time (current millis - millis = variable TIMER, when light gate is tripped, print TIMER/1000). Any help is much appreciated.
Yep: I have a timer set up with millis so that it tracks sprint times down to the millisecond, but if the timer goes over one minute, it obviously would print 60.XXX and on from there. I am wondering if there is a good approach to making it so that it would print something more like 1.23.456.
Update: I have been tinkering, and got it to start printing the minutes (for testing, I set a "minute" to be 10 seconds). Problem is, it only increments the minutes up by one (as it should), but it doesn't reset the seconds. I changed the variable for seconds to seconds so it is easier to work with. I have had one other problem during testing: the counter doesn't count up every millisecond (as it should), instead a seemingly random amount each time. I commented out all the functions that might be causing a delay, but it hasn't changed.
Updated code (sorry for the lack of comments, I don't usually put them in unless I need them later on)
const byte heartbeatLED = 13;
unsigned long TIME;
unsigned int MINUTES;
unsigned int SECONDS;
unsigned int milliSeconds;
unsigned long heartbeatMillis;
unsigned long startMillis;
// s e t u p ( )
//*********************************************************************
void setup()
{
Serial.begin(9600);
pinMode(heartbeatLED, OUTPUT);
startMillis = millis();
} //END of setup()
// l o o p ( )
//*********************************************************************
void loop()
{
//***********************
//time to toggle the heartbeatLED ?
if (millis() - heartbeatMillis >= 100)
{
//restart the TIMER
heartbeatMillis = millis();
//toggle the LED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
TIME = (millis() - startMillis);
MINUTES = TIME / 60000ul;
SECONDS = (TIME / 1000ul) % 60;
milliSeconds = TIME % 1000ul;
Serial.print(MINUTES);
Serial.print(":");
//print a leading zero ?
if (SECONDS < 10)
{
Serial.print("0");
}
Serial.print(SECONDS % 60);
Serial.print(".");
Serial.println(milliSeconds);
}
} //END of loop()
Just wondering, is there any reason for the delay at the end? That seems to make it rather difficult to stop the timer, and I'm not sure that is the best thing to have in a sprint timer... but otherwise this works great! Thanks so much!