Lessons and the learning game

Fixed-point and integer math

Fractions without a floating-point unit: fast, exact integer math on an Uno, a knob and an LED.

How it works

Fixed-point and integer math runs every lesson on the gallery's dimmer: an Arduino Uno, a potentiometer on A0 and an LED on pin 9, a PWM pin. The knob is the real input each lesson scales, guards, looks up or filters, and the LED shows the result.

Every lesson opens a working C sketch. You run it, read what the serial monitor prints, then change a constant and run again. The checks read the serial output, so every number a lesson quotes, the micros() timings included, is one the board really printed.

The lessons

  1. Why floats cost: 1000 int, long and float multiplies timed with micros(), the empty loop taken off, then divides. A float that cannot add 1 to 16777216, and ten dimes that miss a dollar.
  2. Q formats: pi as the integer 804 in Q8.8, printing a Q number with integers only, multiply then shift, divide with the shift first, Q4.12 for finer steps, and negative numbers.
  3. Scaling ADC readings: knob counts to millivolts four ways. A 16-bit sum that reads 3 mV at 5 V, a long divide that is right but slow, a float, and a multiply and shift with a Q8 and then a Q16 constant.
  4. Overflow guards: 30000 + 10000 wrapping to -25536, saturating adds, __builtin_add_overflow, checking before a multiply, and a knob-driven rocket whose speed flips negative.
  5. Lookup tables: gamma correction from a 33-entry PROGMEM table against pow(), flash against SRAM, interpolation with a shift, and the classic one-count-short at the top of the table.
  6. Fast trig: binary angles that wrap by themselves, a quarter-wave sine table in Q1.14 folded into a whole circle, its error measured against sin(), and a satellite orbit drawn with it.

The project: a fixed-point filter

The last lesson smooths a noisy knob with an exponential moving average, run three ways side by side: float, plain integers and Q8 fixed point. You watch the plain one stall 7 counts short, time all of them, step the knob and count the samples it takes to settle, then tune the filter with one shift and strip the noise away to see the stall in the open. It ends with ideas to take it further: two stages, a median, more fraction bits or an adaptive filter.

What the Uno does differently

  • The ATmega328P has no floating-point unit and no divide instruction. A float multiply costs about 159 clocks and a 32-bit divide over 600, where a shift costs a few.
  • An int is 16 bits and a double is the same 4-byte float as a float, so sums that are fine on a laptop overflow here. Every lesson asks where the biggest intermediate value is.
  • Signed overflow is undefined behavior in C. The sketches wrap on purpose in unsigned types and guard everything else.
  • A plain const table is copied into the 2 KB of SRAM at reset. PROGMEM keeps it in flash, read with pgm_read_byte and pgm_read_word.
  • micros() steps in 4 us, and the timer interrupt fires every 1024 us, so the sketches time a hundred or a thousand operations and divide.

After this course

Data structures without malloc keeps the same habit of counting every byte, for the queues, pools and tables that real firmware is built from.