TinkerBlock UNO kit/Colour/12. Lab: a colour DJ
Colour · 12 of 21

Lab: a colour DJ

Each press of the TK04 button on D7 throws a new random colour onto the TK33 bar on D5, and the TK07 knob on A0 sets how bright it is. random() on an UNO repeats the same list after every power-up unless it is seeded, so the sketch seeds it from the moment of the first press.

Three modules: the TK04 button, the TK33 bar and the TK07 knob. The button is debounced, the bar runs on the library from the first colours, and the knob is read as in the dimmer.

Where they plug in

ModuleSocketPin
TK04 push buttonD6/D7D7
TK33 WS2812 LED barD4/D5D5
TK07 disc potentiometerD9/A0A0

The TK04 has its own 10 kΩ pull-down and connects SIGNAL to VCC when pressed, so D7 reads HIGH while it is held, and the sketch sets the pin to INPUT. INPUT_PULLUP would fight the pull-down for nothing.

Random is a list

random() does not roll dice. It computes each number from the one before, starting from a fixed number, so after every power-up it gives the same list in the same order. A colour DJ without a seed plays the same show every time.

Three power-ups
no randomSeed()
Press run: the board is powered up three times, and the button pressed four times after each.

With No seed, the three power-ups throw identical colours. With Seeded at the first press, each run starts from a different place in the list. The figure's seeds are invented press times; the pattern is the point.

The seed has to be something that differs between runs. The sketch uses the moment of the first press:

if (!seeded) {
  randomSeed(micros());
  seeded = true;
}

micros() counts in steps of 4 µs on a 16 MHz UNO, and nobody presses a button to within 4 µs of the same instant twice. The same call in setup() would be useless: nothing human has happened yet, and the board reaches setup() at almost the same microsecond every time.

Colours that never disappoint

A random red, green and blue each from 0 to 255 is often a muddy brown or a near-white, and sometimes almost the colour before. The sketch picks a hue instead, a position on the colour wheel from 0 to 65535, and shows it at full saturation, so every colour is a pure one:

uint16_t step = MIN_STEP + random(65536L - 2L * MIN_STEP);
hue += step;

Every step is at least a sixth of the wheel, either way round, so each press visibly changes the colour. hue is a 16-bit number, so passing 65535 wraps back round the wheel by itself.

The knob sets the brightness

The knob's reading divided by four is the brightness, 0 to 255, and the sketch passes it straight to the colour:

uint32_t c = bar.ColorHSV(hue, 255, level);
bar.fill(bar.gamma32(c));

gamma32() applies the eye's curve, the same idea as squaring in the dimmer, so the knob feels even. The sketch does not use setBrightness() here: called over and over, it rescales the colours already stored and they lose detail each time. It sends the bar again only when the knob has moved by more than one step, so a resting knob costs nothing.

Make it yours

  • A long press for white: time how long stable stays HIGH, as in a long press, and fill with saturation 0.
  • One LED per press: keep five hues and move them down the bar, newest at the top.
  • Throw only warm colours: limit the hue to the reds, oranges and yellows, roughly 0 to 11000.

The code

Colour_DJ.ino

A debounced button that picks a new hue on each press, at least a sixth of the colour wheel from the last, and a knob read every time round loop() that sets the brightness. The bar starts red. The Serial Monitor prints each new hue.

/*
  A colour DJ                         TinkerBlock UNO kit, /manuals/tkbu

  Each press of the TK04 button throws a new random colour onto the
  TK33 WS2812 LED bar, and the TK07 knob sets how bright it is. The
  random numbers are seeded from the moment of the first press, so the
  colours are not the same every time the board starts.

  Wiring, on the TinkerBlock UNO shield:
    TK04 push button           in the D6/D7 socket, so it is on D7
    TK33 WS2812 LED bar        in the D4/D5 socket, so it is on D5
    TK07 disc potentiometer    in the D9/A0 socket, so it is on A0
  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.
    Serial Monitor at 115200.
*/

#include <Adafruit_NeoPixel.h>

const int BUTTON_PIN = 7;  // TK04 SIGNAL: HIGH while pressed
const int DATA_PIN = 5;    // TK33 DATA
const int KNOB_PIN = A0;   // TK07 SIGNAL
const int LEDS = 5;

const unsigned long DEBOUNCE_MS = 20;   // the TK04's book
const uint16_t MIN_STEP = 65536 / 6;    // a sixth of the wheel

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

bool seeded = false;
uint16_t hue = 0;          // 0 to 65535: once round the colour wheel
int level = 0;             // the brightness last shown, 0 to 255

bool stable = LOW;         // the button, once it has held still
bool lastRaw = LOW;
unsigned long lastChange = 0;

void show() {
  // Saturation 255: always a full colour, never a grey or white.
  uint32_t c = bar.ColorHSV(hue, 255, level);
  bar.fill(bar.gamma32(c));
  bar.show();
}

void newColour() {
  if (!seeded) {
    // micros() counts in 4 us steps, and nobody presses a button to
    // within 4 us of the same moment twice.
    randomSeed(micros());
    seeded = true;
  }
  // A new hue at least a sixth of the wheel from the last one, so
  // every press visibly changes the colour.
  uint16_t step = MIN_STEP + random(65536L - 2L * MIN_STEP);
  hue += step;             // wraps round the wheel by itself
  Serial.print("hue ");
  Serial.println(hue);
}

void setup() {
  Serial.begin(115200);
  pinMode(BUTTON_PIN, INPUT); // the TK04 has its own pull-down
  bar.begin();
  level = analogRead(KNOB_PIN) / 4;
  show();                  // red, at the knob's brightness
}

void loop() {
  // Believe the button only once it has held still for 20 ms.
  bool raw = digitalRead(BUTTON_PIN);
  if (raw != lastRaw) {
    lastRaw = raw;
    lastChange = millis();
  }
  if (millis() - lastChange >= DEBOUNCE_MS && raw != stable) {
    stable = raw;
    if (stable == HIGH) {   // pressed, not released
      newColour();
      show();
    }
  }

  // The knob: show again only when the brightness has changed.
  int now = analogRead(KNOB_PIN) / 4;
  if (abs(now - level) > 1) {
    level = now;
    show();
  }
}

The first press seeds the random numbers with micros(), so the colours differ every time the board starts. Comment out the randomSeed() line and press reset a few times to see the same colours come back in the same order.

View on GitHub · kits/tkbu/colour-dj/Colour_DJ/Colour_DJ.ino @ v1.14

When it does not work

Pressing the button does nothing.

The TK04 must be in the D6/D7 socket, on D7. Its own LED lights while it is pressed, which shows the module has power. The sketch uses INPUT, not INPUT_PULLUP: the TK04 has a pull-down on the board and reads HIGH when pressed.

One press changes the colour two or three times.

The contact bounces, and something is reading it without the 20 ms wait. Check DEBOUNCE_MS is 20 and that the colour changes only where stable becomes HIGH, not every time raw changes.

The bar is dark until I turn the knob.

The knob is at its bottom stop, so the brightness is 0. Turn it up. If turning it does nothing, the TK07 is not in the D9/A0 socket or its cable is half in.

The colours are the same after every reset.

randomSeed() is not being called, or it is called with the same number every time, such as a constant. The sketch calls it once, at the first press, with micros(); if you moved it into setup() it gets nearly the same value at every start, because nothing human has happened yet.

Where this goes next

The button keys the buzzer, and the sketch reads the dots and dashes back.

Lab: a Morse code key →

Edit this page — content/books/tkbu/colour-dj.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 →