I started with a very simple Arduino project: two traffic lights controlling an intersection (six LEDs). The first version worked, but the code was repetitive and full of digitalWrite calls. Then I decided to refactor it. By using arrays and compact, modular functions, I was able to rewrite the entire project in a way that was easier to read, easier to modify, and easier to extend.
Then I refactored it. By using arrays and small, modular functions, I rewrote the project so it’s easier to read, easier to modify, and easier to extend. Later, I changed the traffic-light logic entirely for a new version of the traffic lights, and the array/table-driven approach made that change trivial.
Yes, the examples still use blocking delay(), since this post focuses on code structure for beginners. (A non-blocking millis() version is straightforward once the state table is in place.)
The twp examples are implemented in Tinkercad, so you can check them interactively.
This post is part of a Spanish-language magazine called Resorte, with comics, hobbies, and technical notes, inspired by the legendary Lúpin, that appeared for more than 40 years in Argentina. If you read Spanish, check it out!"