Robin2: Very helpful. In the organization element, after over 30 years of teaching university-level programming courses, to me the design part is where most students drop the ball. Agile technology suggests the planning doesn't have to be elaborate, like the old IBM BDUF (Big Design Up Front) approach. As a result, I use the Five Programming Steps (details in Beginning C for Arduino and several other of my books):
Step 1. Initialization. Sets the environment for running the program. Opens ports, DB connections,
file handles, memory allocations, etc. Like many Windows programs that update the File menu
with the most-recently used files, this step gets things ready. Done before the user "sees" anything.
For the Arduino, setup() seems appropriate.
Step 2. Input. Gather the data that will be used by the program. Verify its validity if possible and react
if not as expected. Usually means reading sensors, pots, ports...whatever. This is usually an early
segment of the loop() code. Using function calls helps to encapsulate the code.
Step 3. Processing. Virtually all programs take data in one form, crunch on it, and output it in another
form. The "crunching" is done in this step. In most cases, an algorithm directs the processing.
Sometimes multiple processing steps must be done to interact and produce a given result. This is
also part of the loop() code, but Steps 2 and 3 isolate each other by using function calls.
Step 4. Output. This is where the results of Step 3 are presented. The output may be displayed on LEDs,
LCD display, sensors, meters, printers, data files, or passed on to anther process where it becomes
Step 2 for that process. Again, a function call should be used to decouple the output from the other
steps as much as possible.
Step 5. Termination. This is where most programs clean up after themselves...freeing memory allocations,
closing DB connections, closing ports, turning sensors off, releasing file handles, etc. In many cases, it
"undoes" what Step 1 did. For microcontroller applications, most do not have a Step 5 as they are often
designed to run forever, or until power is removed or there is component failure.
Beginning students rarely take the time to think through a simple set of design steps. Taking these Five Steps and doing a sideways refinement often saves a ton of time down the road.