-
5 minutes + 27 seconds is 327000 milliseconds
-
You divide this number by
intervalwhich is (1000/100) = 10 (not 100 as the comment says) so the result is close to the max of a signed 16 bits int -
You store the result in
elapsedFramewhich is a signed 16 bits int.
So a few milliseconds later, elapsedFrame will be negative and the rest of your code will break
One solution is to use a type that can hold a much bigger value, like uint32_t (unsigned long)
Here is an example that should demonstrate the problem
int frameRate = 100;
long interval = (1000/frameRate);
long elapsedTime;
int elapsedFrames;
void setup()
{
Serial.begin(57600);
}
void loop()
{
elapsedTime = micros();
elapsedFrames = (elapsedTime / interval); // divide by 10, not 100
Serial.print( "elapsedTime = " );
Serial.print( elapsedTime );
Serial.print( " elapsedFrames = " );
Serial.println( elapsedFrames );
}
Then change int elapsedFrames; to uint32_t elapsedFrames; and see the difference