Thank you for your comments Gentlemen!
Here is the code for my Led Dice project. As already mentioned in my first post, it is my first experience with the Arduino language. Corrections, comments, criticisms all welcomed! I am eager to learn more.
Thanks!!!
/*
Led Dice with Arduino
Tosses a pair of die when a button is pressed.
Created 14 June 2010
By Switchblade_46
based on an Arduino Led die project by Davuzz11 at http://www.instructables.com/id/Arduino-Led-Dice/
*/
const int pinLeds1 = 4; // assign group of leds1 to digital pin 4
const int pinLeds2 = 6; // assign group of leds2 to digital pin 6
const int pinLeds3 = 7; // assign group of leds3 to digital pin 7
const int pinLed4 = 5; // assign led4 to digital pin 5
const int buttonPin = 3; // assign push-button digital pin 3
const int pinLeds5 = 8; // assign group of leds5 to digital pin 8
const int pinLeds6 = 10; // assign group of leds6 to digital pin 10
const int pinLeds7 = 11; // assign group of leds7 to digital pin 11
const int pinLed8 = 9; // assign led8 to digital pin 9
int buttonState; // define pushbutton state
int ran1; // define random number for die 1
int ran2; // define random number for die 2
int time1 = 100; // define time1 interval as 0.1 seconds
int time2 = 200; // define time2 interval as 0.2 seconds
int time3 = 300; // define time2 interval as 0.3 seconds
// The setup() method runs once, when the sketch starts
void setup ()
{
pinMode (pinLeds1, OUTPUT); // declare pinLeds1 as output
pinMode (pinLeds2, OUTPUT); // declare pinLeds2 as output
pinMode (pinLeds3, OUTPUT); // declare pinLeds3 as output
pinMode (pinLed4, OUTPUT); // declare pinLed4 as output
pinMode (buttonPin, INPUT); // declare buttonPin as input
pinMode (pinLeds5, OUTPUT); // declare pinLeds5 as output
pinMode (pinLeds6, OUTPUT); // declare pinLeds6 as output
pinMode (pinLeds7, OUTPUT); // declare pinLeds7 as output
pinMode (pinLed8, OUTPUT); // declare pinLed8 as output
randomSeed(analogRead(0)); // to avoid arduino to follow any pattern
}
// the loop() method runs over and over again, tosses the dice until you are bored or you are finished with it.
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){ // checks state of pushbutton to start loop
ran1 = random(1, 7); // a number between 1 to 6 is randomized for die 1
ran2 = random(1, 7); // a number between 1 to 6 is randomized for die 2
//dice are displayed until the button is pressed once again. the following code sets all leds off
//otherwise the latest result of throw will still be displayed and the dice will not work.
digitalWrite (pinLeds1, LOW);
digitalWrite (pinLeds2, LOW);
digitalWrite (pinLeds3, LOW);
digitalWrite (pinLed4, LOW);
digitalWrite (pinLeds5, LOW);
digitalWrite (pinLeds6, LOW);
digitalWrite (pinLeds7, LOW);
digitalWrite (pinLed8, LOW);
delay (time3);
//now both dice are rolling
digitalWrite (pinLed4, HIGH);
digitalWrite (pinLed8, HIGH);
delay (time1);
digitalWrite (pinLed4, LOW);
digitalWrite (pinLed8, LOW);
delay (time1);
digitalWrite (pinLed4, HIGH);
digitalWrite (pinLed8, HIGH);
digitalWrite (pinLeds3, HIGH);
digitalWrite (pinLeds7, HIGH);
delay (time1);
digitalWrite (pinLed4, LOW);
digitalWrite (pinLed8, LOW);
digitalWrite (pinLeds3, LOW);
digitalWrite (pinLeds7, LOW);
delay (time1);
digitalWrite (pinLed4, HIGH);
digitalWrite (pinLed8, HIGH);
digitalWrite (pinLeds2, HIGH);
digitalWrite (pinLeds6, HIGH);
delay (time1);
digitalWrite (pinLed4, LOW);
digitalWrite (pinLed8, LOW);
digitalWrite (pinLeds2, LOW);
digitalWrite (pinLeds6, LOW);
delay (time1);
digitalWrite (pinLed4, HIGH);
digitalWrite (pinLed8, HIGH);
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds5, HIGH);
delay (time1);
digitalWrite (pinLed4, LOW);
digitalWrite (pinLed8, LOW);
digitalWrite (pinLeds1, LOW);
digitalWrite (pinLeds5, LOW);
delay (time1);
//they are rolling once more
digitalWrite (pinLed4, HIGH);
digitalWrite (pinLed8, HIGH);
delay (time1);
digitalWrite (pinLed4, LOW);
digitalWrite (pinLed8, LOW);
delay (time1);
digitalWrite (pinLed4, HIGH);
digitalWrite (pinLed8, HIGH);
digitalWrite (pinLeds3, HIGH);
digitalWrite (pinLeds7, HIGH);
delay (time1);
digitalWrite (pinLed4, LOW);
digitalWrite (pinLed8, LOW);
digitalWrite (pinLeds3, LOW);
digitalWrite (pinLeds7, LOW);
delay (time1);
digitalWrite (pinLed4, HIGH);
digitalWrite (pinLed8, HIGH);
digitalWrite (pinLeds2, HIGH);
digitalWrite (pinLeds6, HIGH);
delay (time1);
digitalWrite (pinLed4, LOW);
digitalWrite (pinLed8, LOW);
digitalWrite (pinLeds2, LOW);
digitalWrite (pinLeds6, LOW);
delay (time1);
digitalWrite (pinLed4, HIGH);
digitalWrite (pinLed8, HIGH);
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds5, HIGH);
delay (time1);
digitalWrite (pinLed4, LOW);
digitalWrite (pinLed8, LOW);
digitalWrite (pinLeds1, LOW);
digitalWrite (pinLeds5, LOW);
delay (time1);
//sets leds off on the first die just for viewing pleasure. It only delays for 0.1 secs before the last rolling of the second die.
digitalWrite (pinLeds5, LOW);
digitalWrite (pinLeds6, LOW);
digitalWrite (pinLeds7, LOW);
digitalWrite (pinLed8, LOW);
delay (time1);
// now arduino will toss the first die
if (ran1 == 1){
digitalWrite (pinLed8, HIGH); // turns on led8 if ran1 equals to 1
}
if (ran1 == 2){
digitalWrite (pinLeds5, HIGH); // turns on leds5 if ran1 equals to 2
}
if (ran1 == 3){
digitalWrite (pinLeds7, HIGH);
digitalWrite (pinLed8, HIGH); // turns on leds7 and led8 if ran1 equals to 3
}
if (ran1 == 4){
digitalWrite (pinLeds5, HIGH);
digitalWrite (pinLeds7, HIGH); // turns on leds5 and leds7 if ran1 equals to 4
}
if (ran1 == 5){
digitalWrite (pinLeds5, HIGH);
digitalWrite (pinLeds7, HIGH);
digitalWrite (pinLed8, HIGH); // turns on leds5 and leds7 and led8 if ran1 equals to 5
}
if (ran1 == 6){
digitalWrite (pinLeds5, HIGH);
digitalWrite (pinLeds6, HIGH);
digitalWrite (pinLeds7, HIGH); // turns on leds5 and leds6 and leds7 if ran1 equals to 6
}
//second die still rolling as most of time dice do not stop at the same time
digitalWrite (pinLed4, HIGH);
delay (time1);
digitalWrite (pinLed4, LOW);
delay (time1);
digitalWrite (pinLed4, HIGH);
digitalWrite (pinLeds3, HIGH);
delay (time1);
digitalWrite (pinLed4, LOW);
digitalWrite (pinLeds3, LOW);
delay (time1);
digitalWrite (pinLed4, HIGH);
digitalWrite (pinLeds2, HIGH);
delay (time1);
digitalWrite (pinLed4, LOW);
digitalWrite (pinLeds2, LOW);
delay (time1);
digitalWrite (pinLed4, HIGH);
digitalWrite (pinLeds1, HIGH);
delay (time1);
digitalWrite (pinLed4, LOW);
digitalWrite (pinLeds1, LOW);
delay (time1);
//sets leds off on the second die just for viewing pleasure. This time it puts a delay of 0.2 seconds to increase excitement.
digitalWrite (pinLeds1, LOW);
digitalWrite (pinLeds2, LOW);
digitalWrite (pinLeds3, LOW);
digitalWrite (pinLed4, LOW);
delay (time2);
// now arduino will toss the second die
if (ran2 == 1){
digitalWrite (pinLed4, HIGH); // turns on led4 if ran2 equals to 1
}
if (ran2 == 2){
digitalWrite (pinLeds1, HIGH); // turns on leds1 if ran2 equals to 2
}
if (ran2 == 3){
digitalWrite (pinLeds3, HIGH);
digitalWrite (pinLed4, HIGH); // turns on leds3 and led4 if ran2 equals to 3
}
if (ran2 == 4){
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds3, HIGH); // turns on leds1 and leds3 if ran2 equals to 4
}
if (ran2 == 5){
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds3, HIGH);
digitalWrite (pinLed4, HIGH); // turns on leds1 and leds3 and led4 if ran2 equals to 5
}
if (ran2 == 6){
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds2, HIGH);
digitalWrite (pinLeds3, HIGH); // turns on leds1 and leds2 and leds3 if ran2 equals to 6
}
}
}