Programming for Robotics with Arduino C++ 1, Challenge 1 solution (how many start times does the naive timer survive?) Idea: start a 20 ms timer 0 to 39 ms before the wrap of the 32-bit counter, poll it every millisecond for 201 ms, and record the shortest and longest gap between its runs, once for Periodic (subtraction) and once for the naive now >= next. A timer is right only if every gap is exactly 20 ms, so the count of runs is not used: the first demonstration of the chapter showed a naive timer with the right count and the wrong gaps. CODE: // x1.cpp -- challenge 1: a 20 ms timer started 0..39 ms before the millis() wrap; which start offsets break the naive timer? #include #include "timing.h" struct Gaps { uint32_t last; bool seen; uint32_t lo, hi; int runs; }; static void note(Gaps& g, uint32_t now) { if (g.seen) { uint32_t d = elapsed(now, g.last); if (d < g.lo) g.lo = d; if (d > g.hi) g.hi = d; } g.last = now; g.seen = true; ++g.runs; } int main() { int naive_bad = 0, good_bad = 0; std::printf(" start (ms before wrap) Periodic gaps naive gaps naive runs\n"); for (uint32_t before = 0; before < 40; ++before) { uint32_t start = 0u - before; // 0 ms before = exactly at the wrap (start = 0) Periodic good(20); uint32_t next = start; Gaps g = {0, false, 999, 0, 0}, n = {0, false, 999, 0, 0}; for (uint32_t k = 0; k <= 200; ++k) { // 201 ms, polled every millisecond uint32_t now = start + k; if (good.due(now)) note(g, now); if (naive_due(now, next)) { next += 20; note(n, now); } } bool nb = (n.lo != 20 || n.hi != 20), gb = (g.lo != 20 || g.hi != 20); naive_bad += nb; good_bad += gb; std::printf(" %-22u %3u..%-3u %3u..%-3u %-3d %s\n", (unsigned)before, (unsigned)g.lo, (unsigned)g.hi, (unsigned)n.lo, (unsigned)n.hi, n.runs, nb ? "WRONG" : "ok"); } std::printf("of 40 start offsets: the naive timer misbehaved for %d, Periodic for %d\n", naive_bad, good_bad); return 0; } END CODE OUTPUT: start (ms before wrap) Periodic gaps naive gaps naive runs 0 20..20 20..20 11 ok 1 20..20 20..20 11 ok 2 20..20 1..39 11 WRONG 3 20..20 1..58 11 WRONG 4 20..20 1..77 11 WRONG 5 20..20 1..96 11 WRONG 6 20..20 1..115 11 WRONG 7 20..20 1..134 11 WRONG 8 20..20 1..153 11 WRONG 9 20..20 1..172 11 WRONG 10 20..20 1..191 11 WRONG 11 20..20 1..1 11 WRONG 12 20..20 1..1 12 WRONG 13 20..20 1..1 13 WRONG 14 20..20 1..1 14 WRONG 15 20..20 1..1 15 WRONG 16 20..20 1..1 16 WRONG 17 20..20 1..1 17 WRONG 18 20..20 1..1 18 WRONG 19 20..20 1..1 19 WRONG 20 20..20 1..1 20 WRONG 21 20..20 20..20 11 ok 22 20..20 1..39 11 WRONG 23 20..20 1..58 11 WRONG 24 20..20 1..77 11 WRONG 25 20..20 1..96 11 WRONG 26 20..20 1..115 11 WRONG 27 20..20 1..134 11 WRONG 28 20..20 1..153 11 WRONG 29 20..20 1..172 11 WRONG 30 20..20 1..20 11 WRONG 31 20..20 1..20 12 WRONG 32 20..20 1..20 13 WRONG 33 20..20 1..20 14 WRONG 34 20..20 1..20 15 WRONG 35 20..20 1..20 16 WRONG 36 20..20 1..20 17 WRONG 37 20..20 1..20 18 WRONG 38 20..20 1..20 19 WRONG 39 20..20 1..20 20 WRONG of 40 start offsets: the naive timer misbehaved for 37, Periodic for 0 END OUTPUT Reading the output. Periodic gaps are 20..20 for all 40 starts. The naive timer is right for only 3 of the 40 (0, 1 and 21 ms before the wrap) and wrong for 37. In those 37 two things happen. The timer's target wraps to a small number while the clock is still near 4294967295, so now >= next is true on every pass: the naive timer fires on consecutive milliseconds (gap 1 ms). It then stays silent until the clock itself wraps and climbs up to the target. For starts 2 to 10 the longest silence runs from 39 to 191 ms; for starts 30 to 39 it recovers after one 20 ms gap; for starts 12 to 20 all gaps are 1 ms, so it ran in a burst and then did not run again inside the 201 ms window. The three that work are the cases where only one tick lies between the run that wraps the target and the wrap of the clock, so there is no pass in which the two disagree. Check on method: a fake clock, polled once per millisecond, with a 201 ms window: a longer window would show more of the silence, and a faster poll would make the burst longer. The conclusion that matters does not depend on those choices: whether the naive timer works depends on where the counter happens to be when it starts, and so it can pass every test and still fail in the field.