Step 6: Program Code

This is the code without the comments to reduce the space (You can find the comments in the downloadable file):

int pinLeds1 = 10;
int pinLeds2 = 9;
int pinLeds3 = 7;
int pinLed4 = 8;
int buttonPin = 6;
int buttonState;
long ran;
int time = 2000;

void setup ()
{
  pinMode (pinLeds1, OUTPUT);
  pinMode (pinLeds2, OUTPUT);
  pinMode (pinLeds3, OUTPUT);
  pinMode (pinLed4, OUTPUT);
  pinMode (buttonPin, INPUT);
  randomSeed(analogRead(0));
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH){
    ran = random(1, 7);
    if (ran == 1){
      digitalWrite (pinLed4, HIGH);
      delay (time);
    }
    if (ran == 2){
      digitalWrite (pinLeds1, HIGH);
      delay (time);
    }
    if (ran == 3){
      digitalWrite (pinLeds3, HIGH);
      digitalWrite (pinLed4, HIGH);
      delay (time);
    }
    if (ran == 4){
      digitalWrite (pinLeds1, HIGH);
      digitalWrite (pinLeds3, HIGH);
      delay (time);
    }
    if (ran == 5){
      digitalWrite (pinLeds1, HIGH);
      digitalWrite (pinLeds3, HIGH);
      digitalWrite (pinLed4, HIGH);
      delay (time);
   }
   if (ran == 6){
      digitalWrite (pinLeds1, HIGH);
      digitalWrite (pinLeds2, HIGH);
      digitalWrite (pinLeds3, HIGH);
      delay (time);
   }
  }
  digitalWrite (pinLeds1, LOW);
  digitalWrite (pinLeds2, LOW);
  digitalWrite (pinLeds3, LOW);
  digitalWrite (pinLed4, LOW);
}


 
Remove these adsRemove these ads by Signing Up
FinalTrigger says: Nov 23, 2011. 2:15 PM
Sorry for the noobish question, can someone explain to me whats the significance of :
randomSeed(analogRead(0));
and
ran = random(1, 7);

Much appreciated. By the way this is a fantastic project to learn with.
Davuzz11 (author) says: Dec 25, 2011. 10:25 AM
Sorry for the waiting!
" randomSeed(analogRead(0)); " is needed for have a correct randomness (without it, the arduino will use the same sequence every time).
" ran = random(1, 7); " set the variable "ran" equal to a number from 1 to 6, that is the result.
{Havoc}.Goliath says: Dec 19, 2011. 8:36 PM
With my limited programming experience im guessing "randomSeed(analogRead(0));" is preparing the program to create a random number and "ran = random(1,7);" is setting ran (a variable?) equal to a random number valued 1 through 7
kloptops says: Jan 28, 2011. 1:48 AM
Nice design, however i decided to make the display routine a bit more interesting with it showing a "random" sequence before displaying the final result.

Thanks for the great starter project.

http://dl.dropbox.com/u/7992054/arduino/Dice.pde
fwanderwrwr says: Sep 20, 2011. 1:48 AM
Great improvement! :)
Paul Smith says: Jan 16, 2010. 6:07 PM
How do you overcome the fact the the random function in the arduino always uses the same sequence of random numbers, thus making it predictable?
anand-patel says: Jun 15, 2011. 5:03 PM
ftp
Davuzz11 (author) says: Jan 17, 2010. 6:35 AM
You can overcome this fact simply putting in the Setup function this line code:
randomSeed(analogRead(0));

However, this line code is already present in the complete code of the instructables.
electronics4you says: Mar 27, 2010. 3:30 AM
Hi Everyone,
I built this project today and came across an error in the code. According to the circuit diagram, the pin configuration should be this:
int pinLeds3 = 7;
int pinLed4 = 8;

It's written the other way around in the downloadable pde of this instructable.
Hope this was helpful..!

Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!