The COVID - 19 lock-down is really a bummer for us all so, while the world is digitalizing why can't the old fashioned dice do too? So here we go..
This project works with a push of a button (Literally XD). It uses a random generator to generate a random number from 1 to 6.
Hardware Setup and Initialization
This DIY Electronic Dice uses six LEDs representing the faces of a standard die. Each LED is connected to a specific digital pin (2 through 7) on the Arduino Uno, while a momentary push button is used as the trigger on pin 11.
Programming the Randomness
The core of the logic resides in the random(min, max) function. In Arduino, random(1, 7) generates a pseudo-random integer between 1 and 6. To ensure the sequence isn't predictable after a reset, it is best practice to seed the generator using randomSeed(analogRead(0)) if you have an unconnected analog pin.
Code Structure
We define the pins and track the button's state:
int ledPins[] = {2, 3, 4, 5, 6, 7};
int button = 11;
int buttonState = 0;
Game Flow
- Input Detection: The Arduino constantly monitors the button state using
digitalRead(button). - Roll Trigger: If the button is pressed (
buttonState == 1), the code calls the random function. - LED Output: Depending on the resulting number (1-6), specific LEDs are turned
HIGH. For example, a result of '3' turns on three LEDs while keeping the othersLOW. - Display Lock: A
delay(10000)is implemented to keep the result visible before resetting for the next turn.
Creative Customization
To make the dice more authentic, you can arrange the LEDs in a traditional 3x3 grid layout (using 7 LEDs total to include the center dot). You can also add a "rolling" animation where the LEDs blink rapidly for a few seconds before settling on the final result, adding to the suspense of the game!