16/10/2018

# Arduino Dice simulator

I was looking at my kids playing snake and ladder tonight, and came an idea in my mind “why don’t I make a dice simulator?”. I already had the LED matrix from the previous post here. So I created the Arduino Dice simulator using my previous circuit.

So I added a permanent button to the circuit that I had using a pull-down resistor of 10K ohm which connects pin 2 to the ground.

Then with little modification to the code as following

``````// LED matrix with latch 74HC595N
// the program random display the character
// the button is added and the character will only be displayed if after the button is pressed
// added code to simulate a dice

int latchPin = 4; // pis connected to shift registors
int clockPin = 5;
int dataPin = 3;
int pins [8] = {6, 7, 8, 9, 10, 11, 12, 13}; // common cathode pins
int button = 2; // connect button and 10K resistor to GND (normal LOW), the other end to 5V
byte A[8] = {   B00000000, // Letters are defined
B00011000,// you can create your own
B00100100,
B01000010,
B01111110,
B01000010,
B01000010,
B00000000
};
byte B[8] = {0x0, 0x7c, 0x42, 0x42, 0x7c, 0x42, 0x42, 0x7c};
byte C[8] = {0x0, 0x3c, 0x42, 0x40, 0x40, 0x40, 0x42, 0x3c};
byte D[8] = {0x0, 0x7c, 0x42, 0x42, 0x42, 0x42, 0x42, 0x7c};
byte E[8] = {0x0, 0x7e, 0x40, 0x40, 0x7c, 0x40, 0x40, 0x7e};
byte F[8]={0x0, 0x7e, 0x40, 0x40, 0x7c, 0x40, 0x40, 0x40};
byte G[8]={0x0, 0x3c, 0x42, 0x40, 0x4e, 0x42, 0x42, 0x3c};
byte H[8]={0x0, 0x42, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x42};
byte I[8]={0x0, 0x70, 0x20, 0x20, 0x20, 0x20, 0x20, 0x70};
byte J[8]={0x0, 0x7c, 0x4, 0x4, 0x4, 0x4, 0x44, 0x38};
byte K[8]={0x0, 0x44, 0x48, 0x50, 0x60, 0x50, 0x48, 0x44};
byte L[8]={0x0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7c};
byte M[8]={0x0, 0x42, 0x66, 0x5a, 0x42, 0x42, 0x42, 0x42};
byte N[8]={0x0, 0x42, 0x62, 0x72, 0x5a, 0x4e, 0x46, 0x42};
byte O[8]={0x0, 0x3c, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3c};
byte P[8]={0x0, 0x7c, 0x42, 0x42, 0x7c, 0x40, 0x40, 0x40};
byte Q[8]={0x0, 0x3c, 0x42, 0x42, 0x42, 0x4a, 0x44, 0x3a};
byte R[8]={0x0, 0x7c, 0x42, 0x42, 0x7c, 0x48, 0x44, 0x42};
byte S[8]={0x0, 0x3e, 0x40, 0x40, 0x3c, 0x2, 0x2, 0x7c};
byte T[8]={0x0, 0x7c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10};
byte U[8]={0x0, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3c};
byte V[8]={0x0, 0x42, 0x42, 0x42, 0x42, 0x42, 0x24, 0x18};
byte W[8]={0x0, 0x42, 0x42, 0x42, 0x42, 0x5a, 0x66, 0x42};
byte X[8]={0x0, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x0};
byte Y[8]={0x0, 0x44, 0x44, 0x28, 0x10, 0x10, 0x10, 0x10};
byte Z[8]={0x0, 0x7e, 0x4, 0x8, 0x10, 0x20, 0x40, 0x7e};
byte X1[8]={0x0, 0x0, 0x18, 0x3c, 0x3c, 0x18, 0x0, 0x0};
byte X2[8]={0x0, 0x20, 0x70, 0x20, 0x4, 0xe, 0x4, 0x0};
byte X3[8]={0x40, 0xe0, 0x40, 0x18, 0x18, 0x2, 0x7, 0x2};
byte X4[8]={0x42, 0xe7, 0x42, 0x0, 0x0, 0x42, 0xe7, 0x42};
byte X5[8]={0x42, 0xe7, 0x42, 0x18, 0x18, 0x42, 0xe7, 0x42};
byte X6[8]={0x66, 0x66, 0x0, 0x66, 0x66, 0x0, 0x66, 0x66};
byte blank[8] = { B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000};
long randNumber;
int buttonpressed = 0;
long time = 0; // last time the button pressed
long debounce = 50;

void setup() {
Serial.begin(9600); // Serial begin
pinMode(latchPin, OUTPUT); // Pin configuration
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
for (int i = 0; i < 8; i++) { // for loop is used to configure common cathodes
pinMode(pins[i], OUTPUT);
digitalWrite(pins[i], HIGH);
}
pinMode(button, INPUT);   // set button input
}

void loop() {
if (buttonpressed == HIGH && millis() - time > debounce){
time = millis();
randNumber = random(1,6);
Serial.println(randNumber);

for (int k = 0; k < 1000; k++) { // showing each letter for 1 second
getChar(randNumber);

}
} else {
display_char(X1);delay(20);
display_char(X2);delay(20);
display_char(X3);delay(20);
display_char(X4);delay(20);
display_char(X5);delay(20);
display_char(X6);delay(20);

}

// add more letters show method here

}

void getChar(int num){
switch (num){
case 1: display_char(X1); break;
case 2: display_char(X2); break;
case 3: display_char(X3); break;
case 4: display_char(X4); break;
case 5: display_char(X5);break;
case 6: display_char(X6);break;
default:
break;
}

}
void display_char(byte ch[8]) { // Method do the multiplexing
for (int j = 0; j < 8; j++) {
digitalWrite(latchPin, LOW);
digitalWrite(pins[j], LOW);

shiftOut(dataPin, clockPin, LSBFIRST, ch[j]);
digitalWrite(latchPin, HIGH);
//delay(1);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, B00000000); // to get rid of flicker when
digitalWrite(latchPin, HIGH);
digitalWrite(pins[j], HIGH);

}
}

``````

I hope you enjoyed this short post. You can view the result in the following video

If you like what I post, don’t forget to subscribe.