Lab 3: Buttons / Switches
Overviewβ
Mechanical switches bounce, so reading them reliably needs pull-up resistors and software debouncing. This lab reads a button, debounces it, and processes it with a finite state machine (FSM). The final project is a 4-way traffic light with multiple configuration modes.
Objectivesβ
- Add new C source/header files to an STM32 project.
- Read a digital input on a timer interrupt and debounce it (e.g. N = 2 filtering).
- Design and implement an FSM in the MCU.
Key ideasβ
- Pull-up resistor: with a button to GND, the pin reads 1 when released, 0 when pressed.
- Debounce: sample every N ms (10 to 50). Compare consecutive reads; only accept a state when readings agree β this filters bounces and noise.
- Module split:
input_reading(driver β buffer) andinput_processing(FSM β output).
// 3-state FSM
enum ButtonState { BUTTON_RELEASED, BUTTON_PRESSED, BUTTON_PRESSED_MORE_THAN_1_SECOND };
Final projectβ
A crossroad traffic light with 12 LEDs (4 red / 4 amber / 4 green), four 7-segment displays and three buttons, with at least 4 modes:
- Mode 1 β Normal: the traffic light runs normally.
- Modes 2/3/4 β Modify: change the red / amber / green durations (range 1β99). Button 1 selects the mode, button 2 increases the value, button 3 sets it.
Exercisesβ
- Sketch the FSM. 2. Draw the Proteus schematic. 3. Create the STM32 project (10 ms timer).
- Make the timer period easy to change without affecting behavior. 5. Add button debouncing and mode increment. 6. Display modes on 7-segments and blink LEDs per mode. 7β9. Increase the red / amber / green durations with buttons 2 and 3. 10. Integrate, record a demo, write the report.
Report & Submissionβ
Submit the FSM sketch, schematic, source code, a demo video and a report on BKeL.