#include <Arduino.h>
class blinkingLed {
public:
unsigned long interval;//every interval ms the LED changes state
//constructor. You have to tell it what pin the LED is connected to and what the interval should be.
blinkingLed(int pin, unsigned long interval):
pin{pin}, interval{interval}, lastBlink{0}//this is called "member initializer list"
{}//the body of the constructor is left empty. The member initializer list does all the work
//call begin for each LED
begin() {
pinMode(pin, OUTPUT);
}
//call run as often as possible to blink the LED (if the interval has passed). If the interval hasn't passed yet, nothing happens.
run() {
unsigned long now = millis();//this is basicall 1:1 taken from the "Arudino blink without delay" example
if (now - lastBlink >= interval) {
lastBlink = now;
state = !state;
digitalWrite(pin, state);
}
}
private:
int pin;
unsigned long lastBlink;
bool state;
};
//array of all the LEDs. Adding a LED or changing an interval or pin only requires a change in this line
blinkingLed myLEDs[] = {{2, 200}, {3, 500}, {4, 900}, {5, 1200}, {6, 2000}, {7, 120}};
void setup() {
for (blinkingLed &led : myLEDs) led.begin();
}
void loop() {
for(blinkingLed &led : myLEDs){
led.run();
}
}