4×4 Whack-A-Mole

Inventr's Alex whack-An-LED but times 4

COMPONENTS AND SUPPLIES

4x4 Keypad

1

InventrKits HERO

The HERO is a derivative of “Arduino UNO R3 Reference design” we just changed up a few things to make it our own. If you’re curious about what goes into our HERO board we published the open-source hardware files on our GitHub.

1

Male to Female Wire

12

OLED Display

1

ABOUT THIS PROJECT

Still the same old whack-A-mole using all the buttons in the 4×4 keypad array paired with an OLED display.

There is 16 framed boxes on the OLED display that corresponds to all 16 buttons of the keypad.
Press the button that corresponds to the solid box that will appear on the display to get a point.
Default game rounds is 10, after that he score will be posted in the end screen.

You can edit the total number of rounds value by changing the “playVal” in the global variable declaration.

Feel free to modify the display to have it look much better than the default.

SCHEMATICS

Whace-A-Mole 2.0

COMMENTS

Comment (1)

  • Trigg Reply

    can you add sound because I tried with a buzzer and still no sound to the keypad (I want sound when you hit a button on a keypad )

    August 12, 2021 at 4:37 pm

Leave a Reply to Trigg Cancel reply

Your email address will not be published. Required fields are marked *

#include “U8glib.h” #include <Keypad.h> U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI //CONSTANTS const byte ROWS = 4; const byte COLS = 4; // GLOBALS int period = 1000; // set time period char customKey; // instantiate customKey var char randChar; //random character generated int score = 0; int playVal = 10; //playtime limit unsigned long timeNow; char buttons[ROWS][COLS] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; byte rowPins[ROWS] = {9, 8, 7, 6}; byte colPins[COLS] = {10, 11, 12, 13}; Keypad customKeypad = Keypad(makeKeymap(buttons), rowPins, colPins, ROWS, COLS); void setup(void) { // flip screen, if required // u8g.setRot180(); // assign default color value u8g.setColorIndex(1); // pixel on Serial.begin(9600); pinMode(8, OUTPUT); randomSeed(analogRead(A0)); } void loop(void) { gameStart(); //Startup Screen playGame(); // Start Game delay(1000); gameEnd(); //End Screen } void drawDisplayBox(uint8_t x, uint8_t y, char randCharVal) { randChar = randCharVal; u8g.firstPage(); do{ drawDefault(x,y); } while ( u8g.nextPage() ); timeNow = millis(); while(millis() < timeNow + period) { customKey = customKeypad.getKey(); if (customKey == randChar) //if customkeypressed same as random key generated { score++; //add 1 to score //Serial Display incase you want to check your score Serial.print("pressed key:"); Serial.println(customKey); Serial.print("randKey:"); Serial.println(randChar); Serial.print("Score:"); Serial.println(score); } } } void drawDefault(uint8_t X, uint8_t Y) //Draw ox frames with solid box { for (uint8_t posX = 0; posX < 4; posX++) { for (uint8_t posY = 0; posY < 4; posY++) { uint8_t xVal = posX*15; uint8_t yVal = posY*15; if (X == xVal && Y == yVal) //if position = random generated position draw solid box else draw framed box { u8g.drawBox(xVal,yVal,8,8); } else { u8g.drawFrame(posX*15,posY*15,8,8); } } } } void drawBlank() //draw box frames only, no solid box { for (uint8_t posX = 0; posX < 4; posX++) //loop of 4 boxes in X axis { for (uint8_t posY = 0; posY < 4; posY++) //loop of boxes in Y axix { uint8_t xVal = posX*15; uint8_t yVal = posY*15; u8g.drawFrame(posX*15,posY*15,8,8); } } } void playGame() { for (int i = 0; i < playVal; i++) // loop for length of playTime { MolePopup(random(16)); // Have a random "mole" pop up. } } void MolePopup(long randNumber) { switch(randNumber) { case 0: drawDisplayBox(0,0, '1'); //Draw solid box with random solid box coordinates and what key should be pressed break; case 1: drawDisplayBox(15,0, '2'); break; case 2: drawDisplayBox(30,0, '3'); break; case 3: drawDisplayBox(45,0,'A'); break; case 4: drawDisplayBox(0,15,'4'); break; case 5: drawDisplayBox(15,15,'5'); break; case 6: drawDisplayBox(30,15,'6'); break; case 7: drawDisplayBox(45, 15,'B'); break; case 8: drawDisplayBox(0,30,'7'); break; case 9: drawDisplayBox(15,30,'8'); break; case 10: drawDisplayBox(30,30,'9'); break; case 11: drawDisplayBox(45, 30,'C'); break; case 12: drawDisplayBox(0,45,'*'); break; case 13: drawDisplayBox(15,45,'0'); break; case 14: drawDisplayBox(30,45,'#'); break; case 15: drawDisplayBox(45, 45,'D'); break; } } void gameEnd() { u8g.firstPage(); do{ u8g.setFont(u8g_font_unifont); u8g.drawStr( 0, 22, "Game Over"); u8g.drawStr( 0, 35, "Score:"); u8g.setPrintPos(0, 50); u8g.print(score); } while ( u8g.nextPage() ); score = 0; // reset score delay(3000); } void gameStart() { u8g.firstPage(); do{ u8g.setFont(u8g_font_unifont); u8g.drawStr( 0, 22, "Starting"); } while ( u8g.nextPage() ); delay(3000); }
PROJECT AUTHOR
Leave a comment
TEAM MEMBERS
Belrey

programming

ADDITIONAL CONTRIBUTORS
Alexander Eschenauer

Idea and Code structure of the game

TABLE OF CONTENTS
Leave a comment
Share on whatsapp
Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on pinterest
Share on email

SIMILAR PROJECTS YOU MIGHT LIKE

I cascaded four SN74HC595 shift registers to control 32 LEDs on 4 registers using 3 wires. I am adding a I2C 20×4 LCD screen to display the register and led values. I might add a standard 16×2 LCD screen to show up it is done.
A simple light meter. It’s not calibrated to any meaningful scale, but the digital readout shows a higher number the brighter is the light falling on the photoresistor.