When I’m stressed I build things.

Some people go for a walk, some people to to the pub but when I’m stressed I build things. My son and I like playing board games but those dice always go rolling all over the place. To solve the problem and to “de-stress” myself I built and Arduino dice. There are loads of circuits around on the Internet and in books, but I just made my own so that I can concentrate on it and forget about the world around me.

This version uses a seven segment LED display and seven Arduino pins. It also uses a button which uses another one of the Arduino pins. Each of the LED segments is connected to an Arduino pin via a 470 ohm resistor. The button uses a 10KOhm pull down resistor to keep the pin low and when the button is pressed the pin goes high. While the pin is high the Arduino program will generate random numbers and when the button is released the last number generated is used.

Below is the code and a small video for the dice.


#define A 11
#define B 10
#define C 6
#define D 3
#define E 2
#define F1 12
#define G 13
#define H 6
#define SEGMENTS 7

const int buttonPin = 8;
int buttonState = 0;
const int delaid = 400;
char zero[SEGMENTS] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW};
char one[SEGMENTS] = {LOW, HIGH, HIGH, LOW, LOW, LOW, LOW};
char two[SEGMENTS] = {HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH};
char three[SEGMENTS] = {HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH};
char four[SEGMENTS] = {LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH};
char five[SEGMENTS] = {HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH};
char six[SEGMENTS] = {HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH};
char seven[SEGMENTS] = {HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW};
char eight[SEGMENTS] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
char nine[SEGMENTS] = {HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH};
int seed;
int num = 0;

void setup() {
Serial.begin(9600);
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F1, OUTPUT);
pinMode(G, OUTPUT);
pinMode(H, OUTPUT);
pinMode(buttonPin, INPUT);
randomSeed(analogRead(0));

Serial.println(num);
}

void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
num = random(1, 7);
} else {
switch (num) {
case 1:
disp(one);
break;
case 2:
disp(two);
break;
case 3:
disp(three);
break;
case 4:
disp(four);
break;
case 5:
disp(five);
break;
case 6:
disp(six);
break;
}
}

}

 

void disp(char segment[]) {
digitalWrite(A, segment[0]);
digitalWrite(B, segment[1]);
digitalWrite(C, segment[2]);
digitalWrite(D, segment[3]);
digitalWrite(E, segment[4]);
digitalWrite(F1, segment[5]);
digitalWrite(G, segment[6]);
}