"C basics: an LED chaser that walks with a pointer"
Six LEDs play patterns stored in arrays; a pointer walks each pattern to its end marker, and the button points it at the next one.
Basic · Beginner
In a real product: Running lights on a sign (Consumer electronics): A sign plays light patterns that are just tables of bits in memory.
A pointer is just an address, and pointer arithmetic is stepping from one element of an array to the next. Here each light pattern is an array of bytes, and each byte is one frame: bit 0 is the first LED, bit 5 the last. A pointer walks through the frames until it reaches the end marker 0xFF. The button makes it point at the start of the next pattern instead.
What you'll learn
const uint8_t *frameand moving it withframe++, and reading through it with*frame- An array's name used as a pointer to its first element, so
frame = PATTERNS[i]needs no& - An array of pointers to arrays of different lengths, each with an end marker (a sentinel)
- Testing the bits of a byte with
frame_bits & (1 << led)
How it works
PATTERNS holds three pointers, one to each pattern array. pattern is the number of the current one and frame points into it. Every 120 ms, show(*frame) lights the LEDs whose bits are set, then frame++ moves to the next byte. When *frame == END the pointer goes back to the start of the same pattern.
The arrays have different lengths, and nothing outside them stores how long they are. The end marker does that job, just as '\0' ends a C string. 0xFF works as the marker because no frame lights all eight bits: there are only six LEDs.
show() loops over the six LEDs and writes each one's bit. The pins are in an array too, so the wiring can change without touching the logic.
The button is read with INPUT_PULLUP: it reads LOW while pressed. Only the change from released to pressed counts, so holding it down moves on just once.
Try this
- Add a fourth pattern, a bouncing pair of LEDs, and add its name to
PATTERNS. - Print
(int)(frame - PATTERNS[pattern]): subtracting two pointers gives the frame number. - Remove
ENDfrom one pattern and see what the pointer walks into.
sketch.ino
// Six LEDs play patterns stored as arrays of frames (one bit per LED); a pointer walks each pattern to its END
// marker. The button on pin 2 points it at the next pattern.
const uint8_t END = 0xFF;
const uint8_t CHASE[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, END };
const uint8_t FILL[] = { 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01, 0x00, END };
const uint8_t CENTRE[] = { 0x0C, 0x12, 0x21, 0x12, END };
const uint8_t *const PATTERNS[] = { CHASE, FILL, CENTRE };
const char *const NAMES[] = { "chase", "fill", "centre" };
const int COUNT = sizeof PATTERNS / sizeof PATTERNS[0];
const int PINS[6] = { 3, 4, 5, 6, 7, 8 };
const int BUTTON = 2;
int pattern = 0;
const uint8_t *frame = PATTERNS[0]; // the frame showing now
void show(uint8_t bits) {
for (int led = 0; led < 6; led++) digitalWrite(PINS[led], bits & (1 << led) ? HIGH : LOW);
}
void setup() {
Serial.begin(9600);
for (int i = 0; i < 6; i++) pinMode(PINS[i], OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
Serial.println("Pattern: chase");
}
void loop() {
static bool wasPressed = false;
bool pressed = digitalRead(BUTTON) == LOW;
if (pressed && !wasPressed) {
pattern = (pattern + 1) % COUNT;
frame = PATTERNS[pattern]; // the array's name is the address of its first frame
Serial.print("Pattern: ");
Serial.println(NAMES[pattern]);
}
wasPressed = pressed;
show(*frame);
frame++;
if (*frame == END) frame = PATTERNS[pattern];
delay(120);
}