Week 2 ยท Decisions & Debugging

Session 7: switch and choosing between modes

Teach & build120 minLesson 05: Operators & Decisions

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.

  1. Lesson 05: switch: one variable, several fixed values
  2. Theory Reference: switch / case
  3. Theory Reference: Truth table for two conditions

Session plan

  1. 0:0010 min
    Recap

    Quiz: && , || and truth tables

  2. 0:1020 min
    Demo

    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.

  3. 0:3045 min
  4. 1:1535 min
    Build

    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.

  5. 1:5010 min
    Wrap-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.