Sorry to bother but I am at my wits end. I'm experimenting with timers, and starting very simple. The following code is supposed to print the content of a variable each time an interval of time has passed, then update that variable. But for some reason I'm getting no output on the serial monitor.
I have used the serial monitor before on other projects without issue, so I'm not sure what the problem could be.
Is there any error in my code?
unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
const long interval = 1000;
void setup() {
Serial.begin(9600);
}
void loop() {
if (currentMillis - previousMillis >= interval) {
Serial.println(previousMillis);
previousMillis = currentMillis;
}
else {
;
}
}
// previousMillis += interval;
currentMillis
variable anywhere. Its value does not magically update, when you have once assigned the value ofmillis()
to it. InsertcurrentMillis = millis()
right at the start of theloop()
function.