UFO Whack a Mole Game

by Finn Schuuring in Circuits > Arduino

194 Views, 2 Favorites, 0 Comments

UFO Whack a Mole Game

1683887520364.jpg

I made a handheld Whack a Mole game using arduino. It is comprised of four buttons that light up when you should press them. The delay between buttons lighting up keeps getting shorter untill you lose.

Supplies

  • Arduino Uno
  • 4 x Button with LEDs build in
  • 4 x 220 ohm resistor
  • wires
  • 9v battery
  • battery holder
  • thermoplastic
  • paper mache
  • airdry clay
  • shiny mirror paint
  • small transparent cap
  • solder
  • a container for under the UFO (i used an ashtray)

Soldering the Buttons

IMG_20230425_161501.jpg
knopsolder.png
IMG_20230426_142828.jpg
IMG_20230430_130100.jpg

First I started soldering the buttons, the black wire in the drawing that connects all the grounds. I used orange wire for every corresponding LED. I recommend using the same color wire as the button/ button wire, and signify which is for buttons and which is for LED by perhaps marking them. That would make it easier to work with later.

You'll want to make sure both metals on which you're soldering are very hot before applying solder.

It's also easier to solder the resistor after soldering the wires.

I made sure to test the buttons with the code down below to see if I did everything right (or wrong).

Crafting the UFO Base

IMG_20230430_133634.jpg
IMG_20230430_133743.jpg
IMG_20230430_133704.jpg
IMG_20230506_181656.jpg

To get the UFO shape how I wanted, I made a very thin layer of thermoplastic over a wide bowl and then i cut out holes for the buttons as well as a hole for the little alien. The alien I also made using air dry clay, and i glued it to a cardboard base for support.

Strengthening the UFO

IMG_20230430_152203.jpg
IMG_20230506_174556.jpg
IMG_20230508_180718.jpg

The thin layer of thermoplastic was very fragile, to compensate I put a layer of paper mache over the plastic. Then it was strong enough to fit in the buttons. I put some hot glue on to secure the buttons in the ufo.

To secure the arduino safely I planned on fitting the top of the UFO onto an ashtray. To make sure these were stuck together, but also removable, I used magnets on the inside of the top half to fit perfectly on the ahstray. In the pictures you can see the first iteration of the magnets where the they are on a horizontal layer of clay. This didnt work so in the next picture you can see I made a vertical layer of clay instead.

I stored the magnets in this layer. First I took some aluminum foil and folded it around the ashtray, to meaure how it would fit. Then I fitted that aluminum foil on the sides of the UFO top half. Then I went over it with more clay to secure it.

Be very careful with using the clay because it is a water based clay, and water and electronics dont mix very well.

Finishing the UFO

IMG_20230506_190531.jpg
1683887520364.jpg

I painted the UFO black first, then i went over it with a special coat of shiny mirror paint. You can use whatever paint you like / have. and finally I fitted a small transparent cap (I used one from some old body lotion bottle) into the middle. And glued the alien through the hole in the cap.

Then I connected the battery and put the lid-like UFO onto the ashtray. Enjoy!

Write the Code

Here is the code for the project. It is quite a simple loop that checks for input on the buttons and orchastrates random lighting on the buttons to signal to the player that they should press it. All other functions are pretty self explanatory.

//whackamole Finn Schuuring

//buzzer
int Buz = 12;
//leds
const int redLED = 2;
const int blueLED = 3;
const int yellowLED = 4;
const int greenLED = 5;
//buttons
const int redButton = 6;
const int blueButton = 7;
const int yellowButton = 8;
const int greenButton = 9;

int waitTime = 2000;

void setup() { //runs at the start of the game, referencing to the component plugged into the arduino
 pinMode(redLED, OUTPUT);
 pinMode(blueLED, OUTPUT);
 pinMode(yellowLED, OUTPUT);
 pinMode(greenLED, OUTPUT);

 pinMode(redButton, INPUT_PULLUP);
 pinMode(blueButton, INPUT_PULLUP);
 pinMode(yellowButton, INPUT_PULLUP);
 pinMode(greenButton, INPUT_PULLUP);

 pinMode(Buz, OUTPUT);

 Serial.begin(9600);
 randomSeed(analogRead(0));
}

void loop() { //this creates the main gameplay loop of the game. here the difficulty is also hightened after each press.
 int newColor = pickNewColor();

 ledOn(newColor);

 if (isWhacked(newColor)) {
  ledOff(newColor);
  delay(500);
  waitTime = 0.9 * waitTime;
 } else {
  gameover();
  waitTime = 2000;
 }
}

boolean isWhacked(int newColor) { //This checks for the actual input and if the correct button is pressed.
 int i = 0;
 int chkButton;
 boolean whacked = false;
 boolean buttonPressed = false;

 Serial.print("Wait Time: ");
 Serial.print(waitTime);
 while ((i < waitTime) and (!buttonPressed)) {

  chkButton = isButtonPressed2();
  if (newColor == chkButton) {
   whacked = true;
   buttonPressed = true;
  } else if (chkButton > 0) {
   whacked = false;
   buttonPressed = true;
  }

  i++;
  delay(1);
 }
 return whacked;
}

int isButtonPressed2() {

 int buttonPressed = 0;

 //2 = red, 3 = blue, 4 = yellow, 5=green

 if (digitalRead(redButton) == LOW) {
  ledOn(redLED);
  buttonPressed = redLED;

  tone(Buz, 587.3295);
  delay(100);
  noTone(Buz);
 }

 if (digitalRead(blueButton) == LOW) {
  ledOn(blueLED);
  buttonPressed = blueLED;

  tone(Buz, 880.0000);
  delay(100);
  noTone(Buz);
 }

 if (digitalRead(yellowButton) == LOW) {
  ledOn(yellowLED);
  buttonPressed = yellowLED;

  tone(Buz, 783.9909);
  delay(100);
  noTone(Buz);
 }

 if (digitalRead(greenButton) == LOW) {
  ledOn(greenLED);
  buttonPressed = greenLED;

  tone(Buz, 1174.659);
  delay(100);
  noTone(Buz);
 }

 return buttonPressed;
}

void gameover() { //This runs a little loop of sound and lights to let the player know they have lost.

 for (int i = 0; i <= 2; i++) {
  ledOn(redLED);
  ledOn(blueLED);
  ledOn(yellowLED);
  ledOn(greenLED);
  tone(Buz, 698.4565);
  delay(200);
  ledOff(redLED);
  ledOff(blueLED);
  ledOff(yellowLED);
  ledOff(greenLED);
  tone(Buz, 498.4565);
  delay(200);
 }
 noTone(Buz);
 delay(2000);
}

//pick random color
int pickNewColor () { //This function randomly picks a new color.

 int randomColor;
 //2 = red, 3 = blue, 4 = yellow, 5=green
 randomColor = random(2, 6);
 return randomColor;
}

void boomerang(int speed) {
 chaseL2R(speed);
 chaseR2L(speed);
}

void chaseL2R(int speed) {
 blink(redLED, speed);
 blink(blueLED, speed);
 blink(yellowLED, speed);
 blink(greenLED, speed);
}

void chaseR2L(int speed) {
 blink(greenLED, speed);
 blink(yellowLED, speed);
 blink(blueLED, speed);
 blink(redLED, speed);
}

void blink(int color, int blinkTime) {
 ledOn(color);
 delay(blinkTime);
 ledOff(color);
 delay(blinkTime);
}

void ledOffAll() {

 digitalWrite(redLED, LOW);
 digitalWrite(blueLED, LOW);
 digitalWrite(yellowLED, LOW);
 digitalWrite(greenLED, LOW);
}

void ledOn(int colorON) {

 if (colorON == redLED) {
  digitalWrite(redLED, HIGH);
 }
 else if (colorON == blueLED) {
  digitalWrite(blueLED, HIGH);
 }
 else if (colorON == yellowLED) {
  digitalWrite(yellowLED, HIGH);
 }
 else if (colorON == greenLED) {
  digitalWrite(greenLED, HIGH);
 }

}

void ledOff(int colorOFF) {

 if (colorOFF == redLED) {
  digitalWrite(redLED, LOW);
 }
 else if (colorOFF == blueLED) {
  digitalWrite(blueLED, LOW);
 }
 else if (colorOFF == yellowLED) {
  digitalWrite(yellowLED, LOW);
 }
 else if (colorOFF == greenLED) {
  digitalWrite(greenLED, LOW);
 }

}

Downloads

Reflection

I learned quite a lot during this project. I had never worked with an arduino before nor had I ever soldered anything in my life. Learning and playing with all these things turned out to be a lot of fun but the best part was actually creating the UFO container. I had a lot of fun sculpting and adding finishing touches such as the shiny mirror paint. If I were to do this again, I would take more notes and be more prepared when making choices about how the electronics would fit inside the UFO. But I did not have that insight yet during this project because I had never done this before. Overall it was a lot of fun!!!