University of Technology Sydney · FACULTY OF ENGINEERING

41099 Chap.7 Functions, control flow and non-blocking timing

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

Functions, control flow and non-blocking timing

In this chapter

What this chapter covers

  • 01

    Calling a function, and the parameters that make a helper reusable

  • 02

    Naming conventions, and why case sensitivity is not a cosmetic issue

  • 03

    Replacing a run of separate declarations with one array

  • 04

    Choosing between conditional chains, selection, and the two loop forms

  • 05

    Elapsed time scheduling, with one timestamp per task

  • 06

    Naming states, listing their actions and writing down every transition

Worked example · free

Migrate a two rate blinking pattern off blocking waits

Q [9 marks]. AskSia assigns nine practice points to this independent exercise; they are not a University marking scheme. A sketch alternates one indicator every 1000 milliseconds and a second every 250 milliseconds, and a button must respond within a tenth of a second at any time. Written with blocking waits the button feels broken. Describe the migration.
  • 3Give each task its own stored timestamp and its own interval constant.
  • 3Read the clock once per pass so both tests compare against one instant.
  • 3Test elapsed time rather than an arrival deadline, and fold the button check into the same pass.
Declare two unsigned long variables holding when each task last acted and two constants holding the intervals, with nothing shared between them. At the top of the repeating function take the current millisecond count into a local variable. For each task, if that value minus its stored timestamp exceeds its interval, act and update the timestamp. With no waits anywhere the repeating function now completes thousands of times a second, so the button is effectively read continuously and the settling test from the previous chapter slots in unchanged.
Sia tip — Compare an elapsed difference against an interval, never a clock value against a stored deadline. The deadline form becomes unreachable when the counter wraps and the task stops permanently.
Glossary

Key terms

Parameter
A value passed into a function when it is called.
Return value
The result a function hands back to whatever called it.
Array
A single named collection holding several values of one type, reached by index.
Fall through
Continued execution into the following case when a selection branch is not terminated.
Blocking wait
An instruction that stops the whole program for a fixed duration.
State machine
A program organised as named states, each with actions and conditions that advance it.
Transition condition
The test that moves a state machine from one state to the next.
Timestamp
A stored clock reading used to measure how long something has been true.
FAQ

Functions, control flow and non-blocking timing FAQ

Why should a helper take parameters instead of reading the program's own variables?

A helper that reaches outside itself can only ever do one job, and moving it into another project drags those variables along. One that is given what it needs and hands back a result can be tested on its own, reused for a second sensor, and described in a sentence. Deciding what to pass in rather than what to reach for is the engineering content of writing helpers.

What makes a selection structure run several branches at once?

A missing terminating keyword at the end of a branch. Execution enters at the matching label and continues into every following branch until told to stop, so the symptom is that choosing one option performs that option and everything below it. A conditional chain cannot do this, which is why the fault appears only after a rewrite.

Is a state that only waits the same thing as a blocking wait?

No, and the difference is what makes the pattern useful. A waiting state records the moment it was entered and on every later pass asks whether enough time has elapsed to move on, while the rest of the program continues to run. A blocking wait hands the whole processor to one task and nothing else happens at all.

Why write the states down before writing any code?

Because a drawing of named states with conditions on the arrows makes the transitions you forgot immediately obvious, which reading code does not. It is also the artefact to show when a tutor asks how the device decides what to do next, and the course materials recommend planning the interaction and the timing before writing anything.

What is gained by replacing several separate declarations with one array?

Not brevity, but the ability to loop. Once seven pin numbers live in one collection, a loop can walk them, so setting a pattern becomes a few lines instead of a few dozen and adding another digit does not multiply the code. The course materials introduce arrays at exactly the point where the segment display would otherwise become unmanageable.

What should the default branch of a selection structure do?

Report. A default that silently does nothing wastes the most useful thing the structure offers. Printing the unexpected value to the serial monitor converts a silent failure during a demonstration into a fact you can talk about, which is a far stronger position than a device that simply stops.

Study strategy

Assessment move

Plan before typing: a system diagram for how the parts interact and a timeline for when things happen. Replace every blocking wait with a timestamp and an elapsed test as a matter of habit, because almost every later requirement assumes it. Keep a commented legend naming each state number.

Working through Functions, control flow and non-blocking timing in 41099? Sia is AskSia’s AI Engineering tutor — ask any 41099 Functions, control flow and non-blocking timing 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 4-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