A couple of days ago I got a Xino board like this one. Not is this about the cheapest way to get an Ardunio compatible microprocessor, but it has the benefit of a nice little prototype area on board too. So, although it fits standard Arduino shields, many simple projects can be completed on just the one board.
I wanted to do something simple and nice with it, and with components and hardware I had to hand. I realised the prototype area would be great for mounting LEDs, and so the idea of a digital dice was born. I gathered a few bits and bobs and got the soldering iron out.
One idea I had was to mount the LEDs on the underside of the board. That would give them a nice solid mounting, as well as let me mount the board on flat the lid of the box. It’s the first project where I’ve been working on components on both sides of the same area, and because I wanted the underside to go as close to the lid as possible, it meant I was restricted about what I put there. That meant the resistor mounting wasn’t quite as neat as I’d like on the component side, but it works pretty well.
All 7 LEDs go to their own digital output (D2 – D8). For a dice I could have got away with linking each of the opposite corners and the two middle LEDs so that only used 4 outputs. However, I didn’t need the outputs for anything else and I wanted to have a cool little rotating animation when the dice was ‘rolled’. In order to roll the dice, I used a small strip of Veroboard connected to one of the analogue inputs and ground. (It would be nice to have a little custom PCB to neaten things up a bit, but that might have to wait for Dice 2.0). The analogue input is connected to +5V via a 10K resistor. When the strip is touched, the resistance of your finger changes the value on that pin and it starts the ‘roll’ animation. A random number between 1 and 6 is picked and this is then shown. Nothing too clever, but here’s the code anyway;
int dice [6] [7] = {{0,0,0,0,0,0,1}, // 1 4 5
{0,0,1,0,0,1,0}, // 2 3 8 6
{0,0,1,0,0,1,1}, // 3 2 7
{1,0,1,1,0,1,0}, // 4
{1,0,1,1,0,1,1}, // 5
{1,1,1,1,1,1,0}}; // 6
int roll [6] [7] = {{1,0,0,0,0,0,1}, // rolling animation
{0,1,0,0,0,0,1},
{0,0,1,0,0,0,1},
{0,0,0,1,0,0,1},
{0,0,0,0,1,0,1},
{0,0,0,0,0,1,1}};
int dieValue = 6;
void setup(){
for (int i = 2; i < 9; i++) {
pinMode (i, OUTPUT);
digitalWrite (i, LOW);
}
pinMode (A2, OUTPUT);
digitalWrite (A2, HIGH);
pinMode (A5, INPUT);
}
void count (){ //can be used as an alternative to roll .
for (int i = 1; i < 7; i++){ // Shows numbers 1 - 6 on the dice in order
for (int p = 0; p < 7; p++){
int state = dice [i-1] [p];
digitalWrite (p+2, state);
}
}
void spin(){ // gives a simple clockwise revolving
for (int i = 1; i < 7; i++){ //animation around the center spot
for (int p = 0; p < 7; p++){
int state = roll [i-1] [p];
digitalWrite (p+2, state);
}
delay (50);
}
}
void displayDie(){
for (int p = 0; p < 7; p++){
int state = dice [dieValue - 1] [p];
digitalWrite (p+2, state);
}
}
void loop(){
displayDie();
int touchpad = analogRead(A5);
if (touchpad < 1020){
dieValue = (random(6)+1);
for (int i = 1; i < 3 ; i++){ //minimum of 3 spins whilst rolling
spin();
}
}
}
And here's a little video of the dice in action