// Arduino Electronic Dice - ozandamci.com
const int ledPins[7] = {2, 3, 4, 5, 6, 7, 8}; // A, B, C, D, E, F, G
const int buttonPin = 12;
// Which LEDs light up for each face of the dice (A B C D E F G)
const bool patterns[6][7] = {
{0, 0, 0, 1, 0, 0, 0}, // 1
{1, 0, 0, 0, 0, 0, 1}, // 2
{1, 0, 0, 1, 0, 0, 1}, // 3
{1, 1, 0, 0, 0, 1, 1}, // 4
{1, 1, 0, 1, 0, 1, 1}, // 5
{1, 1, 1, 0, 1, 1, 1} // 6
};
void setup() {
for (int i = 0; i < 7; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP);
randomSeed(analogRead(A0));
showDice(1);
}
void showDice(int number) {
for (int i = 0; i < 7; i++) {
digitalWrite(ledPins[i], patterns[number - 1][i]);
}
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
// The dice is rolling: random numbers that slow down
for (int wait = 30; wait < 300; wait += 25) {
showDice(random(1, 7));
delay(wait);
}
// Final number
int result = random(1, 7);
showDice(result);
// Wait for the button to be released
while (digitalRead(buttonPin) == LOW) {
}
delay(200);
}
}