Dice Duo Matrix

by AZDelivery in Circuits > Arduino

69 Views, 0 Favorites, 0 Comments

Dice Duo Matrix

Screenshot 2025-03-12 132513.png

Welcome to this exciting Arduino project where we'll create an interactive electronic dice using an Nano V3 microcontroller, a pushbutton, and two LED matrix displays. When the button is pressed, the LED matrices will simulate rolling dice by displaying an animation and then show random dice faces. This project is a fun way to dive into programming microcontrollers and working with LED displays.

Supplies

Circuit Connection

Nano V3 -> LED Matrix

D12 -> Din

D11 -> CLK

D10 -> CS

5V -> VCC

GND -> GND


The Button is connected to D2 and GND and is pulled LOW when pushed.

The second LED Matrix is daisy-chained.


ArduinoCode

You will need to install the “LedControl” library. To do so open the Arduino IDE and go to the Library Manager. Search for “LedControl” (byEberhardFahle) and install.


#include "LedControl.h"
// LedControl pins:
// 12 - DataIn
// 11 - CLK
// 10 - CS
// We have two MAX72XX devices daisy-chained.
LedControl lc = LedControl(12, 11, 10, 2);
/* Dice face patterns */
byte dice[6][8] = {
{ // One
B00000000, B00000000, B00000000, B00011000, B00011000, B00000000,
B00000000, B00000000
},
{ // Two
B00000000, B00000000, B00000000, B01100110, B01100110, B00000000,
B00000000, B00000000
},
{ // Three
B11000000, B11000000, B00000000, B00011000, B00011000, B00000000,
B00000011, B00000011
},
{ // Four
B00000000, B01100110, B01100110, B00000000, B00000000, B01100110,
B01100110, B00000000
},
{ // Five
B00000000, B01100110, B01100110, B00011000, B00011000, B01100110,
B01100110, B00000000
},
{ // Six
B00000000, B11011011, B11011011, B00000000, B00000000, B11011011,
B11011011, B00000000
}
4
};
const int buttonPin = 2; // the number of the pushbutton pin
int buttonState; // the current reading from the input pin
int lastButtonState = HIGH; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the button input
pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if
the output flickers
void setup() {
// Initialize the MAX72XX devices
for (int i = 0; i < 2; i++) {
lc.shutdown(i, false); // Wake up the MAX72XX
lc.setIntensity(i, 8); // Set brightness to medium
lc.clearDisplay(i); // Clear the display
}
// Seed the random number generator
randomSeed(analogRead(0));
// Initialize the pushbutton pin as an input with internal pull-up
resistor
pinMode(buttonPin, INPUT_PULLUP);
// Initialize button states
buttonState = digitalRead(buttonPin);
lastButtonState = buttonState;
}
void displayDieFace(byte face[8], int device) {
for (int i = 0; i < 8; i++) {
lc.setRow(device, i, face[i]);
}
}
void animate() {
unsigned long startTime = millis();
unsigned long animationDuration = 1000; // 1 second
while (millis() - startTime < animationDuration) {
// Generate and display a random pattern on both devices
for (int device = 0; device < 2; device++) {
for (int i = 0; i < 8; i++) {
lc.setRow(device, i, random(0, 256));
5
}
}
// Wait for a random short time
delay(random(50, 200));
}
// Clear both displays
for (int device = 0; device < 2; device++) {
lc.clearDisplay(device);
}
}
void loop() {
// Read the state of the switch into a local variable
int reading = digitalRead(buttonPin);
// Check if the reading is different from the last reading
if (reading != lastButtonState) {
// Reset the debouncing timer
lastDebounceTime = millis();
}
// If the reading has been stable for longer than the debounce delay,
take it as the actual state
if ((millis() - lastDebounceTime) > debounceDelay) {
// If the button state has changed
if (reading != buttonState) {
buttonState = reading;
// Only act when the button is pressed (LOW because of
INPUT_PULLUP)
if (buttonState == LOW) {
animate(); // Show animation on both devices when button is
pressed
// Generate random numbers for each die
int diceValues[2];
for (int i = 0; i < 2; i++) {
diceValues[i] = random(1, 7); // Random number between 1 and 6
}
// Display die faces on each device
for (int i = 0; i < 2; i++) {
displayDieFace(dice[diceValues[i] - 1], i);
}
6
// Optional: delay after showing die face
// delay(500); // adjust delay as needed
}
}
}
// Save the reading for next time
lastButtonState = reading;
}

How the Code Works

Library Inclusion and Initialization

#include "LedControl.h"
LedControl lc = LedControl(12, 11, 10, 2);

● Includes the "LedControl" library to control the LED matrices.

● Initializes a "LedControl" object "lc" with data in ("DIN"), clock ("CLK"), and chip select ("CS") pins connected to pins 12, 11, and 10 of the Arduino, respectively.

● Specifies that there are 2 devices (LED matrices) daisy-chained together.


Defining Dice Face Patterns

byte dice[6][8] = { /* ... */ };

● Creates a 2D array "dice" that holds the bitmap patterns for the six faces of a die.

● Each face is an 8-byte array representing an 8x8 LED matrix pattern.


Button Setup

const int buttonPin = 2;
int buttonState;
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

● Sets up the pushbutton connected to digital pin 2.

● Initializes variables for button state and debounce functionality to handle mechanical switch bouncing


Setup Function

void setup() { // Initialize the MAX72XX devices
for (int i = 0; i < 2; i++) {
lc.shutdown(i, false);
lc.setIntensity(i, 8);
lc.clearDisplay(i); }
randomSeed(analogRead(0));
pinMode(buttonPin, INPUT_PULLUP);
buttonState = digitalRead(buttonPin);
lastButtonState = buttonState; }


● Wakes up both LED matrices from shutdown mode.

● Sets the brightness to a medium level.

● Clears any previous display on the matrices.

● Seeds the random number generator for generating random dice values.

● Configures the button pin with an internal pull-up resistor.

● Reads the initial state of the button.


Displaying Dice Faces

void displayDieFace(byte face[8], int device) { /* ... */
}


● A helper function that takes a dice face pattern and displays it on the specified LED matrix.


Animation Function

void animate() { /* ... */ }

● Creates a rolling dice animation by displaying random patterns on the LED matrices for one second.

● Enhances the user experience by simulating the rolling effect before showing the final result.


Main Loop

void loop() { // Read and debounce the button state
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis(); }
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
animate();
int diceValues[2];
for (int i = 0; i < 2; i++) {
diceValues[i] = random(1, 7); }
for (int i = 0; i < 2; i++) {
displayDieFace(dice[diceValues[i] - 1], i); }
} } }
9
lastButtonState = reading; }


● Continuously checks the state of the button and debounces it.

● When a button press is detected:

○ Calls the "animate()" function to display the rolling animation.

○ Generates two random numbers between 1 and 6 for the dice values.

○ Displays the corresponding dice face on each LED matrix using the "displayDieFace()" function

Main Loop

void loop() { // Read and debounce the button state
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis(); }
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
animate();
int diceValues[2];
for (int i = 0; i < 2; i++) {
diceValues[i] = random(1, 7); }
for (int i = 0; i < 2; i++) {
displayDieFace(dice[diceValues[i] - 1], i); }
} } }
9
lastButtonState = reading; }


● Continuously checks the state of the button and debounces it.

● When a button press is detected:

○ Calls the "animate()" function to display the rolling animation.

○ Generates two random numbers between 1 and 6 for the dice values.

○ Displays the corresponding dice face on each LED matrix using the "displayDieFace()" function