TinkerBlock UNO kit/Colour/11. Lab: police lights
Colour · 11 of 21

Lab: police lights

The TK33 bar on D5 flashes like a patrol car's light bar: the top two LEDs red twice, the bottom two blue twice, 620 ms a cycle. The pattern is a table of eight frames, and loop() plays it by checking millis(), so it never waits and a new pattern is new rows, not new code.

One module, the TK33 bar, driven by the library from the first colours and timed with millis(). The bar stays in its D4/D5 socket, on D5.

ModuleSocketPin
TK33 WS2812 LED barD4/D5D5

A pattern is a table

A flash pattern written as code is a long run of fill(), show() and delay(), and changing the rhythm means reading all of it. Written as data it is a short table: what each LED shows, and for how long.

const Frame FRAMES[] = {
  { "RR...", 70 }, { ".....", 50 },   // red, twice
  { "RR...", 70 }, { ".....", 120 },
  { "...BB", 70 }, { ".....", 50 },   // blue, twice
  { "...BB", 70 }, { ".....", 120 },
};

Each string is the five LEDs, top first: R red, B blue, W white, . dark. The numbers add up to 620 ms a cycle.

A pattern is a table
620 ms a cycle
Frame
—
Sketch time
0 ms
Cycle
620 ms
Press run. Each row of the table is one frame: which LEDs are lit, and for how long.

Run it at real speed, then five times slower to see each frame. Pick Slow wig-wag and the same loop() plays a different table, two rows of 300 ms. The table is the only thing that changed.

Playing it without waiting

loop() asks one question each time round: has the current frame had its time?

if (millis() - frameStart >= FRAMES[frame].ms) {
  frameStart += FRAMES[frame].ms;
  frame = (frame + 1) % FRAME_COUNT;
  showFrame(frame);
}

Most times round the answer is no, and loop() ends at once. Nothing waits, so a button, a sensor or a buzzer can be read in the same loop() without the lights stopping.

frameStart += FRAMES[frame].ms adds the frame's own time rather than setting frameStart = millis(). If a frame starts a millisecond late, the next one is not pushed back with it, so the rhythm keeps time over minutes.

showFrame() turns the letters into colours with colourOf() and sends all five with one show(). Five LEDs take about 150 µs to send, 24 bits each at 1.25 µs a bit, so the cost of a frame change is nothing a person could see.

Make it yours

  • The slow wig-wag from the figure: replace the table with { "RR...", 300 }, { "...BB", 300 },.
  • Add a white strobe in the middle LED: rows like { "..W..", 40 }.
  • Put the TK04 button in its D6/D7 socket and switch between two tables on each press; because loop() never waits, the press is seen the moment it happens.

The code

Police_Lights.ino

A table of frames, each a string of five letters for the five LEDs and a time in milliseconds. loop() moves to the next frame when the current one has had its time, and does nothing else.

/*
  Police lights                       TinkerBlock UNO kit, /manuals/tkbu

  The TK33 WS2812 LED bar flashes like a patrol car's light bar: the
  top two LEDs red twice, the bottom two blue twice, over and over.
  The pattern is a table of frames, and loop() never waits, so the
  sketch can do something else between frames.

  Wiring, on the TinkerBlock UNO shield:
    TK33 WS2812 LED bar    in the D4/D5 socket, so it is on D5
  A module is on the second pin printed beside its socket.

  Arduino IDE
    Tools > Board: Arduino Uno
    Tools > Port:  the CH340 port
    Library: Adafruit NeoPixel by Adafruit, 1.15.5, from
             Sketch > Include Library > Manage Libraries.
*/

#include <Adafruit_NeoPixel.h>

const int DATA_PIN = 5;    // TK33 DATA
const int LEDS = 5;
const int BRIGHTNESS = 80; // of 255

Adafruit_NeoPixel bar(LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);

// One frame: what each LED shows, top to bottom, and for how long.
// R red, B blue, W white, . dark.
struct Frame {
  const char *leds;
  unsigned int ms;
};

const Frame FRAMES[] = {
  { "RR...", 70 }, { ".....", 50 },   // red, twice
  { "RR...", 70 }, { ".....", 120 },
  { "...BB", 70 }, { ".....", 50 },   // blue, twice
  { "...BB", 70 }, { ".....", 120 },
};
const int FRAME_COUNT = sizeof(FRAMES) / sizeof(FRAMES[0]);

int frame = 0;
unsigned long frameStart = 0;

uint32_t colourOf(char c) {
  if (c == 'R') return bar.Color(255, 0, 0);
  if (c == 'B') return bar.Color(0, 0, 255);
  if (c == 'W') return bar.Color(255, 255, 255);
  return 0;
}

void showFrame(int f) {
  for (int i = 0; i < LEDS; i++) {
    bar.setPixelColor(i, colourOf(FRAMES[f].leds[i]));
  }
  bar.show();
}

void setup() {
  bar.begin();
  bar.setBrightness(BRIGHTNESS);
  showFrame(0);
  frameStart = millis();
}

void loop() {
  // Next frame only when this one has had its time.
  if (millis() - frameStart >= FRAMES[frame].ms) {
    frameStart += FRAMES[frame].ms;  // keeps the rhythm exact
    frame = (frame + 1) % FRAME_COUNT;
    showFrame(frame);
  }
  // Anything else the sketch does goes here, and must not wait.
}

R is red, B is blue, W is white and a full stop is dark, top LED first. Edit the rows to change the pattern; add rows for a longer one. FRAME_COUNT follows the table by itself.

View on GitHub · kits/tkbu/police-lights/Police_Lights/Police_Lights.ino @ v1.14

When it does not work

The red flashes are green.

The strip was created with NEO_RGB. These LEDs read green first, so the line must say NEO_GRB + NEO_KHZ800. Blue, the third colour either way, looks right, which hides the mistake until red is asked for.

The pattern is a blur or a steady glow.

Frames of a few milliseconds are too short to see as flashes. The sketch's shortest is 50 ms. If you edited the table, keep each lit frame at 50 ms or more and give the gaps time too.

Only part of a frame lights, or the wrong LEDs light.

Each string must be exactly five characters, one per LED, top first. A string of four leaves the fifth LED dark, and a sixth letter is ignored. Count the characters in the row you changed.

The rhythm drifts when I add code to loop().

Something in loop() now waits: a delay(), or a long loop of its own. Frames still change, but late. Keep loop() free of waits; the sketch advances frameStart by the frame's own time so a late frame does not push every later one back.

Where this goes next

A button that throws random colours onto the bar, and a knob for brightness.

Lab: a colour DJ →

Edit this page — content/books/tkbu/police-lights.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

TinkerBlock UNO R3 Starter Kit, 12 Modules

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →