Electric Dice Using Led and an Arduino Board
by mgrashbin9000 in Circuits > Arduino
274 Views, 0 Favorites, 0 Comments
Electric Dice Using Led and an Arduino Board
This is a simple project where we will use basic electric components to make a custom electric dice. The dice will light up the led to display random numbers. For a simple simulation of this project using wokwi copy the link https://wokwi.com/projects/353649248291225601
Supplies
- Arduino board (Arduino Uno or similar)
- Breadboard and jumper wires
- 7 LEDs (any color of your choice)
- 7 x 220-ohm resistors (or higher, depending on the LED specifications)
- Push-button
Wiring
- Connect the positive (anode) leg of each LED to a separate digital pin(from D2 to D8) on the Arduino through its corresponding 220-ohm resistor.
- Connect the middle led to 5, top left led to 2, top right led to 3, middle left led to 4, middle right led to 6, bottom left led to 7 and bottom right led to 8.
- Connect the negative (cathode) leg of all LEDs to the common ground (GND) on the breadboard.
- Connect one terminal of the push-button switch to A0 pin on the Arduino, and the other terminal to the common ground (GND).
Coding
const int buttonPin = A0;// button pin
const int LEDpins[] = {2, 3, 4, 5, 6, 7, 8};// led pins
void setup() {
pinMode(buttonPin, INPUT_PULLUP);// declaring button pin as input
for (int i = 0; i < 7; i++) {
pinMode(LEDpins[i], OUTPUT);//declaring led pins as output
}
for (int i = 0; i < 7; i++)
{
digitalWrite(LEDpins[i], LOW);// turing off all the leds
}
}
bool firstTime = true;
void displayNumber(byte number) { // function for displaying numbers
digitalWrite(2, number > 1 ? HIGH : LOW);
digitalWrite(3, number > 3 ? HIGH : LOW);
digitalWrite(4, number == 6 ? HIGH : LOW);
digitalWrite(5, number % 2 == 1 ? HIGH : LOW);
digitalWrite(6, number == 6 ? HIGH : LOW);
digitalWrite(7, number > 3 ? HIGH : LOW);
digitalWrite(8, number > 1 ? HIGH : LOW);
}
void loop() {
while (digitalRead(A0) == HIGH) {
// do nothing
}
if (firstTime) {
randomSeed(micros());// to increase randomness
firstTime = false;
}
for (byte i = 0; i < 10; i++) {
int num = random(1, 7);//gives a random number from 1 to 6
displayNumber(num);
delay(50 + 20 * i);
}
}
Conclusion
Congratulations! You have successfully created your own electrical dice using LEDs. This fun and simple project is a great way to learn about basic electronics, coding, and creating interactive circuits. You can further customize your electrical dice by adding more LEDs, changing LED colors, or even creating different animations for each number. Have fun experimenting and exploring .