Lessons and the learning game

Generative art and cellular automata

Turn a 16x16 LED panel into a night sea in C++ on an ESP32-C3: Life, random walks, L-systems, noise, flocking fish and a living light wall.

The course

Generative art and cellular automata is an intermediate Coding course in seven lessons. Every lesson runs on one circuit, a 16x16 WS2812B panel on GPIO 5, and opens its own sketch. Steps are checked against what the board prints (generation counts, measured distances, symbol counts, noise statistics) and against the panel itself, which reports how many of its 256 LEDs are lit.

  1. Game of Life: Conway's B3/S23 on a wrapped 16x16 torus, rules as two bitmasks, and a fingerprint of each generation that spots the R-pentomino settling at generation 69. One extra birth rule (HighLife) and the same five cells die out by generation 9.
  2. Random walks: glowing plankton from xorshift32, trails that fade by an eighth a frame, and 64 unseen walkers that measure the square root law: 100 steps go a typical distance of 10.
  3. L-systems: rewriting rules that grow a string from 1 to 451 symbols in four generations, and a turtle with a stack that draws it as kelp.
  4. Noise fields: value noise in 8.8 fixed point, a hashed lattice, smoothstep and octaves, measured against static (a neighbor step of 16 against 95) and drawn as a drifting bioluminescent tide.
  5. Flocking: twelve fish and Reynolds' three rules. With alignment off the school's order wanders below 0.5; switch it on and it passes 0.9.
  6. Mapping a matrix: from x and y to a place on one wire, serpentine rows, rotation in one function, and a panel rewired straight.
  7. Project: a living light wall.

The finale

The wall plays four scenes from the course, a plankton bloom, drifting walkers, a noise tide and a fish school, from a playlist. Every scene is a start function and a frame function, so the running order is data and a new scene is one more pair. The last step is adding a scene of your own.

The panel

The panel is 256 WS2812B LEDs on one data wire, folded into serpentine rows. Each frame is 6,144 bits, timed to the data sheet's pulse widths, about 8 ms of the board's time with interrupts held off. The part decodes the real protocol, so a sketch with the wrong timing or the wrong mapping looks wrong here as it would on a desk. Its serpentine property switches the wiring.

How the C3 engine differs

The board runs Mokxi's own firmware runtime, not the Arduino core: the same setup() and loop(), with mokxi_ws2812.h for the panel. There is no floating point unit, so the noise and the fish are fixed point; floating point works in software and the sketches use it only for a few square roots each second.

Everything is deterministic: a seed gives the same art on every run, which is how every number in the lessons can be checked.

Where next