Improved Whac-an-LED

Based on a previous inventr.io project, this is an improved Whac-an-LED game with sound and visual scorekeeping, reimplemented on the 4x4 keypad included in the 30 Days Lost in Space kit.

COMPONENTS AND SUPPLIES

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

LED

4

Male to Female Wire

12

Male To Male Wire

7

Small Breadboard

1

4x4 Keypad

1

4 Digit Display Module

1

Passive Buzzer Module

1

220 Ohm Resistor

4

ABOUT THIS PROJECT

After completing the 30 Days set of projects, my nephew and I were working on some of the additional projects found on inventr.io. When it came time to do the Whac-an-LED project, I noticed they were using single buttons instead of a keypad and that there was no sound or visual scorekeeping, so I added them!

SCHEMATICS

Improved Whac-an-LED

COMMENTS

Leave a Reply

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

/* * Whac-an-LED – Connal & UJ * */ // LIBRARIES #include <Keypad.h> #include <TM1637Display.h> // DEFINES #define CLK 3 #define DIO 2 // CONSTANTS const int ledRed = A5; const int ledBlue = A4; const int ledYellow = A3; const int ledGreen = A2; const int buzzer = 4; const byte ROWS = 4; const byte COLS = 4; const uint8_t OFF[] = {0, 0, 0, 0}; // GLOBALS int count = 0; // hit counter int playTime = 10; // number of moles int period = 1000; // set time period char customKey = 'X'; // instantiate customKey var unsigned long timeNow; // instantiate timeNow var TM1637Display display(CLK, DIO); // instantiate display object char buttons[ROWS][COLS] = { {'R', 'B', 'Y', 'G'}, {'R', 'B', 'Y', 'G'}, {'R', 'B', 'Y', 'G'}, {'R', 'B', 'Y', 'G'} }; byte rowPins[ROWS] = {9, 8, 7, 6}; byte colPins[COLS] = {10, 11, 12, 13}; Keypad customKeypad = Keypad(makeKeymap(buttons), rowPins, colPins, ROWS, COLS); void flashRed() { timeNow = millis(); while(millis() < timeNow + period) { digitalWrite(ledRed, HIGH); customKey = customKeypad.getKey(); if (customKey == 'R') { // red button count++; display.showNumberDec(count); tone(buzzer, 1500, 250); } } digitalWrite(ledRed, LOW); } void flashBlue() { timeNow = millis(); while(millis() < timeNow + period) { digitalWrite(ledBlue, HIGH); customKey = customKeypad.getKey(); if (customKey == 'B') { // blue button count++; display.showNumberDec(count); tone(buzzer, 1500, 250); } } digitalWrite(ledBlue, LOW); } void flashYellow() { timeNow = millis(); while(millis() < timeNow + period) { digitalWrite(ledYellow, HIGH); customKey = customKeypad.getKey(); if (customKey == 'Y') { // yellow button count++; display.showNumberDec(count); tone(buzzer, 1500, 250); } } digitalWrite(ledYellow, LOW); } void flashGreen() { timeNow = millis(); while(millis() < timeNow + period) { digitalWrite(ledGreen, HIGH); customKey = customKeypad.getKey(); if (customKey == 'G') { // green button count++; display.showNumberDec(count); tone(buzzer, 1500, 250); } } digitalWrite(ledGreen, LOW); } void molePopup(long led) { switch(led) { case 0: flashRed(); break; case 1: flashBlue(); break; case 2: flashYellow(); break; case 3: flashGreen(); break; } delay(50); } void lightsOn() { digitalWrite(ledRed, HIGH); digitalWrite(ledBlue, HIGH); digitalWrite(ledYellow, HIGH); digitalWrite(ledGreen, HIGH); } void lightsOff() { digitalWrite(ledRed, LOW); digitalWrite(ledBlue, LOW); digitalWrite(ledYellow, LOW); digitalWrite(ledGreen, LOW); } void gameEnd() { for(int i = 0; i < 5; i++) { // turn LEDs on lightsOn(); display.showNumberDec(count); tone(buzzer, 800, 750); delay(1000); // turn LEDs off lightsOff(); display.setSegments(OFF); delay(1000); } count = 0; // reset score } void playGame() { // Play start tone tone(buzzer, 440, 1000); lightsOn(); display.showNumberDec(count); delay(1000); lightsOff(); delay(2000); for(int i = 0; i < playTime; i++) { molePopup(random(4)); } } void setup() { pinMode(ledRed, OUTPUT); pinMode(ledBlue, OUTPUT); pinMode(ledYellow, OUTPUT); pinMode(ledGreen, OUTPUT); pinMode(buzzer, OUTPUT); // Set display brightness display.setBrightness(7); // Clear display display.setSegments(OFF); // Show counter display.showNumberDec(count); randomSeed(analogRead(5)); } void loop() { playGame(); delay(1000); gameEnd(); delay(2000); }
PROJECT AUTHOR
Leave a comment
ADDITIONAL CONTRIBUTORS
Austin Eckman

Whac-an-LED

https://inventr.io/whack-an-led-hero-kit/
Alexander Eschenauer

Whac-an-LED

https://inventr.io/whack-an-led-hero-kit/
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.