Lesson plan

C++ that scales lesson plan

The habits that keep a big program sane, on the Uno: references and const, classes, a ring buffer, a state machine class, templates, and logic split from pins so it tests itself. Ends with a tested calculator.

4 chapters, 7 lessons, about 6 hours, on the Arduino Uno R3. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • References, const and classes: who may touch what.
  • A ring buffer and a state machine, each as a class.
  • Templates, and logic cut away from pins so it can test itself.
  • A keypad calculator built from everything in the course.

Materials for the whole unit

  • Arduino Uno R3
  • Character LCD with I2C backpack
  • Character LCD, 16x2
  • Four-digit display, TM1637
  • Full-size breadboard
  • Half breadboard
  • Hobby servo x 2
  • Keypad, 5x4 calculator
  • LED x 3
  • Obstacle sensor, FC-51
  • Piezo speaker
  • Pushbutton x 3
  • Resistor x 3

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 1 h 50 min

Say what you mean

References, const and classes: who may touch what.

LessonTimeWhat students do
References and constA copy, a reference and a const reference to one struct: which changes stick, what each one costs, and a swap that only works through references.55 min
  1. Run it: one lamp, three calls
  2. A copy goes nowhere
  3. const: read, never write
  4. What a copy costs
  5. Swap through two references
  6. Make the copy a reference
  7. Break the promise on purpose
ClassesA Blinker class with private state, a constructor and an invariant it enforces itself: three objects, three lamps, one loop, and a bad value refused.55 min
  1. Run it: three objects
  2. One loop, many objects
  3. What an object is made of
  4. Private means guarded
  5. const member functions
  6. A fourth speed is one line
  7. Try to reach in

Materials

  • Arduino Uno R3
  • Full-size breadboard
  • LED x 3
  • Pushbutton
  • Resistor x 3

Check for understanding

Each student finishes "Classes" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Try to reach in".

Chapter 2. About 1 h 40 min

Shapes real firmware has

A ring buffer and a state machine, each as a class.

LessonTimeWhat students do
A ring bufferA fixed array that behaves like an endless queue: a head and a tail that wrap, overwrite the oldest when full, and a power-of-two capacity the compiler checks.55 min
  1. Run it: an empty ring
  2. Push one
  3. Fill it
  4. Full: the oldest goes
  5. Look inside
  6. Make it smaller
  7. Let the compiler check the rule
A state machine classThe elevator rebuilt as a Lift class: enum class states, one private go() that every transition passes through, and settings that belong to the constructor.45 min
  1. Run it: a lift in a class
  2. Call it to floor 3
  3. enum class
  4. The whole trip
  5. Where the safety lives
  6. An impatient lift

Materials

  • Arduino Uno R3
  • Four-digit display, TM1637
  • Full-size breadboard
  • Half breadboard
  • Hobby servo x 2
  • LED x 3
  • Obstacle sensor, FC-51
  • Piezo speaker
  • Pushbutton x 3
  • Resistor x 3

Check for understanding

Each student finishes "A state machine class" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "An impatient lift".

Chapter 3. About 1 h 40 min

Write it once

Templates, and logic cut away from pins so it can test itself.

LessonTimeWhat students do
TemplatesclampTo() for any type, countOf() for any array, and the ring buffer as Ring<T, N>: one piece of source, a checked, fast version for every type you use.45 min
  1. Run it: one function, three types
  2. Checked like real code
  3. A template that counts
  4. The ring buffer, for any type
  5. Type into a Ring<char, 4>
  6. A bigger ring is a different type
Testable logic split from pinsA Morse decoder cut in two: logic that takes plain numbers and never touches a pin, so setup() can test it, and a thin pin layer. Find a bug with a test, fix it, move the logic to its own file.55 min
  1. Run it: a sketch that tests itself
  2. Read the failures
  3. The logic needs no button
  4. The pin layer is thin
  5. Fix it in the logic
  6. Give the logic its own file
  7. Write a test of your own

Materials

  • Arduino Uno R3
  • Character LCD with I2C backpack
  • Full-size breadboard
  • LED x 3
  • Piezo speaker
  • Pushbutton
  • Resistor x 3

Check for understanding

Each student finishes "Testable logic split from pins" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Write a test of your own".

Chapter 4. About 45 min

Project

A keypad calculator built from everything in the course.

LessonTimeWhat students do
Project: a calculatorThe keypad calculator rebuilt from this course: a Calculator class that is pure logic, exact arithmetic in hundredths, a self-test, and a pin layer fed by both the keypad and the serial monitor.45 min
  1. Run it: tested before the first key
  2. Use the keypad
  3. Same logic, another input
  4. Exact, or Err
  5. Add a test for chaining
  6. Make it yours

Materials

  • Arduino Uno R3
  • Character LCD, 16x2
  • Full-size breadboard
  • Keypad, 5x4 calculator

Check for understanding

Each student finishes "Project: a calculator" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Make it yours".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.