Does God play dice?

So for this weeks challenge we were told we had to make a die, a digital die.  This was a cool project. At first I just used a line of LEDs to show what number that you had but then I thought I should probably make it look like a real die so I thought I would rewire it and make it look like a real die.  So here it is.

So that was my first attempt the code for that one looked like this...

int pinLed1 = 12;
int pinLed2 = 11;
int pinLed3 = 10;
int pinLed4 = 9;
int pinLed5 = 8;
int pinLed6 = 7;
int buttonPin = 2;
int buttonState = 0;
long ran;
int time = 1000;

void setup ()
{
  pinMode (pinLed1, OUTPUT);
  pinMode (pinLed2, OUTPUT);
  pinMode (pinLed3, OUTPUT);
  pinMode (pinLed4, OUTPUT);
  pinMode (pinLed5, OUTPUT);
  pinMode (pinLed6, OUTPUT);
  pinMode (buttonPin, INPUT);
  randomSeed(analogRead(0));
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH){
    ran = random(1, 7);
    if (ran == 1){
      digitalWrite (pinLed1, HIGH);
      delay (time);
    }
    if (ran == 2){
      digitalWrite (pinLed1, HIGH);
      digitalWrite (pinLed2, HIGH);
      delay (time);
    }
    if (ran == 3){
      digitalWrite (pinLed1, HIGH);
      digitalWrite (pinLed2, HIGH);
      digitalWrite (pinLed3, HIGH);
      delay (time);
    }
    if (ran == 4){
      digitalWrite (pinLed1, HIGH);
      digitalWrite (pinLed2, HIGH);
      digitalWrite (pinLed3, HIGH);
      digitalWrite (pinLed4, HIGH);
      delay (time);
    }
    if (ran == 5){
      digitalWrite (pinLed1, HIGH);
      digitalWrite (pinLed2, HIGH);
      digitalWrite (pinLed3, HIGH);
      digitalWrite (pinLed4, HIGH);
      digitalWrite (pinLed5, HIGH);
      delay (time);
   }
   if (ran == 6){
      digitalWrite (pinLed1, HIGH);
      digitalWrite (pinLed2, HIGH);
      digitalWrite (pinLed3, HIGH);
      digitalWrite (pinLed4, HIGH);
      digitalWrite (pinLed5, HIGH);
      digitalWrite (pinLed6, HIGH);
      delay (time);
   }
  }
  digitalWrite (pinLed1, LOW);
  digitalWrite (pinLed2, LOW);
  digitalWrite (pinLed3, LOW);
  digitalWrite (pinLed4, LOW);
  digitalWrite (pinLed5, LOW);
  digitalWrite (pinLed6, LOW);
  digitalWrite (pinLed7, LOW);
}

It was pretty simple just every time that you clicked the button down it showed a number of LEDs and that would be the number that the die had.THis
I thought that this was a little too hard to instantly tell what number it was it is also hard to see because the LED were so close together.  So I decided to spread them out a little bit and make it look more like real dice.  So here is the second attempt...


Here is the code for that one it is harder to follow....

int pinLed1 = 12;
int pinLed2 = 11;
int pinLed3 = 10;
int pinLed4 = 9;
int pinLed5 = 8;
int pinLed6 = 7;
int pinLed7 = 6;
int buttonPin = 2;
int buttonState = 0;
long ran;
int time = 1000;

void setup ()
{
  pinMode (pinLed1, OUTPUT);
  pinMode (pinLed2, OUTPUT);
  pinMode (pinLed3, OUTPUT);
  pinMode (pinLed4, OUTPUT);
  pinMode (pinLed5, OUTPUT);
  pinMode (pinLed6, OUTPUT);
  pinMode (pinLed7, 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 (pinLed7, HIGH);
      digitalWrite (pinLed2, HIGH);
      delay (time);
    }
    if (ran == 3){
      digitalWrite (pinLed5, HIGH);
      digitalWrite (pinLed4, HIGH);
      digitalWrite (pinLed3, HIGH);
      delay (time);
    }
    if (ran == 4){
      digitalWrite (pinLed6, HIGH);
      digitalWrite (pinLed5, HIGH);
      digitalWrite (pinLed3, HIGH);
      digitalWrite (pinLed1, HIGH);
      delay (time);
    }
    if (ran == 5){
      digitalWrite (pinLed6, HIGH);
      digitalWrite (pinLed5, HIGH);
      digitalWrite (pinLed4, HIGH);
      digitalWrite (pinLed3, HIGH);
      digitalWrite (pinLed1, HIGH);
      delay (time);
   }
   if (ran == 6){
      digitalWrite (pinLed6, HIGH);
      digitalWrite (pinLed7, HIGH);
      digitalWrite (pinLed5, HIGH);
      digitalWrite (pinLed3, HIGH);
      digitalWrite (pinLed2, HIGH);
      digitalWrite (pinLed1, HIGH);
      delay (time);
   }
  }
  digitalWrite (pinLed1, LOW);
  digitalWrite (pinLed2, LOW);
  digitalWrite (pinLed3, LOW);
  digitalWrite (pinLed4, LOW);
  digitalWrite (pinLed5, LOW);
  digitalWrite (pinLed6, LOW);
  digitalWrite (pinLed7, LOW);
}

This picture might help follow it better. 

This looked much better and I was pretty happy with the results.  I didn't have too much trouble with this one either but I did have a hard time at first getting the button to work.  I finally realized that I had it wired wrong I had the two wires in the 3.3 V and the 5 V slots on the Arduino which is just one slot over form the ground and the 5 V like it is suppose to be so it took me a few minutes to figure out the problem but after rewiring that it worked like a gem.  I really liked this project and I learned quite a bit.  I think you could probably find this type of program in some type of game or slot machine thing.  I don't know if I have ever seen this type directly but I am sure something similar to this in many different digital games. Oh and here is the histogram of my rolls.


This seems pretty even between split between the rolls I did 125 rolls and this is what I got.  I imagine that this is not really truly random because it is really hard to produce truly random numbers but it seems pretty good.  Anyway I enjoyed this project and look forward to the next. 





Comments

Popular posts from this blog

What is social bookmarking?

Motor with sonar

Arduino Analog input for a digital output