ButtonMasherz

Zachary Bustamante and Sean Vichinsky

ButtonMasherz

This game pits two opponents against each other in a battle of quick reflexes. Each person has a button that they must press fifty times. The first person to press their button fifty times wins the game and proves his worth. The game is accompanied with a monitor that tracks progress and a speaker that notes when a player has reached the end, as well as every ten button presses.

The button is hooked up to the Arduino board, which tracks the button and notes each time it's pressed. When the button is pressed, the Arduino increases a variable called "buttonPresses" ("otherbuttonPresses" for P2) by one. It is the (other)buttonPresses variable that tracks the overall score. Every ten button presses, a few different things happen. First, one of the lights on that person's side will light up. Second, the speaker will play a brief tone that increases with pitch for every tenth button press. Finally, the monitor will display a message that indicates that person's progress, such as, "PLAYER ONE IS BLAZING OFF" or "PLAYER TWO IS ALMOST THERE." When one

player gets to fifty button presses, an eight tone melody will play, and the monitor will display the message, "PLAYER 1/2 WINS!". Additionally, the lights of the losing player will shut off, to let that player know how much he has failed at everything in life.

Below is the pitches.h file, followed by the file for the game itself

____

/*************************************************
* Public Constants
*************************************************/

/*************************************************
* Public Constants
*************************************************/

 #define NOTE_B0  31
 #define NOTE_C1  33
 #define NOTE_CS1 35
 #define NOTE_D1  37
 #define NOTE_DS1 39
 #define NOTE_E1  41
 #define NOTE_F1  44
 #define NOTE_FS1 46
 #define NOTE_G1  49
 #define NOTE_GS1 52
 #define NOTE_A1  55
 #define NOTE_AS1 58
 #define NOTE_B1  62
 #define NOTE_C2  65
 #define NOTE_CS2 69
 #define NOTE_D2  73
 #define NOTE_DS2 78
 #define NOTE_E2  82
 #define NOTE_F2  87
 #define NOTE_FS2 93
 #define NOTE_G2  98
 #define NOTE_GS2 104
 #define NOTE_A2  110
 #define NOTE_AS2 117
 #define NOTE_B2  123
 #define NOTE_C3  131
 #define NOTE_CS3 139
 #define NOTE_D3  147
 #define NOTE_DS3 156
 #define NOTE_E3  165
 #define NOTE_F3  175
 #define NOTE_FS3 185
 #define NOTE_G3  196
 #define NOTE_GS3 208
 #define NOTE_A3  220
 #define NOTE_AS3 233
 #define NOTE_B3  247
 #define NOTE_C4  262
 #define NOTE_CS4 277
 #define NOTE_D4  294
 #define NOTE_DS4 311
 #define NOTE_E4  330
 #define NOTE_F4  349
 #define NOTE_FS4 370
 #define NOTE_G4  392
 #define NOTE_GS4 415
 #define NOTE_A4  440
 #define NOTE_AS4 466
 #define NOTE_B4  494
 #define NOTE_C5  523
 #define NOTE_CS5 554
 #define NOTE_D5  587
 #define NOTE_DS5 622
 #define NOTE_E5  659
 #define NOTE_F5  698
 #define NOTE_FS5 740
 #define NOTE_G5  784
 #define NOTE_GS5 831
 #define NOTE_A5  880
 #define NOTE_AS5 932
 #define NOTE_B5  988
 #define NOTE_C6  1047
 #define NOTE_CS6 1109
 #define NOTE_D6  1175
 #define NOTE_DS6 1245
 #define NOTE_E6  1319
 #define NOTE_F6  1397
 #define NOTE_FS6 1480
 #define NOTE_G6  1568
 #define NOTE_GS6 1661
 #define NOTE_A6  1760
 #define NOTE_AS6 1865
 #define NOTE_B6  1976
 #define NOTE_C7  2093
 #define NOTE_CS7 2217
 #define NOTE_D7  2349
 #define NOTE_DS7 2489
 #define NOTE_E7  2637
 #define NOTE_F7  2794
 #define NOTE_FS7 2960
 #define NOTE_G7  3136
 #define NOTE_GS7 3322
 #define NOTE_A7  3520
 #define NOTE_AS7 3729
 #define NOTE_B7  3951
 #define NOTE_C8  4186
 #define NOTE_CS8 4435
 #define NOTE_D8  4699
 #define NOTE_DS8 4978
____

#include "pitches.h" // includes the sound files needed for the speaker
#include <SoftwareSerial.h>  //includes files needed for speaker
int Pin = 15;     //establishes Player One's button as Pin 15
int otherPin = 12;   // establishes Plaer Two's button as Pin 12
int ledone = 2;   //establishes Player One's first light as Pin 2
int ledtwo = 3; //establishes Player One's second light as Pin 3
int ledthree = 4;   //establishes Player One's third light as Pin 4
int ledfour = 5;    //establishes Player One's fourth light as Pin 5
int ledfive = 6;    //establishes Player One's victory light as Pin 6
int ledsix = 7;     //establishes Player Two's victory light as Pin 7
int ledseven = 8;   //establishes Player Two's fourth light as Pin 8
int ledeight = 9;   //establishes Player Two's third light as Pin 9
int lednine = 10;   //establishes Player Two's second light as Pin 10
int ledten = 11;    //establishes Player Two's first light as Pin 11
int x = 1;          //establishes the "x" variable as 1
int y = 1;          //establishes the "y" variable as 1
int val;            //establishes Player One's "val" as an open variable
int buttonState;    //establishes the open variable for Player One's buttonstate
int valt;           //establishes Playe Two's "val" as an open variable
int buttonStatet;   //establishes the open variable for Player Two's buttonstate
int buttonPresses = 0;  //establishes Player One's "Score" as zero
int otherbuttonPresses = 0;   //establishes Player Two's "score" as zero
int sensorReading = analogRead(A0);  //begin looking for serial signal from pin A0
SoftwareSerial mySerial(17,16);  //establishes Speaker's pins as 17 and 16


int melody [] = {  //begin playing the opening melody
  NOTE_A4, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_DS5 };     //notes for the melody
  int noteDurations[] = {    //play each note for a cerain amount of time
    1,4,2,2,1};   //whole note, quarter note, half note, half note, whole note
    void setup () {    //begin void setup
      for (int thisNote = 0; thisNote < 5; thisNote++) {   //sets up variables for the melody
        int noteDuration = 1000/noteDurations[thisNote]; //translates note lengths into seconds
        tone (14, melody [thisNote],noteDuration); // play melody from pin 14
        delay(noteDuration+30); // delay each note by 30 milliseconds
  
  }
    
  pinMode(Pin, INPUT);   //establishes Player One's button as an input
  pinMode(otherPin, INPUT);   //establishes Player Two's button as an input
  pinMode(ledone, OUTPUT);   //establishes Player One's first light as an output
  pinMode(ledtwo, OUTPUT);    //establishes Player One's second light as an output
  pinMode(ledthree, OUTPUT);   //establishes Player One's third light as an output
  pinMode(ledfour, OUTPUT);   //establishes Player One's fourth lught as an output
  pinMode(ledfive, OUTPUT);    //establishes Player One's victory light as an output
  pinMode(ledsix, OUTPUT);     //establishes Player Two's victory light as an output
  pinMode(ledseven, OUTPUT);    //establishes Player Two's fourth light as an output
  pinMode(ledeight, OUTPUT);    //establishes Player Two's third light as an output
  pinMode(lednine, OUTPUT);    //establishes Player Two's second  light as an output
  pinMode(ledten, OUTPUT);     //establishes Player Two's first light as an output
  Serial.begin(9600);     //begin serial communication at 9600
  buttonState = digitalRead(Pin);    //read the state of Player One's button
  buttonStatet = digitalRead(otherPin);  //read the state of Player Two's button
  for (int ledfive = 6, ledsix = 7; ledfive >= 1, ledsix <= 11; ledfive = ledfive-1, ledsix = ledsix+1){ // begin the countdown 
    pinMode(ledfive, OUTPUT);  //establishes Player One's first light as an output
    digitalWrite(ledfive, HIGH); //turn on Player One's victory light for countdown
    digitalWrite(ledsix, HIGH); //turn on Player Two's victory light for countdown
    delay(500); // wait 500 milliseconds
    digitalWrite(ledfive, LOW); //turn off Player One's victory light
    digitalWrite(ledsix, LOW); //turn off PLayer Two's victory light
  
  }

}




void loop ()
{
  val = digitalRead(Pin); //begin the void loop function

  valt = digitalRead(otherPin);  //constantly read the state of Player Two's button

  if (val != buttonState) //if the button is on
  {
    if (val == HIGH) { //if the button has been pushed
      buttonPresses++; //increase Player One's "score" by 1 point

 
    }
  }
  buttonState = val; // read the state of Playe One's button
  if (valt != buttonStatet) {  // If Player Two's button is on
    if (valt == HIGH) {  //if Player Two's button has been pushed
      otherbuttonPresses++;  //increase Player Two's "score" by 1 point
    }
  }
  buttonStatet = valt; // constantly read the state of Player Two's button


  if (buttonPresses == 10)  //if Player One's score is at 10 points
  {

    digitalWrite(ledone, HIGH);  //turn on Player One's first light
    tone (14, NOTE_A4, 200);  //play the note that signifies a light has been turned on
    mySerial.write(254); //set up printing to the monitor
    mySerial.write(128); //set up printing to the monitor
    mySerial.write("                ");  //clear the screen
    mySerial.write("                ");  //clear the screen
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("Player One is   ");  //Print a message to the monitor
    mySerial.write("blazing off! ");  //Print a message to the monitor
    


  }
  if (buttonPresses == 20) //if Player One's score is at least twenty
  {
      tone (14, NOTE_B4,200); //play the note that signifies a light has been turned on
    digitalWrite(ledone, HIGH); //turn on Player One's first light
    digitalWrite(ledtwo, HIGH); //turn on Player Two's second light
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("                ");  //clear the screen
    mySerial.write("                ");  //clear the screen
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("Player One is   ");  //Print a message to the monitor
    mySerial.write("going hot!");  //Print a message to the monitor
  
  }

  if (buttonPresses == 30) //if Player One's score is at least thirty
  {
    digitalWrite(ledone, HIGH); //turn on Player One's first light
    digitalWrite(ledtwo, HIGH); //turn on Player One's second light
    digitalWrite(ledthree, HIGH); //turn on Player One's third light
     tone (14, NOTE_C5,200); //play the note that signifies a light has been turned on
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("                ");  //clear the screen
    mySerial.write("                ");  //clear the screen
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("Player One is   ");  //Print a message to the monitor
    mySerial.write("getting close!");  //Print a message to the monitor
   

  }
  if (buttonPresses == 40) //if Player One's score is at least fourty
  {
    digitalWrite(ledone, HIGH); //turn on Player One's first light
    digitalWrite(ledtwo, HIGH); //turn on Player One's second light
    digitalWrite(ledthree, HIGH); //turn on Player One's third light
    digitalWrite(ledfour, HIGH); //turn on Player One's fourth light
    tone(14, NOTE_D5,200); //play the note that signifies a light has been turned on
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("                ");  //clear the screen
    mySerial.write("                ");  //clear the screen
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("Player One is   ");  //Print a message to the monitor
    mySerial.write("almost there!");  //Print a message to the monitor
    
  }
  if (buttonPresses >= 50) //if Player One's score is fifty
  {
    digitalWrite(ledone, HIGH); //turn on Player One's first light
    digitalWrite(ledtwo, HIGH); //turn on Player One's second light
    digitalWrite(ledthree, HIGH); //turn on Player One's third light
    digitalWrite(ledfour, HIGH); //turn on Player One's fourth light
    digitalWrite(ledfive, HIGH); //turn on Player One's victory light
    if (y == 1) //if "y" is equal to one
    {
      int melody [] = {  //begin playing the victory melody
        NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_F5, NOTE_D5, NOTE_F5, NOTE_E5,       };  //notes to the melody
      int noteDurations[] = {  //play each note for a certain amount of time
        4,8,8,8,6,6,6,1.5,      };  //quarter note, three eighth notes, three sixth notes, one quarter note, one fifth note
      for (int thisNote = 0; thisNote < 8; thisNote++) { //sets up variables for melody
        int noteDuration = 1000/noteDurations[thisNote]; //translats notes into seconds
        tone (14, melody [thisNote],noteDuration); //play melody from Pin 14
        delay(noteDuration+50); //delay each note by 50 milliseconds
      }
      y = 2; //y now equals 2
    }
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("                ");  //clear the screen
    mySerial.write("                ");  //clear the screen
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("Player One Wins! ");  //Print a message to the monitor
    digitalWrite(ledsix, LOW); //turn off Player Two's victory light
    digitalWrite(ledseven, LOW); //turn off Player Two's fourth light
    digitalWrite(ledeight, LOW); //turn off PLayer Tow's third light
    digitalWrite(lednine, LOW); //turn off Player Two's second light
    digitalWrite(ledten, LOW); //turn off Player Two's third light
    otherbuttonPresses = 0; //reset Player Two's core to zero


  }
  if (otherbuttonPresses == 10) //if Player Two's score is at least 10
  {
    digitalWrite(ledten, HIGH);  //turn on Player Two's first light
        tone(14, NOTE_A5, 200); //play the note that signifies a light has been turned on
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("                ");  //clear the screen
    mySerial.write("                ");  //clear the screen
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("Player Two is   ");  //Print a message to the monitor
    mySerial.write("blazing off!");  //Print a message to the monitor


  }
  if (otherbuttonPresses == 20) //if Player Two's "score" is twenty
  {
    tone(14, NOTE_B5,200); //play the note that signifies a light has been turned on
    digitalWrite(ledten, HIGH); //turn on Player Two's first light
    digitalWrite(lednine, HIGH); //turn on Player Two's second light
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);   //set up printing to the monitor
    mySerial.write("                ");  //clear the screen
    mySerial.write("                ");  //clear the screen
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("Player Two is   ");  //Print a message to the monitor
    mySerial.write("going hot!");  //Print a message to the monitor
    
  }

  if (otherbuttonPresses == 30) //if Player Two's "Score" is thirty
  {
     tone(14,NOTE_C6, 200); //play the note that signifies a light has been turned on
    digitalWrite(ledten, HIGH); //turn on Player Two's first light
    digitalWrite(lednine, HIGH); //turn on Player Two's second light
    digitalWrite(ledeight, HIGH); //turn on Player Two's third light
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("                ");  //clear the screen
    mySerial.write("                ");  //clear the screen
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("Player Two is   ");  //Print a message to the monitor
    mySerial.write("getting close!");  //Print a message to the monitor
   
  }
  if (otherbuttonPresses == 40) //if Player Two's "score" is fourty
  {
     tone(14, NOTE_D6, 200); //play the note that signifies a light has been turned on
    digitalWrite(ledten, HIGH); //turn on Player Two's first light
    digitalWrite(lednine, HIGH); //turn on Player Two's second light
    digitalWrite(ledeight, HIGH); //turn on Player Two's third light
    digitalWrite(ledseven, HIGH); //turn on Player Two's fourth light
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("                ");  //clear the screen
    mySerial.write("                ");  //clear the screen
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("Player Two is   ");  //Print a message to the monitor
    mySerial.write("almost there!");  //Print a message to the monitor
   
  }
  if (otherbuttonPresses >= 50) //if Player Two's score is fifty
  {
    digitalWrite(ledten, HIGH); //turn on Player Two's first light
    digitalWrite(lednine, HIGH); //turn on Player Two's second light
    digitalWrite(ledeight, HIGH); //turn on Player Two's third light
    digitalWrite(ledseven, HIGH); //turn on Player Two's fourth light
    digitalWrite(ledsix, HIGH); //turn on Player Two's victory light
    if (x == 1)  //if variable x is equal to one
    {
      int melody [] = {   //begin melody
        NOTE_E6, NOTE_E6, NOTE_E6, NOTE_E6, NOTE_F6, NOTE_D6, NOTE_F6, NOTE_E6,       };   //play these notes as part of the melody
      int noteDurations[] = {   //play each note for a certain amount of time
        4,8,8,8,6,6,6,1.5,      };  //quarter note, three eighth notes, three sixth notes, one 1.5/8th note
      for (int thisNote = 0; thisNote < 8; thisNote++) {  //begin for statement for melody
        int noteDuration = 1000/noteDurations[thisNote]; //translate note lengths into seconds
        tone (14, melody [thisNote],noteDuration); //play melody on pin 14
        delay(noteDuration+50); //delay each note by 50 milliseconds
      }
      x = 2;  //variable x is now equal to 2
    }
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("                ");  //clear the screen
    mySerial.write("                ");  //clear the screen
    mySerial.write(254);  //set up printing to the monitor
    mySerial.write(128);  //set up printing to the monitor
    mySerial.write("Player Two Wins! ");  //Print a message to the monitor
    digitalWrite(ledone, LOW); //turn off Player One's first light
    digitalWrite(ledtwo, LOW); //turn off Player One's second light
    digitalWrite(ledthree, LOW); //turn off Player One's third light
    digitalWrite(ledfour, LOW); //turn off Player One's fourth light
    digitalWrite(ledfive, LOW); //turn off Player One's victory light
    buttonPresses = 0; //reset Player One's score back to zero

  }


}




There were a multitude of problems that we ran into during the development of this project. With the number of "if" statements that we used, there were so many of the "{" and "}"'s that we had trouble figuring out which ones were actually necessary and which ones only hindered the game. Once we solved that (with some help from Mr. Dickie), we set out to include a speaker into the game. For the most part, this went smoothly, but we had a difficult time deciding which notes would make up the melody. After that came the monitor, which was a late addition to the game. The hardest part about adding the monitor was putting the variables and the Serial.prints in the right place to where they cooperated best with the other processes involved in the programming.

If we had more time, we could've added something else to the game besides the speaker and the monitor. This would include an actual LCD screen that would run graphics instead of messages, and an LCD timer to track Personal Records.
 
  
  
  
  
  
  
 




ą
CIMG1795.JPG
(1669k)
Sean Vichinsky,
Jun 4, 2013, 8:18 AM
ą
CIMG1796.JPG
(2686k)
Sean Vichinsky,
Jun 4, 2013, 8:17 AM
ą
CIMG1802.JPG
(2787k)
Sean Vichinsky,
Jun 4, 2013, 8:16 AM
ą
CIMG1805.JPG
(2797k)
Sean Vichinsky,
Jun 4, 2013, 8:16 AM
ą
CIMG1808.JPG
(2797k)
Sean Vichinsky,
Jun 4, 2013, 8:16 AM
ą
CIMG1810.JPG
(2756k)
Sean Vichinsky,
Jun 4, 2013, 8:15 AM
ą
CIMG1812.JPG
(2805k)
Sean Vichinsky,
Jun 4, 2013, 8:15 AM
ą
CIMG1818.JPG
(2792k)
Sean Vichinsky,
Jun 4, 2013, 8:14 AM
ą
CIMG1821.JPG
(1496k)
Sean Vichinsky,
Jun 4, 2013, 8:13 AM
Comments