Struggling with OOP, I’m adapting some tutorial code from here to extend the capabilities of a simple LED class.
However it doesnt generate the correct delay when I try to use millis() instead of a delay.
(cribbed from blink without delay)
this is the bit that causes issues - I’ve attached the sketch & led.h, led.cpp files for completeness
here is the bit of the sketch that calls the flash(t) function
void loop() {
led1.flash(100);
led2.off();
delay(5000);
}
void Led::flash(int t) { //flash LED for time t
unsigned long previousMillis; // will store last time LED was updated
// for (int i=2; i>0 ;i--){change(); delay(t);} ***with delay the timing is correct
for (int i=2; i>0 ;i--){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= t){ previousMillis = currentMillis; change(); }
}
}
I’m sure its a trivial mistake but I just cant see it. The millis code gives delay times of about 3 sec.
led.cpp (1.18 KB)
led.h (293 Bytes)
OOP_example.ino (411 Bytes)