A math practice toy, part 2: Mixed questions and a score system on the Nano
Part 2from the A math practice toy series · 4 parts in all
Part 2 of the math-toy series, NANO-MathToyMix100-1602, adds what a practice tool actually needs to hold a child's attention: mixed question types (addition, subtraction and multiplication in one session) and a score/streak system displayed on the second row of the LCD.
What changed from part 1
- A question-type selector: each round picks
+,−or×at random, with operands sized so the question is fair (multiplication caps its factors well below 100, or no child could ever do it in their head). - The second LCD row became a status bar: current score, streak of correct answers, and a small per-type counter. Seeing the streak grow turned out to be the single best motivator.
- Timing: the toy measures how long each answer took, so parents can see whether the child is fluent or merely correct. Fluency is the real goal of arithmetic drills.
A pattern worth stealing: one function per question type
bool nextQuestion() {
switch (random(3)) {
case 0: return askAddition();
case 1: return askSubtraction();
case 2: return askMultiplication();
}
}
Each askX() function generates operands, prints the question, waits for the
answer, and returns true/false. The main loop does not know or care which type ran. When you
later add a fourth type, you touch exactly two lines. This is the
open/closed principle in its smallest form, and it translates directly to professional
code.
Series index
Part 1 was the plus/minus original; part 3 moved to a 128x64 graphics LCD; part 4 added WiFi, NTP clock and backlight dimming on ESP8266. All repositories are linked from my GitHub profile.
Repository: github.com/bobhuang1/NANO-MathToyMix100-1602