Blinking Objects
Three LEDs blink at three speeds at once, each looked after by its own object, with no delay() anywhere.
- Intermediate
- LED & Lighting
- 8 parts
Opens in your browser with the parts, wiring and code already in place. Press Run to watch it work, then change anything you like.
How it works
Three LEDs, each with its own resistor, are wired to pins 11, 12 and 13, just as in the Traffic Light. What is new is how the code is organised. A class is a blueprint that bundles information (here which pin, how often to blink, whether the LED is on, and when it last changed) together with the functions that work on it. Each Blinker object made from the blueprint looks after one LED.
Blinker(11, 250) creates an object for pin 11 that changes every 250 milliseconds; the constructor stores those two numbers. begin() makes the pin an output, and update() is the whole behaviour: it reads millis(), and once the interval has passed since lastChange it flips isOn, writes the pin and notes the time. Nothing ever waits, so the three objects, kept together in the array blinkers, can be updated one after another in loop(), thousands of times a second, and each LED blinks at its own pace: every 250, 500 and 1000 milliseconds. The Traffic Light uses delay() and can only do one thing at a time. The word private: marks the data that only the object’s own functions may touch, and public: marks what the rest of the sketch can call. The Serial tab shows each time an LED turns on.
Parts
- Arduino Uno
- 3 × Resistor
- 3 × LED
- GND
Wiring
- Arduino Uno D11 → Resistor 1 A
- Resistor 1 B → LED 1 Anode (+)
- LED 1 Cathode (-) → GND
- Arduino Uno D12 → Resistor 2 A
- Resistor 2 B → LED 2 Anode (+)
- LED 2 Cathode (-) → GND
- Arduino Uno D13 → Resistor 3 A
- Resistor 3 B → LED 3 Anode (+)
- LED 3 Cathode (-) → GND
Arduino code
// Blinking Objects
// A class bundles information (which pin, how often to blink, whether the LED
// is on) together with the functions that use it. Each Blinker looks after
// one LED on its own timer, so three LEDs blink at three speeds at once
// without a single delay().
class Blinker {
private:
int pin;
unsigned long interval;
unsigned long lastChange;
bool isOn;
public:
Blinker(int ledPin, unsigned long blinkEvery) {
pin = ledPin;
interval = blinkEvery;
lastChange = 0;
isOn = false;
}
void begin() {
pinMode(pin, OUTPUT);
}
void update() {
unsigned long now = millis();
if (now - lastChange >= interval) {
lastChange = now;
isOn = !isOn;
digitalWrite(pin, isOn ? HIGH : LOW);
if (isOn) {
Serial.print("Pin ");
Serial.print(pin);
Serial.println(" on");
}
}
}
};
Blinker blinkers[3] = { Blinker(11, 250), Blinker(12, 500), Blinker(13, 1000) };
void setup() {
Serial.begin(9600);
for (int i = 0; i < 3; i++) {
blinkers[i].begin();
}
}
void loop() {
for (int i = 0; i < 3; i++) {
blinkers[i].update();
}
}
Try changing it
- Change a speed by editing the second number in Blinker(12, 500).
- Add a fourth LED: place another LED and resistor on pin 10 in the editor, make the array Blinker blinkers[4] with a new Blinker(10, 100) in the list, and change both loops from i < 3 to i < 4.