/* Electronic Dice By Victor Uribe All Rights Reserved This code is only to be distributed on instructables.com unless otherwise given permission */ int switchPin = 12; //Digital Pin for button or motion sensor int topLeft = 2; //Positive to Top Left LED from digital pin 2 int topRight = 4; //Positive to Top Right LED from digital pin 4 int middleLeft = 5; //Positive to Middle Left LED from digital pin 5 int center = 6; //Positive to Center LED from digital pin 6 int middleRight = 7; //Positive to MIddle Right LED from digital pin 7 int bottomLeft = 8; //Positive to Bottom Left LED from digital pin 8 int bottomRight = 10; //Positive to Bottom Right LED from digital pin 10 int val; //Variable to check if button is pressed int val2; //Constant that val checks against in order to tell //if the button is pressed int randshow; //Variable where random number is stored int lightMode; //Variable to check if the dice is on or off int buttonState; //Checks whether the button is switched on or off int numdelay = 500; //Delay between Scrolling numbers void setup() { pinMode(topLeft , OUTPUT); //Setup for the pins going to the LEDs pinMode(topRight , OUTPUT); pinMode(center , OUTPUT); pinMode(bottomLeft , OUTPUT); pinMode(bottomRight , OUTPUT); buttonState = digitalRead(switchPin); //Checks whether the switch is open or closed circuit Serial.begin(9600); //Sets up the serial port to communicate with the //computer } void loop() { val = digitalRead(switchPin); //Sets value of val depending if the button is open //or closed delay(10); //delays the program for 10milliseconds (mostly for //debouncing the button) val2 = digitalRead(switchPin); //Same as val if (val == val2) { //Checks the state of the button if (val != buttonState) { //If val (button changes from open to closed or vice //versa) changes then... if (val == LOW) { //If when the button is pressed the circuit is open if (lightMode == 0) { //If it is not currently running... //Show #1 //Turns on the center LED to indicate 1 on a dice digitalWrite(center, HIGH); delay(numdelay); //Again a delay for 500ms digitalWrite(center, LOW); //Turns on the Top Right led and Bottom Left to //indicate the number 2 //Show #2 digitalWrite(topRight, HIGH); digitalWrite(bottomLeft, HIGH); delay(numdelay); digitalWrite(topRight, LOW); digitalWrite(bottomLeft, LOW); //Show #3 digitalWrite(topLeft, HIGH); digitalWrite(bottomRight, HIGH); digitalWrite(center, HIGH); delay(numdelay); digitalWrite(topLeft, LOW); digitalWrite(bottomRight, LOW); digitalWrite(center, LOW); //Show #4 digitalWrite(topLeft, HIGH); digitalWrite(bottomRight, HIGH); digitalWrite(topRight, HIGH); digitalWrite(bottomLeft, HIGH); delay(numdelay); digitalWrite(topLeft, LOW); digitalWrite(bottomRight, LOW); digitalWrite(topRight, LOW); digitalWrite(bottomLeft, LOW); //Show #5 digitalWrite(topLeft, HIGH); digitalWrite(bottomRight, HIGH); digitalWrite(topRight, HIGH); digitalWrite(bottomLeft, HIGH); digitalWrite(center, HIGH); delay(numdelay); digitalWrite(topLeft, LOW); digitalWrite(bottomRight, LOW); digitalWrite(topRight, LOW); digitalWrite(bottomLeft, LOW); digitalWrite(center, LOW); //Show #6 digitalWrite(topLeft, HIGH); digitalWrite(topRight, HIGH); digitalWrite(middleLeft, HIGH); digitalWrite(middleRight, HIGH); digitalWrite(bottomLeft, HIGH); digitalWrite(bottomRight,HIGH); delay(numdelay); digitalWrite(topLeft, LOW); digitalWrite(topRight, LOW); digitalWrite(middleLeft, LOW); digitalWrite(middleRight, LOW); digitalWrite(bottomLeft, LOW); digitalWrite(bottomRight, LOW); //Pick random number from 0-6 and show number... randshow = random(1, 7); if (randshow == 1) { //Show #1 digitalWrite(center, HIGH); //if random number = 1 then it shows one (same for //all other numbers... delay(1500); //Delay that shows actual number digitalWrite(center, LOW); } if (randshow == 2) { //Show #2 digitalWrite(topRight, HIGH); digitalWrite(bottomLeft, HIGH); delay(1500); digitalWrite(topRight, LOW); digitalWrite(bottomLeft, LOW); } if (randshow == 3) { //Show #3 digitalWrite(topLeft, HIGH); digitalWrite(bottomRight, HIGH); digitalWrite(center, HIGH); delay(1500); digitalWrite(topLeft, LOW); digitalWrite(bottomRight, LOW); digitalWrite(center, LOW); } if (randshow == 4) { //Show #4 digitalWrite(topLeft, HIGH); digitalWrite(bottomRight, HIGH); digitalWrite(topRight, HIGH); digitalWrite(bottomLeft, HIGH); delay(1500); digitalWrite(topLeft, LOW); digitalWrite(bottomRight, LOW); digitalWrite(topRight, LOW); digitalWrite(bottomLeft, LOW); } if (randshow == 5) { //Show #5 digitalWrite(topLeft, HIGH); digitalWrite(bottomRight, HIGH); digitalWrite(topRight, HIGH); digitalWrite(bottomLeft, HIGH); digitalWrite(center, HIGH); delay(1500); digitalWrite(topLeft, LOW); digitalWrite(bottomRight, LOW); digitalWrite(topRight, LOW); digitalWrite(bottomLeft, LOW); digitalWrite(center, LOW); } if (randshow == 6) { //Show #6 digitalWrite(topLeft, HIGH); digitalWrite(topRight, HIGH); digitalWrite(middleLeft, HIGH); digitalWrite(middleRight, HIGH); digitalWrite(bottomLeft, HIGH); digitalWrite(bottomRight,HIGH); delay(1500); digitalWrite(topLeft, LOW); digitalWrite(topRight, LOW); digitalWrite(middleLeft, LOW); digitalWrite(middleRight, LOW); digitalWrite(bottomLeft, LOW); digitalWrite(bottomRight, LOW); } Serial.println(randshow); //If running on usb and the arduino software is //running with serail monitor then it will show //the number in the software lightMode = 1; //turns the program off } else { lightMode = 0; //turns the program on } } } buttonState = val; //Saves whether button is on or off } }