Programming for Robotics with Arduino C++ 1, Challenge 2 solution (budget the black box) Idea: the RAM used by the globals grows linearly with the log size, so two compiler reports give the cost of one record and of everything else. With 2048 bytes of RAM and 512 bytes to keep for the stack, the largest log is the largest N for which 345 + 8 N is at most 1536. Then compile with N - 1, N and N + 1 to see whether the prediction holds. The script calls arduino-cli (compile only; no board is needed) with -DLOGN=; pass it the folder of the sketch. CODE: """x2.py -- challenge 2: how many log records fit if 512 bytes must stay free for the stack? Predict from two builds, then check with the real compiler.""" import re import subprocess import sys CLI = r"C:\Users\emuba\tools\arduino-cli\arduino-cli.exe" SKETCH = sys.argv[1] # the folder robot_ch1 RAM, STACK_RESERVE = 2048, 512 def ram_used(n): out = subprocess.run([CLI, "compile", "--fqbn", "arduino:avr:uno", "--build-property", "compiler.cpp.extra_flags=-DLOGN=%d" % n, SKETCH], capture_output=True, text=True).stdout m = re.search(r"Global variables use (\d+) bytes", out) return int(m.group(1)) if m else None a, b = 16, 64 ra, rb = ram_used(a), ram_used(b) per_record = (rb - ra) / (b - a) base = ra - per_record * a predicted = int((RAM - STACK_RESERVE - base) // per_record) print("measured: LOGN=%d uses %d bytes, LOGN=%d uses %d bytes" % (a, ra, b, rb)) print("so one record costs %.1f bytes and everything else costs %.0f bytes" % (per_record, base)) print("prediction: the largest log that leaves %d bytes free is %d records" % (STACK_RESERVE, predicted)) for n in (predicted - 1, predicted, predicted + 1): used = ram_used(n) print("check: LOGN=%d -> %d bytes of globals, %d bytes left for the stack%s" % (n, used, RAM - used, " <- too big" if RAM - used < STACK_RESERVE else "")) END CODE OUTPUT: measured: LOGN=16 uses 473 bytes, LOGN=64 uses 857 bytes so one record costs 8.0 bytes and everything else costs 345 bytes prediction: the largest log that leaves 512 bytes free is 148 records check: LOGN=147 -> 1521 bytes of globals, 527 bytes left for the stack check: LOGN=148 -> 1529 bytes of globals, 519 bytes left for the stack check: LOGN=149 -> 1537 bytes of globals, 511 bytes left for the stack <- too big END OUTPUT Reading the output. Two builds give 8.0 bytes a record and 345 bytes for everything else; the prediction is 148 records, and the compiler confirms it exactly: 147 records leave 527 bytes, 148 leave 519, and 149 leave 511, one byte more than the reserve is short, so 148 is the largest. Check on method: the figure is the compiler's report of statically allocated globals, so 'left for the stack' is an upper bound on what the stack can use, not a measurement of how deep it goes. The program was never run, so the 512-byte reserve is a number chosen, not one proved enough: a deep call chain, a String or an interrupt handler could need more, and on a real board the stack growing into the globals corrupts them without any message. Flash was not the limit: it was about 15 % in every one of these builds.