Thursday, November 14, 2024

Week 4 Challenge - Roll a Die with a Button

This one went very, very smoothly on Tinkercad.  In real life, not so much.  There was soooooo much pain and suffering related to getting every dumb pin connected exactly right!

I also did not like that the center pin couldn't actually be in the center, since it needs its own input line.  The perfectionist in me was definitely not enjoying it. Also soooooo many wires!!!  The circuit diagram was a mess, and it was worse in real life!

In the video you can see that it's working, but also that it's a mess.


Per the video, I also did 100 trials and put the results in Excel to plot a histogram.  It's not a perfect uniform distribution, but since it's only 100 trials, I think the graph looked pretty good.


And now for the code!

// Arduino is like C so it indexes starting at 0
int ledPins[] = {3,4,5,6,7,8,9};
int randomNumber;
int buttonPin = 2;
int buttonState = 0;

void setup(){

  int index;

  for(index = 0; index < 7; index++){
    pinMode(ledPins[index],OUTPUT);
  }

  pinMode(buttonPin, INPUT);
 
  Serial.begin(9600);
  randomSeed(analogRead(0));
 
  // Turn all the LEDs off:
  for(index = 0; index < 7; index++)  {
    digitalWrite(ledPins[index], LOW);    
  }
  delay(100);
 
} // end setup


void loop(){
 
  int index;

   buttonState = digitalRead(buttonPin);
 
 
  if (buttonState == HIGH) {
    Serial.println("----------In the If Statement-------------------");
     // Turn all the LEDs off:
     for(index = 0; index < 7; index++)  {
       digitalWrite(ledPins[index], LOW);    
     }
     delay(100);
   
     randomNumber = random(1,7); // Generate a random # from 1 to 6
     Serial.println(randomNumber);
  }
 
  switch(randomNumber) {
    case 1:
      showOne();
      break;
    case 2:
      showTwo();  
      break;
    case 3:
      showThree();
      break;
    case 4:
      showFour();
      break;
    case 5:
      showFive();
      break;
    case 6:
      showSix();
      break;
    default:
      //Serial.println("Nothing happening");
      break;
  } // end switch
 
} // end main loop


void showOne()
{
  // Turn on #6, which is index 3
  digitalWrite(ledPins[3], HIGH);
} // end showOne()

void showTwo() {
  digitalWrite(ledPins[0], HIGH);
  digitalWrite(ledPins[6], HIGH);
} // end showTwo()

void showThree(){  
  digitalWrite(ledPins[1], HIGH);
  digitalWrite(ledPins[3], HIGH);
  digitalWrite(ledPins[5], HIGH);
} // end showThree()

void showFour(){
  digitalWrite(ledPins[0], HIGH);
  digitalWrite(ledPins[1], HIGH);
  digitalWrite(ledPins[5], HIGH);
  digitalWrite(ledPins[6], HIGH);
} // end showFour()

void showFive(){
  digitalWrite(ledPins[0], HIGH);
  digitalWrite(ledPins[1], HIGH);
  digitalWrite(ledPins[3], HIGH);
  digitalWrite(ledPins[5], HIGH);
  digitalWrite(ledPins[6], HIGH);
} // end showFive()

void showSix(){
  digitalWrite(ledPins[0], HIGH);
  digitalWrite(ledPins[1], HIGH);
  digitalWrite(ledPins[2], HIGH);
  digitalWrite(ledPins[4], HIGH);
  digitalWrite(ledPins[5], HIGH);
  digitalWrite(ledPins[6], HIGH);
} // end showSix()

No comments:

Post a Comment

Nooooo, don't make me reflect on my learning!

 ... I just want to stick wires in breadboards and make froggies dance!!! Okay, but seriously, that stupid dancing frog was clearly my most ...