Week 4: Digital Dice

Challenge: Create a Digital Dice system. The arduino rolls a random number and then lights up the appropriate LEDs to replicate how that number appears on a dice roll.

Reflection: I designed this system similar to how Dr. B showed in his video. I placed 7 LEDs that I can control to show any of the possible dice rolls. Instead of having one long video this week I wanted to break it up into a bunch of short videos. Each video shows a different step or challenge that I encountered during this build. Overall I appreciated the openness of this project. The tutorials are nice, but being given a wide open challenge really tests our skills.

The first video is the longest. It does a short walkthrough of the code and the board.
Part 1: Setup and code

The second video depicts the first major problem that I encountered. If I held the button down too long the arduino would repeatedly roll, causing the lights to quickly flash between different states. 

Part 2: Potential Problem #1 Excessive Rolling

After playing around for a little bit I created a while loop that basically pauses the program after one roll. It waits for me to let go of the button before it can roll again.
Part 3: Fixing Excessive Rolling

This was the second issue I encountered. As I was keeping track of the dice rolls I never saw a number 6. Something must be wrong with my code, let's see how I fixed it. 
Part 4: Another issue arises_Where is Number 6? 

This final video is a walkthrough my code. It reiterates a lot of the above notions, but it is a screen capture (windows shortcut: Windows key + G). 
Code Walkthrough & Tutorial


Rolling Randomness. One of the biggest challenges for computers is their use as a random number generator. In general, it is quite difficult to tell a robot (which is following a specific line of code) to generate a random number. If anyone uses itunes you know that if you press shuffle it seems to play the same order of songs. That's because it 'randomly' picks the songs using a predetermined script. On the first look it appears random, but if you exam it enough you start to see the patterns.

I kept track of 100 dice rolls. In theory each number should be rolled 1/6 of the time, which correlates to a percentage of near 17%.

  1. 14%
  2. 17%
  3. 20%
  4. 18%
  5. 19%
  6. 12%



Rolls 1 and 6 are a little low, but this could be just part of random fluctuation. If we ran another thousand test we could examine just how fair our digital dice system is.


Copy of Code:

/*
  Travis Ray
  Week 4 Project
  Digital Dice

  Task Description: Create a program that randomly rolls a dice number. LEDs are then turned on to indicate that number visually.


  Pseudocode:
  If Button is pressed:
    Clear all LEDs
    Save a random number (1-6)
    Light up appropriate LEDs

*/

int myWait = 100;    //Wait time before you can press the button again.
const int LEDs[] = {12, 11, 10, 9, 8, 7, 6}; //Leds are plugged into digital 12 through 6
const int button1 = 2; //Button is plugged into digital 2


//Set up button and LEDs
void setup() {
  pinMode(button1, INPUT);  //Sets up the button as an input device

  int index;
  for (index = 0; index <= 6; index++)
  {
    pinMode(LEDs[index], OUTPUT);   //Sets up all LEDs as outputs
  }


  //Serial.begin(9600);   //Debugging Tool
}



void loop() {
  int buttonValue;                    //creates a variable to store the button value
  buttonValue = digitalRead(button1);  //stores value LOW = pressed, HIGH = No pressed

  if (buttonValue == LOW)               //If button is pressed, execute diceroll program
  {
    Diceroll();
  }

  while (buttonValue == LOW)
  {
    // Serial.print("Hello");    //Debugging to see if while loops works properly
    // Serial.print("\t");
    delay(100);
    buttonValue = digitalRead(button1);
  }
}


void Diceroll()
{
  clearLED();      //Clears all the LEDs to Low
  int mynum;
  mynum = random(1, 7); //Stores random number

  showLED(mynum);     //Turns lights on
}


//Turns all LEDs to LOW
void clearLED()
{
  int index = 0;
  for (index = 0; index <= 6; index++)
  {
    digitalWrite(LEDs[index], LOW);
  }
}



void showLED(int mynum)
{
  if (mynum == 1)                       //Rolls 1
  {
    digitalWrite(LEDs[3], HIGH);
  }
  else if (mynum == 2)                  //Rolls 2
  {
    digitalWrite(LEDs[2], HIGH);
    digitalWrite(LEDs[4], HIGH);
  }
  else if (mynum == 3)                  //Rolls 3
  {
    digitalWrite(LEDs[2], HIGH);
    digitalWrite(LEDs[3], HIGH);
    digitalWrite(LEDs[4], HIGH);
  }
  else if (mynum == 4)                  //Rolls 4
  {
    digitalWrite(LEDs[0], HIGH);
    digitalWrite(LEDs[2], HIGH);
    digitalWrite(LEDs[4], HIGH);
    digitalWrite(LEDs[6], HIGH);
  }
  else if (mynum == 5)                  //Rolls 5
  {
    digitalWrite(LEDs[0], HIGH);
    digitalWrite(LEDs[2], HIGH);
    digitalWrite(LEDs[3], HIGH);
    digitalWrite(LEDs[4], HIGH);
    digitalWrite(LEDs[6], HIGH);
  }
  else if (mynum == 6)                  //Rolls 6
  {
    digitalWrite(LEDs[0], HIGH);
    digitalWrite(LEDs[1], HIGH);
    digitalWrite(LEDs[2], HIGH);
    digitalWrite(LEDs[4], HIGH);
    digitalWrite(LEDs[5], HIGH);
    digitalWrite(LEDs[6], HIGH);
  }
}












Comments

Popular posts from this blog

Week 3: Times Square and Multiple Inputs