Skip to main content

Section 2.8 Lab 2: Switches and State Machines

Lab 2 has two parts: a Technical Study on RC debouncer design (prelab math), and two Design Challenges implemented on the Nucleo. Read the full lab handout for the prelab deliverables.
Figure 2.8.1. Lab 2 breadboard setup: three LEDs (two red WIN LEDs and one green starter LED), two pushbutton switches, and debounce capacitors. Each LED has its own 220 ฮฉ current-limiting resistor.

Activity Activity

1. Design Challenge 1: Pushbutton Counter.

Starting from blinkyCNT.c, implement a button-controlled pause/resume: pressing the button once pauses the LED counter; pressing again resumes it. Use your debounce capacitor (0.1 ยตF or 0.15 ยตF) across the switch. Use the FSM pattern to avoid blocking.

2. Design Challenge 2: Reaction Time Game.

Each player has a debounced button and a red WIN LED. A third green LED is the start signal. The game runs as follows:
  1. Initially, the green starter LED is on (or blinking rapidly).
  2. Either player presses their button to start the game.
  3. The starter LED flashes three times, one second apart, then glows steadily: this is the go signal.
  4. Each player presses their button as soon as they see the steady light. The first player to press wins; their WIN LED lights (or blinks rapidly) for one second.
  5. The system returns to step 1.
Design with a state machine. Start with a paper state diagram before writing any code; include it in your submission along with pseudocode and a debugging diary.
Optional extensions (no extra points): (a) A false start, pressing before the steady light, awards the win to the other player. (b) Make the delay between the last flash and the steady ON slightly random (run a fast counter in the background and sample it when a player starts the game) to prevent timing the flashes.