class Flasher {
int ledPin; // the number of the LED pin
long OnTime; // milliseconds of on-time
long OffTime; // milliseconds of off-time
int ledState; // ledState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
public:
Flasher(int pin, long on, long off) {
ledPin = pin;
pinMode(ledPin, OUTPUT);
OnTime = on;
OffTime = off;
ledState = LOW;
previousMillis = 0;
}
void Update() {
unsigned long currentMillis = millis();
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime)) {
ledState = LOW; // Turn it off
previousMillis = currentMillis;
digitalWrite(ledPin, ledState);
}
else if((ledState == LOW) && (currentMillis - previousMillis >= OffTime)) {
ledState = HIGH; // turn it on
previousMillis = currentMillis;
digitalWrite(ledPin, ledState);
}
}
};
void setup() {
}
Flasher led1(12, 100, 300); // duty cycle 25%
Flasher led2(11, 500, 500); // duty cycle 50%
Flasher led3(10, 750, 250); // duty cycle 75%
Flasher led4(9, 900, 100); // duty cycle 90%
void loop() {
led1.Update();
led2.Update();
led3.Update();
led4.Update();
}