Ok, I was able to find an easy fix with the rollover issue using the working code I posted earlier:
unsigned long T1, T2;
unsigned long lastPressureTime, lastPrintTime;
int loopCounter;
void setup()
{
Serial.begin(115200);
T1 = 20;
T2 = 1000;
lastPressureTime = 0;
lastPrintTime = 0;
loopCounter = 0;
}
void loop()
{
if (millis() >= lastPressureTime + T1) // loop 50 times per second
{
lastPressureTime += T1;
loopCounter++;
}
if (millis() >= lastPrintTime + T2) // print every second
{
lastPrintTime += T2;
Serial.println(loopCounter);
loopCounter = 0;
}
}
The output for every second I get is this:
50
49
51
50
49
51
50
49
50
51
50
49
51
50
49
51
50
It should output a constant 50 but it doesn't. Any idea why that is?