Hi,
I was experimenting with printing out the amount of time since the last boot. I wanted it to print it every 5 seconds. I am actually getting it printing the output 5 times every 5 seconds. At first I thought it was just running through the loop that fast, so I put in a boolean variable to stop it from printing it again until it had passed the next 5 second segment, but that didn't help. Here is the code:
bool printsec = true;
void setup() {
Serial.begin(115200);
Serial.println("Starting up.");
}
void loop() {
// Print time since last boot
if (millis() % 5000 == 0 && printsec)
{
printsec = false;
Serial.print((millis()/1000));
Serial.println(" seconds since last boot");
}
else {printsec = true;}
}
Here is the output
Starting up.
5 seconds since last boot
5 seconds since last boot
5 seconds since last boot
5 seconds since last boot
5 seconds since last boot
10 seconds since last boot
10 seconds since last boot
10 seconds since last boot
10 seconds since last boot
10 seconds since last boot
What am I doing wrong?