Digital Dice

This code is designed to simulate a rolling dice using a 74HC595 shift register and a 7-segment digital display. The dice roll simulation is activated by directly shaking the tilt switch. Upon this action, the digital display cycles through random numbers between 1 and 6, simulating the rolling of a dice. After a brief interval, the display stops, showing a random number that signifies the outcome of the dice roll.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Elite Explorer Kit

300+

Elite Explorer Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

Arduino Uno R4 WiFi

-

Breadboard

BUY

Jumper Wires

BUY

Resistor

BUY

Tilt Switch

-

74HC595

BUY

7-segment Display

BUY

Wiring

../_images/05_dice_bb.png

Schematic

../_images/05_digital_dice_schematic.png

Code

Note

  • You can open the file 05_digital_dice.ino under the path of elite-explorer-kit-main\fun_project\05_digital_dice directly.

  • Or copy this code into Arduino IDE.

How it works?

Here’s a detailed explanation of the code:

  1. Initialization of variables:

    dataPin, clockPin, latchPin: Pins for the 74HC595. buttonPin: The digital pin where the button is connected. numbers[]: An array to store the encoding representing numbers 1 through 6 on a common anode digital tube.

  2. Volatile variables:

    rolling: This is a volatile variable indicating whether the dice is currently rolling. It’s declared as volatile since it’s accessed both in the interrupt service routine and the main program.

  3. setup():

    Set the modes for the relevant pins. Set the input mode for the button using the internal pull-up resistor. Assign an interrupt to the button, which calls the rollDice function when the button’s state changes.

  4. loop():

    It checks if rolling is true. If it is, it continues to display a random number between 1 and 6. If the button has been pressed for more than 500 milliseconds, the rolling stops.

  5. rollDice():

    This is the interrupt service routine for the button. It checks if the button is pressed (low level). If it is, the current time is recorded and the rolling begins.

  6. displayNumber():

    This function displays a number on the digital tube. It sends the number to the digital tube through the 74HC595 shift register.