Try Embedded

"C basics: three blinkers, one struct"

Three LEDs blink at their own rates without delay(), each described by a struct in an array, and updated by one function through a pointer.

Basic · Beginner

In a real product: Status lights on a router (Telecom and networking): Each light blinks at its own rate to show a different state, all from one loop.

When you have several things of the same kind, a struct keeps everything about one of them together. Here each blinking LED has a pin, an on time, an off time, its state and the time it last changed. Three of them sit in an array, and one function updates any of them. None of them uses delay(), so all three run at once and the loop stays free.

What you'll learn

  • Declaring a struct, setting it up with { ... }, and reaching its fields with . and ->
  • An array of structs, and a loop over it with sizeof to count the elements
  • Passing a struct by pointer (Blinker *b), so the function changes the real one and not a copy
  • Non-blocking timing with millis(): "has enough time passed?" instead of "wait"

How it works

Blinker holds what a blinker needs. blinkers[] describes three of them: a fast red one, a slow green one, and a blue one that flashes briefly (on 60 ms, off 940 ms), like a heartbeat.

update(Blinker *b, unsigned long now) works out how long the LED should stay in its present state: onMs if it is on, offMs if it is off. If that much time has passed since b->changedAt, it flips the state, writes the pin and notes the time. b->state is short for (*b).state.

Passing &blinkers[i] hands the function the address of the real struct. A struct passed by value would be copied, the copy would change, and the original would never blink.

now - b->changedAt >= interval keeps working when millis() wraps around after 49 days, because unsigned subtraction wraps the same way.

Try this

  • Add a fourth blinker on pin 9 with one line in the array, and nothing else.
  • Add a count field and print each blinker's count every 5 s.
  • Change update to take a Blinker by value and watch the LEDs stop.

sketch.ino

// Three LEDs blinking at their own rates, each described by a struct, all updated without delay().
struct Blinker {
  int pin;
  unsigned long onMs, offMs;
  bool state;
  unsigned long changedAt;
};

Blinker blinkers[] = {
  { 12, 100, 100, false, 0 },   // red: fast
  { 11, 700, 700, false, 0 },   // green: slow
  { 10, 60, 940, false, 0 },    // blue: a short flash each second
};
const int COUNT = sizeof blinkers / sizeof blinkers[0];

void update(Blinker *b, unsigned long now) {
  unsigned long interval = b->state ? b->onMs : b->offMs;
  if (now - b->changedAt >= interval) {
    b->state = !b->state;
    digitalWrite(b->pin, b->state ? HIGH : LOW);
    b->changedAt = now;
  }
}

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < COUNT; i++) pinMode(blinkers[i].pin, OUTPUT);
  Serial.print(COUNT);
  Serial.print(" blinkers, each ");
  Serial.print(sizeof(Blinker));
  Serial.println(" bytes");
}

void loop() {
  unsigned long now = millis();
  for (int i = 0; i < COUNT; i++) update(&blinkers[i], now);

  static unsigned long lastReport = 0;
  if (now - lastReport >= 1000) {
    lastReport = now;
    Serial.print("t=");
    Serial.print(now);
    Serial.print(" ms:");
    for (int i = 0; i < COUNT; i++) Serial.print(blinkers[i].state ? " on " : " off");
    Serial.println();
  }
}

Lessons behind it