Built-in project · Arduino Uno R3

Tilt dice on an 8x8 matrix, on an Arduino Uno

Shake the tilt switch on pin 2 and an 8x8 MAX7219 matrix (DIN 13, CLK 7, CS 8) rolls a die and settles on a number, while an RGB LED on pins 9, 6 and 5 shows when it has stopped. The whole build, an Arduino Uno and 6 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate Runs in your browser. Free, and no account needed.

Tilt dice on an 8x8 matrix · Arduino Uno R3live0.000 s 0.00x
Click the tilt switch a few times to shake it
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Electronic dice: shake the tilt switch, watch the die roll and stop.
//
//   tilt switch -> pin 2 and GND (INPUT_PULLUP)
//   MAX7219 DIN -> pin 13    CLK -> pin 7    CS -> pin 8
//   RGB LED red -> pin 9, green -> pin 6, blue -> pin 5, each through 220 ohm
//
// A tilt switch is a ball in a tube. Shaken, the ball bounces on the contacts
// and the pin flickers between HIGH and LOW. An interrupt on pin 2 counts
// every flicker without the sketch having to watch for it. While flickers keep
// coming the die keeps rolling; when they stop, it slows down and settles on
// a number, and the RGB LED turns green.
//
// Click the tilt switch a few times to shake it.

#include <LedControl.h>

const int TILT = 2;
const int RED_PIN = 9;
const int GREEN_PIN = 6;
const int BLUE_PIN = 5;

LedControl lc = LedControl(13, 7, 8, 1);  // DIN, CLK, CS, modules

// One face per number, eight rows each.
const byte FACES[6][8] = {
    {0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00},
    {0x00, 0x06, 0x06, 0x00, 0x00, 0x60, 0x60, 0x00},
    {0xC0, 0xC0, 0x00, 0x18, 0x18, 0x00, 0x03, 0x03},
    {0x00, 0x66, 0x66, 0x00, 0x00, 0x66, 0x66, 0x00},
    {0xC3, 0xC3, 0x00, 0x18, 0x18, 0x00, 0xC3, 0xC3},
    {0x66, 0x66, 0x00, 0x66, 0x66, 0x00, 0x66, 0x66},
};

volatile unsigned int shakes = 0;
unsigned int seen = 0;

void onShake() {
  shakes++;
}

void showFace(int n) {
  for (int row = 0; row < 8; row++) lc.setRow(0, row, FACES[n - 1][row]);
}

void color(int r, int g, int b) {
  analogWrite(RED_PIN, r);
  analogWrite(GREEN_PIN, g);
  analogWrite(BLUE_PIN, b);
}

void roll() {
  color(0, 0, 160);
  int n = 1;
  int wait = 40;
  while (wait < 300) {
    n = random(1, 7);
    showFace(n);
    delay(wait);
    if (shakes != seen) {
      seen = shakes;
      wait = 40;  // still shaking: keep rolling fast
    } else {
      wait += 30;
    }
  }
  color(0, 160, 0);
  Serial.print("rolled a ");
  Serial.println(n);
}

void setup() {
  pinMode(TILT, INPUT_PULLUP);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  lc.shutdown(0, false);
  lc.setIntensity(0, 6);
  lc.clearDisplay(0);
  attachInterrupt(digitalPinToInterrupt(TILT), onShake, CHANGE);
  randomSeed(analogRead(A5));
  Serial.begin(9600);
  Serial.println("Mokxi Uno: tilt dice. Shake the tilt switch.");
  roll();
}

void loop() {
  if (shakes != seen) {
    seen = shakes;
    roll();
  }
}

Parts list

8 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

12 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 13; LED matrix, 8x8 pin DIN
  • Arduino Uno R3 pin 9; Resistor, 220 Ω (3) pin 1
  • Arduino Uno R3 pin 8; LED matrix, 8x8 pin CS
  • Arduino Uno R3 pin 7; LED matrix, 8x8 pin CLK
  • Arduino Uno R3 pin 6; Resistor, 220 Ω (2) pin 1
  • Arduino Uno R3 pin 5; Resistor, 220 Ω (1) pin 1
  • Arduino Uno R3 pin 2; Tilt switch pin 2
  • Arduino Uno R3 pin 5V; LED matrix, 8x8 pin VCC
  • Ground: Arduino Uno R3 pin GND; RGB LED pin C; Tilt switch pin 1; LED matrix, 8x8 pin GND
  • RGB LED pin R; Resistor, 220 Ω (3) pin 2
  • RGB LED pin G; Resistor, 220 Ω (2) pin 2
  • RGB LED pin B; Resistor, 220 Ω (1) pin 2

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.