Electric Dice (Led)
We all are familiar with dice and often played LUDO or Snake & Ladders game by using dice. Dice is a cube that contains 6 different numbers from 1 to 6 on all of its sides. We throw dice on a surface to get a random number while playing the games. In this project, we have tried to replicate it with a digital dice using an Arduino Uno board. In place of throwing the dice, here we need to press a button to get a random number between 0 to 6.
Supplies
1. Arduino UNO
2. LED's*6
3. Pushbuttons *2
4. Connecting wires
5. Breadboard
6. 1 k & 220 ohms resistor
7. Power supply 8. Buzzer 9. Paper 10. Tape 11. Computer
Circuit Diagram
At the beginning of the program, I/O pins 2 to 9 are configured as digital outputs, and pins 9 & 12 are configured as a digital inputs with an internal pull-up resistor. The program waits until the button is pressed. During the button being hold pressed, the program executes in a loop continuously and increments a variable (named i) between 1 and 6. In every loop, the current number of the variable is sent to the LEDs, and this way the dice rolls. At the time when the button is released, the dice stops rolling and the last value of the variable is displayed until the next button – press. The rolling speed is dependent on a delay command, used at the end of the loop.
The Code
Code:
file:///C:/Users/ASUS/Downloads/Electronic_dice.ino
int i = 0;
int button = 9; int button2 = 12; int buzzer = 10; int d = 50; int h; void setup() { Serial.begin(9600); pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); pinMode(buzzer, OUTPUT); pinMode(button, INPUT); pinMode(button2, INPUT); //for(int i=0;i<7;i++)
} void one() { digitalWrite(8, HIGH); digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, LOW); digitalWrite(7, LOW); delay(d);
} void two() { digitalWrite(2, HIGH); digitalWrite(7, HIGH);
digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, LOW);
digitalWrite(8, LOW); delay(d); } void three() { digitalWrite(2, HIGH); digitalWrite(8, HIGH); digitalWrite(7, HIGH);
digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, LOW); delay(d); } void four() { digitalWrite(2, HIGH); digitalWrite(4, HIGH); digitalWrite(5, HIGH); digitalWrite(7, HIGH);
digitalWrite(3, LOW);
digitalWrite(6, LOW);
digitalWrite(8, LOW); delay(d); } void five() { digitalWrite(2, HIGH); digitalWrite(4, HIGH); digitalWrite(5, HIGH); digitalWrite(7, HIGH); digitalWrite(8, HIGH);
digitalWrite(3, LOW);
digitalWrite(6, LOW); delay(d);
} void six() { digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, HIGH); digitalWrite(5, HIGH); digitalWrite(7, HIGH); digitalWrite(6, HIGH);
digitalWrite(8, LOW); delay(d); }
void loop() { int a = digitalRead(button); int b = digitalRead(button2); Serial.println(b); if (a == 1) { i = random(1, 7); delay(20); //Serial.println(i); for (h = 0; h < 3000; h += 180) { tone(buzzer, h); delay(30); } noTone(buzzer); }