Arduino Whack a Mole

For this challenge we will create a a game of Whack-A-Mole using an Arduino device/board, three LEDs, three push buttons and a piezo buzzer.

You do not need to have access to an Arduino board. Instead you can use the online Arduino simulator from Tinkercad to recreate the electronic circuit and add the code provided below.

Open Tinkercad in new window

Electronic Circuit


First you will need to recreate the following electronic circuit:
arduino-Whack a Mole

C/C++ Code


Then you will need to use the following code (When using tinkerpad, choose the text option rather than blocks)

// Arduino Whack-A-Mole - www.101computing.net/arduino-whack-a-mole
int red = 0;
int green = 0;
int blue = 0;
int redIn = 5;
int redOut = 11;
int greenIn = 6;
int greenOut = 12;
int blueIn = 7;
int blueOut = 13;
int piezoPin = 10;
int incorrectOut = 10;
int correctOut = 9;
int mole = -1;

void setup()
{
  pinMode(redOut, OUTPUT); 
  pinMode(greenOut, OUTPUT); 
  pinMode(blueOut, OUTPUT); 
  pinMode(redIn, INPUT); 
  pinMode(greenIn, INPUT); 
  pinMode(blueIn, INPUT); 
}

// During the pause all thre moles are hidden, pressing any button would trigger a low pitch noise
void pause() {
  unsigned long current;
  unsigned long end;
  
  mole = -1;
  digitalWrite(redOut, LOW);
  digitalWrite(greenOut, LOW);
  digitalWrite(blueOut, LOW);
  
  int pause = rand() % 500 + 100;
  current = millis();
  end = millis() + pause;
  while  (millis()<end) {
   	red = digitalRead(redIn); 
   	green = digitalRead(greenIn); 
   	blue = digitalRead(blueIn); 
    if (red == HIGH || green == HIGH || blue == HIGH) { 
       tone(piezoPin, 100, 300); //Pin,Frequency,Duration
       delay(300);
    }
  }
}

// Randomly decide which LED to turn on
void displayMole() {
  mole = rand() % 3 + 1;
  if (mole==1) {
    digitalWrite(redOut, HIGH); 
  } else if (mole==2) {
    digitalWrite(greenOut, HIGH);
  }else if (mole==3) {
    digitalWrite(blueOut, HIGH); 
  }
}

// If the user presses a button corresponding to the right LED (Whack a mole) > High pitch noise
// If the user presses the wrong button > Low pitch noise
void whackMole() {
  unsigned long current;
  unsigned long end;
  current = millis();
  end = millis() + 500;
  while  (millis()<end) {
  	red = digitalRead(redIn);  // read input value
  	if (red == HIGH) {         // check if the input is pressed
   		if (mole==1) {
        	// High pitch tone +++
    		tone(piezoPin, 1000, 10); //Pin,Frequency,Duration
    		delay(10);
    	} else {
        	// Low pitch tone ---
        	tone(piezoPin, 100, 300); //Pin,Frequency,Duration
    		delay(300);
   	 	}  
  	}
    green = digitalRead(greenIn);  // read input value
  	if (green == HIGH) {         // check if the input is pressed
   		if (mole==2) {
        	// High pitch tone +++
    		tone(piezoPin, 1000, 10); //Pin,Frequency,Duration
    		delay(10);
    	} else {
        	// Low pitch tone ---
        	tone(piezoPin, 100, 300); //Pin,Frequency,Duration
    		delay(300);
   	 	}  
  	}
    blue = digitalRead(blueIn);  // read input value
  	if (blue == HIGH) {         // check if the input is pressed
   		if (mole==3) {
        	// High pitch tone +++
    		tone(piezoPin, 1000, 10); //Pin,Frequency,Duration
    		delay(10);
    	} else {
        	// Low pitch tone ---
        	tone(piezoPin, 100, 300); //Pin,Frequency,Duration
    		delay(300);
   	 	}  
  	}    
  }  
}

void loop() {
  pause();
  displayMole();
  whackMole();
}

Did you like this challenge?

Click on a star to rate it!

Average rating 4.6 / 5. Vote count: 11

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: