Dice Roller
A digital dice roller, controlled by Arduino. Each dice is represented by 7 LEDs that are arranged according to standard dice pip pattern.
There are two dice and one button. Pressing the button rolls the dice, displays a sequence of random numbers, followed by the result.
The LEDs are connected to the Arduino pins as follows:
Hardware
Item | Quantity | Notes |
---|---|---|
Arduino Uno R3 | 1 | |
5mm LED | 14 | Red and Green |
12mm Push button | 1 | |
Resistor | 14 | 220Ω |
Diagram
Pin Connections
Arduino Pin | Part | Location |
---|---|---|
2 | Red LED | Top-left |
3 | Red LED | Top-right |
4 | Red LED | Mid-left |
5 | Red LED | Center |
6 | Red LED | Mid-right |
7 | Red LED | Bottom-left |
8 | Red LED | Bottom right |
9 | Green LED | Top-left |
10 | Green LED | Top-right |
11 | Green LED | Mid-left |
12 | Green LED | Center |
A3 | Green LED | Mid-right |
A4 | Green LED | Bottom-left |
A5 | Green LED | Bottom right |
A0 | Button |
- The LEDs are connected through a 220Ω resistor each.
Video Tutorial
Source code
dice-roller.ino
// Arduino Dice Roller
// Copyright (C) 2020, Uri Shaked
#define BUTTON_PIN A0
const byte die1Pins[] = { 2, 3, 4, 5, 6, 7, 8};
const byte die2Pins[] = { 9, 10, 11, 12, A3, A4, A5};
void setup() {
pinMode(A0, INPUT_PULLUP);
for (byte i = 0; i < 7; i++) {
pinMode(die1Pins[i], OUTPUT);
pinMode(die2Pins[i], OUTPUT);
}
}
void displayNumber(const byte pins[], byte number) {
digitalWrite(pins[0], number > 1 ? HIGH : LOW); // top-left
digitalWrite(pins[1], number > 3 ? HIGH : LOW); // top-right
digitalWrite(pins[2], number == 6 ? HIGH : LOW); // middle-left
digitalWrite(pins[3], number % 2 == 1 ? HIGH : LOW); // center
digitalWrite(pins[4], number == 6 ? HIGH : LOW); // middle-right
digitalWrite(pins[5], number > 3 ? HIGH : LOW); // bottom-left
digitalWrite(pins[6], number > 1 ? HIGH : LOW); // bottom-right
}
bool randomReady = false;
void loop() {
bool buttonPressed = digitalRead(BUTTON_PIN) == LOW;
if (!randomReady && buttonPressed) {
/* Initialize the random number generator with the number of
microseconds between program start and the first button press */
randomSeed(micros());
randomReady = true;
}
if (buttonPressed) {
for (byte i = 0; i < 10; i++) {
int num1 = random(1, 7);
int num2 = random(1, 7);
displayNumber(die1Pins, num1);
displayNumber(die2Pins, num2);
delay(50 + i * 20);
}
}
}