Week 4: Microcontroller Programming

The Assignment: Program an Arduino board to do something.

Materials:

My Project:

For this week’s assignment, I wanted to create a device that would help my two roommates and I decide whose turn it was to do chores (taking out the trash, picking up more toilet paper, etc.) To do this, I created a three player “game” with an Arduino-controlled microcrontroller a breadboard, and a set of three LEDs and buttons

My first task was getting the computer to recognize button input. To do this, I created a pull-down circuit in which the press of the button connects the pin of interest to the 3V power source. This in turn allows the Arduino interface to recognize the button state as being “pressed” and in response providing power to a pin that powers an LED light. The simple circuit is illustrated below:

Next, I worked to make two more duplicates of a button input circuit to represent the three necessary button inputs for the game. This proved a little bit trickier because although the coding was fairly simple copying, pasting, and modifying, I had to be sure to change every variable name to the assigned button number to avoid bugs.

Once I got the three button input system working, it was time to make the game! To do this, I created a logical gate in my code to check if all three buttons are pressed. If only one or two are pressed, then the according LEDs will light up, but the game won’t start. Once all three buttons are pressed, the game blinks and initiates a little fun light show (which I wrote in a function called “dance” which is sampled below).


          void dance() {
              digitalWrite(ledPin, HIGH);
              delay(100);
              digitalWrite(ledPin2, HIGH);
              delay(100);
              digitalWrite(ledPin3, HIGH);
              delay(100);
              digitalWrite(ledPin, LOW);
              delay(100);
              digitalWrite(ledPin2, LOW);
              delay(100);
              digitalWrite(ledPin3, LOW);
              delay(100);
          }
          

Next, a random number generator chooses a number 0, 1, or 2 and then based on the chosen number calls a specified “blink” function to indicate on the LED that the corresponding player has been chosen. Sample code is below, with the player 2 and player 3 selected code cut out:


        //check to see if all three buttons are pressed
        if ((buttonState == HIGH) && (buttonState2 == HIGH) && (buttonState3 == HIGH)) {
           //do the initiating flashing then dance twice
           digitalWrite(ledPin, HIGH);
           digitalWrite(ledPin2, HIGH);
           digitalWrite(ledPin3, HIGH);
           delay(500);
           digitalWrite(ledPin, LOW);
           digitalWrite(ledPin2, LOW);
           digitalWrite(ledPin3, LOW);
           delay(500);
           dance();
           dance();
           //choose a random player by determining an int of 0, 1, or 2
           int choice = random(0, 3);
           //if player 1 is chosen, flash their LED;
           if (choice == 0){
             flash1();
             flash1();
             flash1();
             return;
           }
      }
      

Challenges & Workarounds:

Final Product: