Lesson plan

Fixed-point and integer math lesson plan

Fractions without a float unit, on an Uno and a knob: what floats really cost, Q formats, scaling, overflow guards, tables and fast trig. Ends with a fixed-point filter you tune and measure.

4 chapters, 7 lessons, about 5 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

  • Time what a float costs, then keep fractions in integers on purpose.
  • Scale a knob to millivolts, and guard every sum against overflow.
  • Work it out once and look it up: gamma curves and a quarter-wave sine.
  • A fixed-point filter on a noisy knob, measured and tuned.

Materials for the whole unit

  • Arduino Uno R3
  • Full-size breadboard
  • LED
  • Potentiometer
  • Resistor

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 25 min

Floats and Q numbers

Time what a float costs, then keep fractions in integers on purpose.

LessonTimeWhat students do
Why floats costThe Uno has no floating-point hardware. Time int, long and float multiplies with micros(), take off the loop overhead, and find where a float runs out of bits.40 min
  1. Run it: a thousand multiplies
  2. Take off the loop
  3. Out of bits at 16777216
  4. Ten dimes
  5. Now time a divide
Q formatsFractions stored in plain integers: Q8.8 in an int16_t, printing one with integers only, multiply then shift, divide with the shift first, and trading range for precision.45 min
  1. Run it: pi in an integer
  2. What a Q number can hold
  3. Multiply, then shift
  4. Divide: shift first
  5. Trade range for precision
  6. Negative numbers too

Materials

  • Arduino Uno R3
  • Full-size breadboard
  • LED
  • Potentiometer
  • Resistor

Check for understanding

Each student finishes "Q formats" 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: "Negative numbers too".

Chapter 2. About 1 h 20 min

Real readings

Scale a knob to millivolts, and guard every sum against overflow.

LessonTimeWhat students do
Scaling ADC readingsTurn a 0 to 1023 knob reading into millivolts four ways: a 16-bit sum that wraps, a long divide, a float, and a multiply and shift, timed against each other.40 min
  1. Run it: turn the knob up
  2. The 16-bit trap
  3. Right, but slow
  4. Multiply and shift
  5. Sixteen fraction bits
Overflow guardsWhat an int16_t does past 32767, and three guards: saturating adds, __builtin_add_overflow and checking before a multiply, on a knob-driven rocket speed.40 min
  1. Run it: 30000 + 10000
  2. Saturate instead
  3. Let the compiler check
  4. Full throttle
  5. A redline of your own

Materials

  • Arduino Uno R3
  • Full-size breadboard
  • LED
  • Potentiometer
  • Resistor

Check for understanding

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

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

Chapter 3. About 1 h 25 min

Tables and trig

Work it out once and look it up: gamma curves and a quarter-wave sine.

LessonTimeWhat students do
Lookup tablesGamma-correct an LED from a 33-entry table kept in flash with PROGMEM, time it against pow(), then interpolate between entries with integers only.40 min
  1. Run it: pow() against a table
  2. Flash, not SRAM
  3. Turn the knob: steps
  4. Interpolate
  5. One count short
Fast trigSine and cosine from a 65-entry quarter-wave table in Q1.14, with binary angles that wrap on their own, timed against sin() and flown as a satellite orbit.45 min
  1. Run it: sin() against isin()
  2. Binary angles
  3. Four quarters from one
  4. How wrong is it?
  5. An orbit
  6. A higher orbit

Materials

  • Arduino Uno R3
  • Full-size breadboard
  • LED
  • Potentiometer
  • Resistor

Check for understanding

Each student finishes "Fast trig" 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: "A higher orbit".

Chapter 4. About 45 min

Project

A fixed-point filter on a noisy knob, measured and tuned.

LessonTimeWhat students do
Project: a fixed-point filterSmooth a noisy knob with an exponential moving average three ways, in float, in plain integers and in Q8, then measure the stall, the cost and the settle time, and tune it.45 min
  1. Run it: the integer stall
  2. What each one costs
  3. Step the knob
  4. Tune it
  5. Take the noise away
  6. Make it yours

Materials

  • Arduino Uno R3
  • Full-size breadboard
  • LED
  • Potentiometer
  • Resistor

Check for understanding

Each student finishes "Project: a fixed-point filter" 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.