What you learn today
- Relative jump depending on a pin
- Short fallback lines
- Weigh readability
A sensor on a pin is optional: if it is attached, the chip should read it, otherwise take a fallback value. With bdns and a label that works. For one or two lines there is the shorter form: brdse and brdns.
Relative depending on the pin
| Instruction | jumps by n lines if |
|---|---|
brdse device n | a device is set |
brdns device n | no device is set |
move temp FALLBACK # fallback value in case d1 is empty
brdns d1 2 # no device: leave out reading
l temp d1 Temperature
Without a device the chip jumps over the l, and temp keeps the fallback value. With a device it reads and overwrites it. Two lines, no label. As with bdse and bdns, a register holding a ReferenceId may serve as the device.
Short fallback lines
The pattern is called default first, exception after: first the safe value sits in the register, then comes the line that replaces it only under a condition.
Several optional sensors can be averaged like this. Every block only counts if its sensor is attached:
move total 0
move n 0
brdns d0 4
l t d0 Temperature
add total total t
add n n 1
The distance 4 skips the three lines after it.
Weighing readability
Relative branches save labels and lines. But they come at a price: whoever reads the code has to count lines, and every inserted line moves the targets. As a rule of thumb:
| Stretch | choice |
|---|---|
| one to three lines | relative branch |
| whole section | label with bdns |
| target in another section | always a label |
brdns d1 2 jumps over one line, not two. The distance 1 would be the next line, so no jump at all. Whoever reads “2” as “skip two lines” lands one line too far.
Only divide by the count once it is greater than 0, or catch the result. Without any sensor total / n is 0 / 0 otherwise, and that gives NaN.
Diagram
Exercise
Task
Gas sensors may be attached to d0 and d1, an LED display to d2. The wall heater “Labor” hangs on the network.
- Use
brdse/brdnsto build the average of the attached sensors. - Heater on if the average is below 20 °C. Without a sensor: off.
- The display shows the average in °C, −999 without a sensor. If the display is missing, there is no error.
Absolute comparison jumps and bdse/bdns are not allowed here.
Hints
Hint 1
One block per sensor: brdns sensorA 4, then read, add, count.
Hint 2
div mean total n gives NaN without a sensor – slt then leaves the heater off.
Hint 3
Display: brgtz n 2 skips move c NOVALUE, brdns display 2 skips the s.
Quiz
4 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed the simulator exercise
- Passed the quiz with at least 60 %
Without an account progress is saved in this browser only.
Report a mistake in this lesson
Please sign in to report. Sign in