class FlashingLed {
private:
byte ledPin;
unsigned long onTime, offTime, lastMillis;
public:
FlashingLed(const byte pin, const unsigned long on, const unsigned long off) : ledPin(pin), onTime(on), offTime(off) {}
void begin() {
pinMode(ledPin, OUTPUT);
lastMillis = millis() - onTime;
}
void flashWhenNeeded() {
if (digitalRead(ledPin) == HIGH) { // Read the current state of the led. HIGH is on
if (millis() - lastMillis >= onTime) { // Pin 3 is high (LED on), check on time and turn LED off if time has expired
lastMillis += onTime; // Add the value of onTime to lastMillis ready to start the next timing interval
digitalWrite(ledPin, LOW); // Turn LED off
}
} else { // LED is off
if (millis() - lastMillis >= offTime) { // Pin 3 is low (LED off), check off time and turn LED on if time has expired
lastMillis += offTime; // Add the value of offTime to lastMillis ready to start the next timing interval
digitalWrite(ledPin, HIGH); // Turn LED on
}
}
}
};
FlashingLed leds[] = {{2, 500, 500}, {3, 1750, 250}};
void setup() {
Serial.begin(115200);
Serial.println(F(__FILE__));
for (auto &l : leds) l.begin();
Serial.println("Setup complete");
}
void loop() {
for (auto &l : leds) l.flashWhenNeeded();
}