gharryh
1
The code below should produce a number range that looks like this
50
100
...
250
but the code produces
50
101
152
203
254
305
Thr question is why
int count = 0;
int tmrNow = 0;
static unsigned long tmrPrev = 0;
void setup() {
Serial.begin(9600);
tmrPrev = millis();
}
void loop() {
if (millis() - tmrPrev >= 5000UL) {
Serial.print(millis() / 1000);
Serial.println(count);
count++;
tmrPrev = millis();
}
}
Because there is no guarantee that when you do tmrPrev = millis(); millis won't have incremented a bit.
Try:
tmrPrev += 5000UL;
Only use one copy of millis per loop execution
void loop() {
unsigned long timeNow = millis();
if (timeNow - tmrPrev >= 5000UL) {
Serial.print(timeNow / 1000);
Serial.println(count);
count++;
tmrPrev += 5000UL;
}
}
gharryh
4
Oh stupid myself, I forgot a Serial.print()
Serial.print(millis() / 1000);
Serial.print(" - ");
Serial.println(count);
Onmitting the extra print the result looks like a complete number
system
Closed
6
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.