Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!

Whack-A-Button!

Relive your childhood Whack-A-Mole memories with this wild breadboard version using the tm4c123gh6pm LaunchPad!

IntermediateFull instructions provided1 hour653
Whack-A-Button!

Things used in this project

Hardware components

EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1
Jumper wires (generic)
Jumper wires (generic)
×20
Breadboard (generic)
Breadboard (generic)
×1
Buzzer
Buzzer
×1
Single Turn Potentiometer- 10k ohms
Single Turn Potentiometer- 10k ohms
×1
Tilt Switch, SPST
Tilt Switch, SPST
×1
LED (generic)
LED (generic)
×4
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×4
7 Segment LED Display, InfoVue
7 Segment LED Display, InfoVue
×1

Software apps and online services

Energia
Texas Instruments Energia

Story

Read more

Schematics

Schematics

As we could not find the Fritzing representation of the Breadboard BoosterPack, we instead represented it using purple wires.

Code

whack_a_button.ino

C/C++
This code produces a random sequence of lit LEDs which change when the corresponding button is hit, enabling the game to be played.
Save in a folder called "whack_a_button"
Make sure to change the extension to .ino (not .c)
/*
  Whack-a-Button
  Sidekick Basic Kit for TI LaunchPad

  Modeled after Whack-a-Mole, press the button corresponding to the LED that lights up
  as many times as you can before the time runs out.

  Creators: Amar Moturu, Darren Corapcioglu, Elizabeth Sims, John Shieh, Manaal Khan, 
  Rob Gonzalez, Son Nguyen, and Sophia Hetts
*/
 #include "pitches.h"
 #include <stdio.h>
// Define the LED digit patterns for 0 - 9 in a 2 dimensional array.
// The 2D array (an array of arrays or a matrix) has 10 arrays that each
// contain 7 values.
// 1 = LED on, 0 = LED off, in this order:
// Common Anode version
byte seven_segment_digits[11][7] = { 
                                     { 0,0,0,0,0,0,1 }, // display '0'
                                     { 1,0,0,1,1,1,1 }, // display '1'
                                     { 0,0,1,0,0,1,0 }, // display '2'
                                     { 0,0,0,0,1,1,0 }, // display '3'
                                     { 1,0,0,1,1,0,0 }, // display '4'
                                     { 0,1,0,0,1,0,0 }, // display '5'
                                     { 0,1,0,0,0,0,0 }, // display '6'
                                     { 0,0,0,1,1,1,1 }, // display '7'
                                     { 0,0,0,0,0,0,0 }, // display '8'
                                     { 0,0,0,1,1,0,0 }, // display '9'  
                                     { 0,1,1,0,1,1,0 }  // display 'E'                                 
                                   };

//Ending Happy Birthday Song
int song[] = {
  NOTE_G6, NOTE_G6, NOTE_A6, NOTE_G6,NOTE_C7, NOTE_B6,
  NOTE_G6, NOTE_G6, NOTE_A4, NOTE_G4,NOTE_D5, NOTE_C5,
  NOTE_G4, NOTE_G4, NOTE_G5, NOTE_E5,NOTE_C5, NOTE_B4, NOTE_A4,
  NOTE_F5, NOTE_F5, NOTE_E5, NOTE_C5,NOTE_D5, NOTE_C5

  };

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  3, 4, 4, 4, 4, 2,
  3, 4, 4, 4, 4, 2,
  3, 4, 4, 4, 4, 4, 2,
  3, 4, 4, 4, 4, 2};

//Frequencies for notes from buzzer
int melody[] ={NOTE_B0, NOTE_A3, NOTE_FS6, NOTE_DS8};
//Start at 400ms between each count
int initial_interval = 400;
int interval = 400;
//Set score to 1
int score = 1;
//Decrement in ms when correct button is pressed
int decrement = 6;
//Set the LED off to start
int LED_state = LOW;
//Set random LED to turn on
int LED = rand() % 4 + 11;
int other_LEDs[3]; //The buttons that player should not push.
int buzzer = 33; // Buzzer connected to port 33

void setup() {
  
  //Set up the 7-segment display
  pinMode(3, OUTPUT); // set segment A as output
  pinMode(4, OUTPUT); // set segment B as output
  pinMode(5, OUTPUT); // set segment C as output
  pinMode(6, OUTPUT); // set segment D as output
  pinMode(7, OUTPUT); // set segment E as output
  pinMode(8, OUTPUT); // set segment F as output
  pinMode(9, OUTPUT); // set segment G as output
  pinMode(10, OUTPUT); // set dot as output
  for(int i = 3; i < 10; i++) { // start with segments off
    digitalWrite(i, HIGH);
  }
  digitalWrite(10, HIGH);  // start with the dot off

  //Set up the LEDs
  Serial.begin(9600); 
  int i= 0;
  for(int i=11;i<15;i++){
    pinMode(i, OUTPUT);    
  }
  //Set up the buttons
  pinMode(15, INPUT_PULLUP);   
  pinMode(17, INPUT_PULLUP);   
  pinMode(18, INPUT_PULLUP);   
  pinMode(19, INPUT_PULLUP); 

  //Set up the tilt switch
  pinMode(28, INPUT_PULLUP);
  
}

//Gets the pin number of button associated with each LED
int LED_to_push(int LED){
  switch(LED){
    case 11:
      return 15;
    case 12:
      return 17;
    case 13:
     return 18;
     case 14:
      return 19;
  }
}

//Gets the note frequency associated with each LED
int LED_to_note(int LED){
  switch(LED){
    case 11:
      return melody[0];
    case 12:
      return melody[1];
    case 13:
     return melody[2];
     case 14:
      return melody[3];
  }
}

//Displays the count number on the 7-segment display using the array above and setting each pin
void display(int count){
  int pin = 3;
    for (int segCount = 0; segCount < 7; ++segCount) {
      digitalWrite(pin, seven_segment_digits[count][segCount]);
      ++pin;
    }
}

//Switches the value of the pin associated with the LED (switches LED on and off)
void LED_switch(int LED){
  if (LED_state == LOW)
      LED_state = HIGH;
    else
      LED_state = LOW;
  digitalWrite(LED, LED_state); 
}

//Update the other_LED array
void update_LEDs(int LED){
  int count=0, port=11;
  for(int count=0; count<4; count++){
    if (count+11 != LED){
      other_LEDs[count]= port;
      port++;
    }else{
      other_LEDs[count]= port+1;
      port+=2;
    }
  }

}

void loop() {
  Serial.print("Interval");
  Serial.println(interval);
  boolean flag = true;
  //Generates random pin number for LED to display
  LED = (LED - 10 + rand()%3 )%4+11; //Make sure the same LED does light up twice in a row
  update_LEDs(LED);
  int but0 = LED_to_push(other_LEDs[0]);
  int but1 = LED_to_push(other_LEDs[1]);
  int but2 = LED_to_push(other_LEDs[2]);
  Serial.print(but0);
  Serial.print(but1);
  Serial.print(but2);
  //Counts down time to press correct button
  for (int count = 9; count >= 0; --count) {
    //Turn on/change LED, tone, and display
    LED_switch(LED);
    tone(buzzer, LED_to_note(LED),interval);
    display(count);
    int delay_interval = 50;
    //Checks every 50ms if correct button is pressed
    for (int delay_cnt = 0; delay_cnt < interval; delay_cnt += delay_interval){
      //Checks if correct button corresponding to LED is pressed
      if (!digitalRead(LED_to_push(LED))){
        Serial.print("~");
        //Turns LED off
        digitalWrite(LED, LOW);
        LED_state = LOW;
        //Decreases time between counts
        if (interval > 80) interval -= decrement;
        flag = false;
        break;
      }else if(!digitalRead(but0)| !digitalRead(but1) | !digitalRead(but2) ) break; //Wrong buttons => End GAME
      delay(delay_interval);
    }
    if (!flag) { //Right button --> Increment score
      score++;
      Serial.print("SCORE");
      Serial.println(score);
      delay(250);
      break;
    }
  }

  //If correct button was not pressed, game is over
  //Flag is still true
  if(flag){
    display(10);
    
    // Calculate the score
    int size = (int)(log10(score)+1); // Size of the array to store score
    int score_array[size];
    int i =0;
    //populate the array with the right score
    for (int i=size-1; i>=0; i--){
       score_array[i] = score%10; 
       score=(int)score/10;
   }
   
   delay(100);
   
    while(flag){
      //Play Happy Birthday song and display the score.
      for (int thisNote = 0; thisNote < 25; thisNote++) {
        //Display score
         if (thisNote%(size+1)<size) display(score_array[thisNote%(size+1)]); 
         else display(10);
         //Turn on LED
         int LED = rand() % 4+11;
         digitalWrite(LED,HIGH);
         //Sound
         int noteDuration = 1000/noteDurations[thisNote];
         tone(buzzer, song[thisNote],noteDuration);
         int pauseBetweenNotes = noteDuration * 1.30;
         delay(pauseBetweenNotes);
         noTone(33);
         //Turn off LED
         digitalWrite(LED,LOW);
         if (digitalRead(28)){//Check tilt switch
          flag = false;
          break;
        }
      }
      if(!flag){
        //Reset the interval, score and break to start game
          interval = initial_interval;
          delay(303);
          score = 1;
          Serial.print(score);
          break;
      }
      delay(100);   
    }
  }
}

pitches.h

C/C++
Defines pitches for end of game sound.
Save in a folder called "whack_a_button"
Change the extension to .h (not .c).
/*************************************************
 * 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

Credits

Manaal Khan
1 project • 2 followers
Contact
Sophia Hetts
1 project • 3 followers
Contact
Rob Gonzalez
1 project • 0 followers
Contact
John Shieh
1 project • 3 followers
Contact
Darren _Corapcioglu
1 project • 2 followers
Contact
Amar Moturu
1 project • 1 follower
Contact
Elizabeth Sims
1 project • 1 follower
Contact
Son Nguyen
2 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.