41099 Chap.4 Arduino program structure and digital output
Arduino program structure and digital output
What this chapter covers
- 01
Two framework functions with different lifetimes, and the rule for sorting statements between them
- 02
Configuring a pin as an output, and why a pin cannot be an input at the same time
- 03
Writing a pin high or low, and the 5 V and 0 V those words stand for
- 04
The blocking wait, its parameter type and its range
- 05
Declaring and initialising a variable, and why the equals sign is an instruction
Turn a working but brittle sketch into one you can modify under observation
- 3Replace every literal pin number with one named constant declared above the framework functions.
- 2Give the on time and the off time separate names so they can differ.
- 2Make the requested change and state why the two sketches differ only under modification.
Key terms
- Sketch
- A complete program written for the board, containing both framework functions.
- Pin mode
- The configured electrical role of a pin, either sending voltage to a circuit or reading voltage from one.
- Assignment operator
- The single equals symbol, which evaluates the right side and stores the result in the variable named on the left.
- Data type
- The declaration of what kind of value a variable holds and how much memory it occupies.
- Named constant
- A value given a name once so that changing it is a single edit rather than several.
Arduino program structure and digital output FAQ
How do you decide whether a statement belongs in the one time function or the repeating one?
Ask whether the statement describes something that is true for the whole run. Pin configuration is, so it belongs in the function that executes once. Anything whose answer can change while the board is powered belongs in the function that repeats. The distinction is about lifetime rather than style, and it is what a marker is listening for.
Is it wrong to configure a pin inside the repeating function if the circuit still works?
It is not a compile error and the device usually behaves, which is exactly why the habit survives. It becomes a real fault as soon as the program has more than one mode, because a configuration statement buried in the repeating function silently re-asserts the old role after something else deliberately changed it. Correct today, unstable tomorrow.
What is the difference between setting a pin low and leaving it unconfigured?
A pin held low is actively connected to ground and will sink current, which is the whole basis of active low wiring. A pin that has never been configured is in neither state and its voltage is not predictable, so anything reading it gets meaningless values. Configure every pin your sketch uses.
Why does the timing function insist on a large unsigned type?
Because it counts milliseconds, and an ordinary signed integer on this board overflows after about thirty three seconds and turns negative. Every comparison based on elapsed time then produces nonsense. The unsigned long type is thirty two bits wide and never negative, which is why the course materials specify it explicitly.
Assessment move
Read other people's sketches, not only your own, and for each line ask why it sits where it does. Before any assessed modification, replace repeated literals with named constants, because a change request under observation is far easier when it is one edit. Say the purpose of each framework function out loud once.
Working through Arduino program structure and digital output in 41099? Sia is AskSia’s AI Engineering tutor — ask any 41099 Arduino program structure and digital output question and get a clear, step-by-step explanation grounded in how 41099 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.