Introduction To Coding Class

Lesson #10: Whack an LED

The Great Mole Hunt of Cogsworth City

The emergency alert crackled through Chief Engineer Cogsworth's workshop at 0347 hours. The city's underground maintenance tunnels were under siege. Not by water leaks or electrical failures, but by something far more insidious: mechanical moles. These rogue automatons, originally designed to inspect pipe integrity, had malfunctioned and were now popping up randomly throughout the tunnel system, disrupting critical infrastructure.

Cogsworth rubbed his tired eyes and reached for his prototype testing board. The city council needed a solution, and they needed it fast. Traditional mole-catching methods were useless against these metal pests that appeared and disappeared with mechanical precision. What they needed was an equally mechanical response system.

"If these moles want to play games," Cogsworth muttered, pulling out four colored LEDs and an equal number of buttons, "then we'll build a game that can beat them at their own speed." The concept was elegantly simple: create a reaction-based detection system that could track and respond to random mole appearances faster than any human operator.

Your mission, should you choose to accept it, is to help Cogsworth prototype this anti-mole defense system. Four colored lights will simulate the moles popping up in different tunnel sectors. Four buttons will serve as your weapons. Hit the right button when the right light appears, and you score a direct hit. Miss, and that mechanical pest scurries away to cause more havoc. The fate of Cogsworth City's underground infrastructure rests in your steady hands and quick reflexes.

What You'll Learn

When you finish building Cogsworth's anti-mole defense system, you'll be able to:

  • Create custom functions that encapsulate specific behaviors and can be called from anywhere in your program
  • Use the switch statement to elegantly handle multiple conditions based on a single variable
  • Generate truly random sequences using the random() function and proper seeding techniques
  • Combine input reading with output control in real-time reaction-based programs
  • Structure complex programs using multiple functions that work together toward a common goal
  • Implement game logic with scoring, timing, and end conditions

Understanding the Whack-a-Mole Concept

Think about the classic arcade game that inspired this anti-mole defense system. In traditional whack-a-mole, plastic rodents pop up from holes at random intervals, and players must whack them with a mallet before they disappear. The faster your reflexes, the higher your score.

Our electronic version replaces the physical moles with LEDs and the mallet with buttons. But here's where it gets interesting: unlike the mechanical arcade version, our system can generate truly unpredictable sequences. Real randomness is crucial for training quick reflexes and preventing players from memorizing patterns.

The programming challenge lies in coordinating multiple inputs and outputs in real-time. Each LED must light up for a precise duration while simultaneously monitoring its corresponding button. Miss the timing window, and the opportunity is gone. This requires careful orchestration of delays, digital reads, and state management.

Beyond gaming, this type of system appears everywhere in industrial automation. Emergency response panels, quality control stations, and diagnostic equipment all use similar patterns: random or sequential activation of indicators coupled with operator response requirements. Master this concept, and you've mastered a fundamental pattern in human-machine interaction design.

Wiring the Defense System

This circuit requires eight total connections: four LEDs as outputs and four buttons as inputs. The electrical principle here is creating two distinct signal paths: one for the microcontroller to send signals out to the LEDs, and another for button presses to send signals back in.

  1. Connect the red LED's long leg to digital pin 2, short leg to ground
  2. Connect the blue LED's long leg to digital pin 3, short leg to ground
  3. Connect the yellow LED's long leg to digital pin 4, short leg to ground
  4. Connect the green LED's long leg to digital pin 5, short leg to ground
  5. Connect the red button between digital pin 12 and ground
  6. Connect the blue button between digital pin 11 and ground
  7. Connect the yellow button between digital pin 10 and ground
  8. Connect the green button between digital pin 9 and ground

Notice we're using the built-in pull-up resistors for the buttons (activated in code), which eliminates the need for external resistors. The LEDs connect directly because the HERO Board's output pins can safely drive them without current-limiting resistors.