Sunday, November 29, 2015

Week 4 - Adventures in Making - Electronic Dice Machine

This week's challenge was the Electronic Dice Machine that would generate a random number using 7 LED's  to simulate the possible combinations that you may get when you roll the dice.   I found the project to be fun and challenging.  I want to give a shout out to Nichole Hahn for trying to help me understand the code.  Interestingly enough, I understand the code and was able to writing and find some additional code pieces that I was able to use to construct a automatic random number generator that works just fine.  However, I can't seem to get my button to work for manual number generation.  This is something I will work on later.  Check out my video of  my automatic random number generator and my manual number generator that I am still working on. 

 
Here is the chart from my random number generator:
 
Here is the code for my automatic electronic dice machine:
 
 //Pin to to turn dice on & off
int button = 2;

//LED for DICE
int bottomLeft = 7;
int bottommiddleLeft = 8;
int uppermiddleLeft = 9;
int upperLeft = 10;
int bottomRight = 11;
int middleRight = 12;
int upperRight = 13;
int BUTTON;
int val =0;
int state = 0;
long randNumber;

//Initial setup
void setup()
  {
  pinMode(bottomLeft, OUTPUT);
  pinMode(bottommiddleLeft, OUTPUT);
  pinMode(uppermiddleLeft, OUTPUT);
  pinMode(upperLeft, OUTPUT);
  pinMode(bottomRight, OUTPUT);
  pinMode(middleRight, OUTPUT);
  pinMode(upperRight, OUTPUT);
 
  pinMode(button, INPUT);
 
  Serial.begin(9600);
  randomSeed(analogRead(0));

**This loop allows it to continuously generate a new number combination

}
 void loop(){
  if(digitalRead(button) == HIGH && state == 0){
  //if(val == HIGH) && (state == 0)
  state = 1;
  randNumber = random(1, 7);
  delay(50);
  Serial.println(randNumber);
  if (randNumber == 6){
   six();
  }
  if (randNumber == 5){
   five();
  }
  if (randNumber == 4){
   four();
  }
  if (randNumber == 3){
   three();
  }
  if (randNumber == 2){
   two();
  }
  if (randNumber == 1){
   one();
  }
  delay(1000);
  clearAll();
  state = 0;
 }
}
//Creates a function for each  sides of the die
 void six()
{
  digitalWrite(bottomLeft, OUTPUT);
  digitalWrite(bottommiddleLeft, OUTPUT);
  digitalWrite(uppermiddleLeft, OUTPUT);
  digitalWrite(upperLeft, OUTPUT);
  digitalWrite(bottomRight, OUTPUT);
  digitalWrite(middleRight, OUTPUT);
  digitalWrite(upperRight, OUTPUT);
}
void five()
{
 digitalWrite(upperLeft, HIGH);
 digitalWrite(bottomLeft, HIGH);
 digitalWrite(uppermiddleLeft, HIGH);
 digitalWrite(upperRight, HIGH);
 digitalWrite(bottomRight, HIGH);
}
void four()
{
 digitalWrite(upperLeft, HIGH);
 digitalWrite(bottomLeft, HIGH);
 digitalWrite(upperRight, HIGH);
 digitalWrite(bottomRight, HIGH);
}
void three()
{
 digitalWrite(upperLeft, HIGH);
 digitalWrite(uppermiddleLeft, HIGH);
 digitalWrite(bottomRight, HIGH);
}
void two()
{
 digitalWrite(bottomRight, HIGH);
 digitalWrite(upperLeft, HIGH);
}
void one(){
 digitalWrite(uppermiddleLeft, HIGH);
}
void clearAll(){
 digitalWrite(bottomLeft, LOW);
  digitalWrite(bottommiddleLeft, LOW);
  digitalWrite(uppermiddleLeft, LOW);
  digitalWrite(upperLeft, LOW);
  digitalWrite(bottomRight, LOW);
  digitalWrite(middleRight, LOW);
  digitalWrite(upperRight, LOW);
}
 
Here is the code from my manual electronic dice machine that I am still working on:
 
//Pin to turn dice on & off
int button = 2;
 
//LED for DICE
int bottomLeft = 7;
int bottommiddleLeft = 8;
int uppermiddleLeft = 9;
int upperLeft = 10;
int bottomRight = 11;
int middleRight = 12;
int upperRight = 13;

int val = 0;
int state = 0;
long randNumber;

//Initial setup
void setup()
  {
  pinMode(bottomLeft, OUTPUT);
  pinMode(bottommiddleLeft, OUTPUT);
  pinMode(uppermiddleLeft, OUTPUT);
  pinMode(upperLeft, OUTPUT);
  pinMode(bottomRight, OUTPUT);
  pinMode(middleRight, OUTPUT);
  pinMode(upperRight, OUTPUT);
 
  pinMode(button, INPUT);
 
  Serial.begin(9600);
  //randomSeed(analogRead(0));
}
void loop(){
  int val;
  int buttonState;
  buttonState = digitalRead(button);
  if (buttonState == HIGH)
  
   if (val == HIGH){
       digitalWrite(bottomLeft, LOW);
       digitalWrite(bottommiddleLeft, LOW);
       digitalWrite(uppermiddleLeft, LOW);
       digitalWrite(upperLeft, LOW);
       digitalWrite(bottomRight, LOW);
       digitalWrite(middleRight, LOW);
       digitalWrite(upperRight, LOW);
       delay(250);
     randNumber = random(1, 7);
   }
     if (randNumber == 1){
       digitalWrite(uppermiddleLeft, HIGH);
     }
      if (randNumber == 2){
       digitalWrite(bottomLeft, HIGH);
       digitalWrite(upperRight, HIGH);
     }
    
     if (randNumber == 3){
       digitalWrite(bottomLeft, HIGH);
       digitalWrite(upperLeft, HIGH);
       digitalWrite(upperRight, HIGH);
     }
      if (randNumber == 4){
       digitalWrite(bottomLeft, HIGH);
       digitalWrite(uppermiddleLeft, HIGH);
       digitalWrite(bottomRight, HIGH);
       digitalWrite(upperRight, HIGH);
     }
      if (randNumber == 5){
       digitalWrite(bottomLeft, HIGH);
       digitalWrite(uppermiddleLeft, HIGH);
       digitalWrite(upperLeft, HIGH);
       digitalWrite(bottomRight, HIGH);
       digitalWrite(upperRight, HIGH);
     }
      if (randNumber == 6){
       digitalWrite(bottomLeft, HIGH);
       digitalWrite(bottommiddleLeft, HIGH);
       digitalWrite(uppermiddleLeft, HIGH);
       digitalWrite(bottomRight, HIGH);
       digitalWrite(bottomRight, HIGH);
       digitalWrite(upperRight, HIGH);
     }
   }
 
I like my automatic electronic dice machine.  But I am still working on my manual electronic dice machine, because I know that it will working when I find what is not working with the button. 
 
  Here is a picture of my board configuration:
 
 
SketchUp
 
SketchUp is a really great designing tool.  I enjoyed getting familiar with all of the tools it has available for designing my MakerSpace.  I have in fact purchase a one year student copy because I know I will be able to use it for other designing purposes as well.    I am just getting started but here is a picture of my starting design project:
 
 
I am really going to enjoy designing my MakerSpace with SketchUp.



No comments:

Post a Comment