University of Technology Sydney · FACULTY OF ENGINEERING

41099 Chap.4 Arduino program structure and digital output

- one subject, every graph, every model, every mark
5 Chapters3-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 4 of 8 · 41099

Arduino program structure and digital output

In this chapter

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

Worked example · free

Turn a working but brittle sketch into one you can modify under observation

Q [7 marks]. AskSia assigns seven practice points to this independent exercise; they are not a University marking scheme. A sketch drives pin 9 high for 500 milliseconds and low for 500 milliseconds, with the pin number typed in four places and the duration typed in two. A tutor asks you to move the indicator to pin 6 and halve the on time while they watch. Describe the preparation that makes this safe.
  • 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.
Declare a constant integer holding the pin number and replace all four literals with it, so moving the pin becomes one edit with no chance of missing an occurrence. Declare two further constants for the two durations, which makes the asymmetry the tutor asked for expressible without rereading the program. The requested change is then two numbers and nothing else. Both versions behave identically before the request and very differently under it, and the request is the assessment.
Sia tip — Name anything that appears more than once, and name anything a tutor might plausibly ask you to change. Those two rules cover almost every literal worth replacing.
Glossary

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.
FAQ

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.

Study strategy

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.

Related courses

41057 · 25400

A+Everything unlocked
Unlocks this Bible + all 26 of your University of Technology Sydney subjects - and 1,000+ Bibles across every Australian university.
Sia - your UTS41099 tutor, unlimited, worked the way the exam marks it
The full 3-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works
Unlock the full UTS41099 Bible + 26 University of Technology Sydney subjects
$0.99 Trial