What you learn today
- Make a light blink in rhythm
- Enable blinking with a lever
- Set the speed with a define
Above the airlock door a light should blink as long as someone has flipped the “warning” lever. sleep could do that, but then the chip would only notice the lever every few seconds. So you build the blinker with a tick counter.
A small preview: define
It should be easy to change the speed later without hunting through the code. That is what define is for:
define TICKS 4 # ticks per phase (4 ticks = 2 s)
Wherever TICKS appears afterwards, the chip uses 4. A define takes no register. define comes in detail in week 5 – today this one line at the very top is enough.
The sequence
- Read the lever. If it is not flipped: counter and light to 0, back to the start.
- Otherwise increase the counter by one.
- Once the counter reaches
TICKS: counter to 0 and flip the light. - Write the light, back to the start.
Flipping means: 0 becomes 1, 1 becomes 0. You know that from the emergency light:
sub r2 1 r2 # flip the light state
Milestones
- Lever off: the light is off.
- Lever on: the light is off for
TICKSticks first, then on forTICKSticks, and so on. - Lever back: the light goes off right away.
- Lever on again: blinking starts over with the off phase.
With TICKS = 4 the light is on for two seconds and off for two seconds.
Reset counter and light state while the lever is off. Otherwise the next flip starts blinking in the middle of a phase.
The counter lives in a register. If you take the chip out and put it back, all registers are 0 – the blinker starts over.
Diagram
Project task
Task
d0 is the warning light, d1 the “warning” lever.
define TICKS 4sets the length of one phase in ticks.- Lever flipped: light off for
TICKSticks first, then on forTICKSticks, on and on. - Lever not flipped: light off, counter back to 0.
No sleep – the lever should be read every tick.
Milestones
- Milestone 1: lever off – light off
- Milestone 2: light blinks in rhythm
- Milestone 3: lever back – light off at once
- Milestone 4: restart begins with the off phase
Hints
Hint 1
Three registers: r0 lever, r1 counter, r2 light state.
Hint 2
blt r1 TICKS next skips the flip while the phase is running.
Hint 3
In the “lever off” branch set r1 and r2 to 0.
Quiz
2 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed all milestones of the project
- 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