State records information needed to interpret the next input.
Distinguish a held button from a new press. An event loop should update state consistently rather than repeatedly toggling an output while one press is held.
The bigger picture
A microcontroller program repeatedly reads inputs, updates state and sets outputs. Its behaviour depends on electrical characteristics and timing as well as code. A correct-looking condition may behave poorly when a switch bounces or a sensor fluctuates.
- Read input
- Check range
- Update state
- Set output
Concepts to keep close
- Digital input
- An input interpreted as discrete states within the device's electrical specifications.
- Analogue reading
- A numerical representation of a measured signal within a defined range.
- Debouncing
- Handling short repeated transitions so one physical action is not counted many times.
Reasoning through the problem
Track the previous button state.
Detect the transition from released to pressed.
Update the output once for that transition.
Use the idea carefully
Check the board and component documentation before wiring. State the expected input range and the response to invalid readings. Trace timing explicitly, including repeated inputs. Test the logic in simulation, then verify hardware behaviour with suitable supervision; changing a variable on screen does not reproduce every electrical condition.