Week 2 ยท Decisions & Debugging
Session 7: switch and choosing between modes
Learning outcomes
- Use switch with break and default to choose between modes
- Decide when to use an if chain and when to use switch
- Fix an assignment used as a comparison from the compiler warning
- Turn a table of input combinations into a mode number
Kit and components
- Uno, breadboard, two push buttons, onboard LED
Pre-reading about 15 min
Students read these on their phones before class. They are listed on the student Before class page.
Session plan
- 0:0010 minRecap
Quiz: && , || and truth tables
- 0:1020 minDemo
switch versus if
Rewrite the S6 output chain with a mode variable and a switch. Delete one break and show the fall-through on the Serial Monitor.
- 0:3045 minCode Along
Dead bands, the = bug and switch
- 1:1535 minBuild
Buttons to a mode number
Students compute byte mode = aPressed + 2 * bPressed and use a switch to choose off, slow flash, fast flash or steady for the four combinations, then test against a table in the logbook.
- 1:5010 minWrap-up
What does break do?
Checkpoint: ready to move on?
Students can explain what break does, and their switch-based sketch gives the output in their table for all four button combinations.
Extension for fast finishers
Print the mode on Serial only when it changes - a preview of edge detection in S14.
Catch-up path
Work through Mode selector with the switch example open beside it.
Homework and practice
Code Along 3.7 if not finished.
Facilitator notes
- Code Along 3.7 needs switch, so do not set it as homework before this session.