LED Electronic Dice Simulator with Arduino
The LED Dice Simulator is an engaging Arduino project that replicates the rolling of a six-sided die using LEDs. It involves an Arduino board connected to buttons and LEDs, creating a simple and interactive dice simulation.
Each LED corresponds to a number on the die, and when a button is pressed, a random LED lights up, mimicking the result of rolling a physical die.
The project enhances both coding and hardware skills, making it suitable for beginners and educational purposes.
Component List:
Name | Quantity | Component |
---|---|---|
U1 | 1 | Arduino Uno R3 |
D1 D2 D3 D4 D5 D6 D7 | 7 | Blue LED |
R1 R2 R3 R4 R5 R6 R7 | 7 | 220 Ω Resistor |
S1 | 1 | Pushbutton |
R8 | 1 | 10 kΩ Resistor |
The circuit consists of an Arduino board connected to eight LEDs (representing the six die faces), and three buttons (simulate the rolling action and initiate the LED display).
Each LED is connected to a digital pin, and the buttons are connected to input pins. This uncomplicated setup allows for easy replication and understanding.
Arduino Code:
int button = 2;
int bottomLeft = 3;
int middleLeft = 4;
int upperLeft = 5;
int middle = 6;
int bottomRight = 9;
int middleRight = 8;
int upperRight = 7;
int state = 0;
long randNumber;
void setup(){
pinMode(bottomLeft, OUTPUT);
pinMode(middleLeft, OUTPUT);
pinMode(upperLeft, OUTPUT);
pinMode(middle, OUTPUT);
pinMode(bottomRight, OUTPUT);
pinMode(middleRight, OUTPUT);
pinMode(upperRight, OUTPUT);
pinMode(button, INPUT);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop(){
if (digitalRead(button) == HIGH && state == 0){ state = 1; randNumber = random(1, 7); delay(100); Serial.println(randNumber); if (randNumber == 6){ six(); } if (randNumber == 5){ five(); } if (randNumber == 4){ four(); } if (randNumber == 3){ three(); } if (randNumber == 2){ two(); } if (randNumber == 1){ one(); } delay(1000); clearAll(); state = 0; }
}
void six(){
digitalWrite(bottomLeft, HIGH);
digitalWrite(middleLeft, HIGH);
digitalWrite(upperLeft, HIGH);
digitalWrite(bottomRight, HIGH);
digitalWrite(middleRight, HIGH);
digitalWrite(upperRight, HIGH);
}
void five(){
digitalWrite(upperLeft, HIGH);
digitalWrite(bottomLeft, HIGH);
digitalWrite(middle, HIGH);
digitalWrite(upperRight, HIGH);
digitalWrite(bottomRight, HIGH);
}
void four(){
digitalWrite(upperLeft, HIGH);
digitalWrite(bottomLeft, HIGH);
digitalWrite(upperRight, HIGH);
digitalWrite(bottomRight, HIGH);
}
void three(){
digitalWrite(upperLeft, HIGH);
digitalWrite(middle, HIGH);
digitalWrite(bottomRight, HIGH);
}
void two(){
digitalWrite(bottomLeft, HIGH);
digitalWrite(upperRight, HIGH);
}
void one(){
digitalWrite(middle, HIGH);
}
void clearAll(){
digitalWrite(bottomLeft, LOW);
digitalWrite(middleLeft, LOW);
digitalWrite(upperLeft, LOW);
digitalWrite(middle,LOW);
digitalWrite(bottomRight, LOW);
digitalWrite(middleRight, LOW);
digitalWrite(upperRight, LOW);
}
The LED Dice Simulator is a fun and educational project for Arduino enthusiasts. It combines programming logic to generate random outcomes with a simple circuit setup, making it an excellent introduction to both software and hardware aspects of Arduino projects. This engaging simulation serves as a hands-on tool for learning about digital inputs, outputs, and the basics of coding, making it a valuable resource for beginners entering the world of Arduino programming.