Basic Timer Problem

Hello,

I have made a very simple timer program as there was something going wrong on one of my projects.

All it does is print to the serial "3 seconds" every 3 seconds, which it does for the first 15 seconds or so. Then it suddenly starts printing it all the time just basically every loop.

Is there something I am missing here? Any help would be appreciated, Thanks

int timer = 0;

void setup(void){
  Serial.begin(115200);
}

void loop(void){
  if((millis() - timer) >= 3000)
  {
   Serial.println("3 seconds");
   timer = millis(); 
  }
}

timer should be declared as an unsigned long

Doh' I don't know why i missed that.

Thanks!

The value that you are storing in timer is when an event occurred. The name of the variable that you are storing the value in should reflect that of the value being stored. timer does NOT represent what is being stored. lastUselessEventTime would. Or, perhaps, in your real code an even better name could be used.