Tinkercad Lesson Notes - Class VII
A Digital Dice is an electronic version of a traditional dice. Instead of rolling a cube, an Arduino generates a random number from 1 to 6 and displays the result using LEDs.
| Component | Arduino Pin |
|---|---|
| LED 1 | D2 |
| LED 2 | D3 |
| LED 3 | D4 |
| LED 4 | D5 |
| LED 5 | D6 |
| LED 6 | D7 |
| Push Button | D8 and GND |
Each LED must be connected through a 220 Ω resistor to prevent damage from excessive current.
int leds[] = {2,3,4,5,6,7};
int button = 8;
void setup()
{
for(int i=0;i<6;i++)
pinMode(leds[i], OUTPUT);
pinMode(button, INPUT_PULLUP);
randomSeed(analogRead(A0));
}
void loop()
{
if(digitalRead(button)==LOW)
{
int dice = random(1,7);
for(int i=0;i<6;i++)
digitalWrite(leds[i], LOW);
digitalWrite(leds[dice-1], HIGH);
delay(1000);
}
}