3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Arduino Led Dice

Step 6Program 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 = 8;
int pinLed4 = 7;
int buttonPin = 6;
int buttonState;
int 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);
}


« Previous StepDownload PDFView All StepsNext Step »
7 comments
Nov 23, 2011. 2:15 PMFinalTrigger says:
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.
Dec 19, 2011. 8:36 PM{Havoc}.Goliath says:
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
Jan 28, 2011. 1:48 AMkloptops says:
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
Sep 20, 2011. 1:48 AMfwanderwrwr says:
Great improvement! :)
Jan 16, 2010. 6:07 PMPaul Smith says:
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?
Jun 15, 2011. 5:03 PManand-patel says:
ftp
Mar 27, 2010. 3:30 AMelectronics4you says:
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

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
9
Followers
1
Author:Davuzz11