In this article, we will talk about an easy Arduino project that anyone can do, the Electronic Dice Making project. You can bring this project to life at a very low cost, and the materials for this project are quite minimal. If you're tired of seeing the same projects while trying to improve your Arduino skills, this unconventional project is just for you. :)
There are many projects related to turning on and off LEDs. However, most of these projects don't have a purpose... In this project, we added meaning to the blinking of the LEDs. In our project, 6 LEDs arranged on a breadboard are lit with the help of a push button. A random LED between 1-6 lights up simultaneously, thus digitizing the dice rolling process. Let's move on to the list of materials and how to make the project without further ado.
Materials List
That's all there is to our materials list. Just click on their names to access the purchase pages of the mentioned products. It's possible to customize the materials according to your preferences. You can use a different color LED instead of red, or choose LEDs in different colors to use together.
Now let's see how to connect these materials to each other.
Electronic Dice Making Connection Diagram
We will make our connections by adhering to the connection diagram above. Since the Arduino codes shared below are compatible with this connection diagram, it is important to make connections using these pins. Otherwise, these codes will not work.
Arduino Codes
#define DEBUG 0
// 6 consecutive digital pins for the LEDs
int first = 2;
int second = 3;
int third = 4;
int fourth = 5;
int fifth = 6;
int sixth = 7;
// pin for the button switch
int button = 12;
// value to check state of button switch
int pressed = 0;
void setup() {
// set all LED pins to OUTPUT
for (int i = first; i <= sixth; i++) {
pinMode(i, OUTPUT);
}
// set button pin to INPUT
pinMode(button, INPUT);
// initialize random seed by noise from analog pin 0 (should be unconnected)
randomSeed(analogRead(0));
// if we're debugging, connect to serial
#ifdef DEBUG
Serial.begin(9600);
#endif
}
void buildUpTension() {
// light LEDs from left to right and back to build up tension
// while waiting for the dice to be thrown
// left to right
for (int i = first; i <= sixth; i++) {
if (i != first) {
digitalWrite(i - 1, LOW);
}
digitalWrite(i, HIGH);
delay(100);
}
// right to left
for (int i = sixth; i >= first; i--) {
if (i != sixth) {
digitalWrite(i + 1, LOW);
}
digitalWrite(i, HIGH);
delay(100);
}
}
void showNumber(int number) {
digitalWrite(first, HIGH);
if (number >= 2) {
digitalWrite(second, HIGH);
}
if (number >= 3) {
digitalWrite(third, HIGH);
}
if (number >= 4) {
digitalWrite(fourth, HIGH);
}
if (number >= 5) {
digitalWrite(fifth, HIGH);
}
if (number == 6) {
digitalWrite(sixth, HIGH);
}
}
int throwDice() {
// get a random number in the range [1,6]
int randNumber = random(1, 7);
#ifdef DEBUG
Serial.println(randNumber);
#endif
return randNumber;
}
void setAllLEDs(int value) {
for (int i = first; i <= sixth; i++) {
digitalWrite(i, value);
}
}
void loop() {
// if button is pressed - throw the dice
pressed = digitalRead(button);
if (pressed == HIGH) {
// remove previous number
setAllLEDs(LOW);
buildUpTension();
int thrownNumber = throwDice();
showNumber(thrownNumber);
}
}
By trying to understand the basic logic of this code, you can better grasp the working logic of Arduino codes. In this project formalized by using if statements based on the number randomly obtained, a certain number of LEDs are instructed to light up. Thus, we have an electronic dice. When the button is pressed, it lights up one of the LEDs between 1-6, giving us the result of the dice.
We've come to the end of our Electronic Dice Making project. Stay tuned to Motorobit to discover such innovative projects before anyone else!