What you learn today
- sleep in seconds instead of ticks
- Build delays
- No sleep on line 0
After the pressure equalises, the light above the door should only come on after three seconds. With yield you would have to count six ticks for that. sleep makes it easier.
sleep counts seconds
sleep 3 pauses the chip for three seconds, not three ticks. At 0.5 s per tick that is six ticks. After that it continues on the next line. A register works instead of a number too: sleep r5.
# light three seconds after start
s d0 On 0
sleep 3
s d0 On 1
Inside a loop sleep also replaces the yield. A reading every five seconds looks like this:
# pressure to the display every 5 seconds
start:
l r0 d1 Pressure
s d2 Setting r0
sleep 5
j start
Since October 2024 sleep uses game time. If you pause the game, the waiting time pauses too.
The line 0 trap
sleep must not be on line 0. Internally the chip remembers where it waits as a negative line number – and −0 is the same as 0. On line 0 it therefore does not notice that it is waiting and burns all 128 instructions every tick. A comment or label before it is enough.
sleep makes the chip deaf
During sleep the chip reads nothing and switches nothing. A button press in that time is lost, a pressure drop goes unnoticed for five seconds. For displays and slow sequences that is fine. For anything that has to react quickly you use yield and count the ticks yourself – that comes the day after tomorrow.
| Instruction | waits | chip reacts meanwhile |
|---|---|---|
yield | one tick (0.5 s) | again in the next tick |
sleep 2 | two seconds (4 ticks) | no |
| tick counter | as long as you like | yes, every tick |
Diagram
Exercise
Task
A gas sensor is attached to d0, a light to d1, an LED display to d2.
- At start the light is off.
- After 3 seconds it comes on and stays on.
- After that the display shows the current pressure every 2 seconds.
Use sleep – and mind line 0.
Hints
Hint 1
First s d1 On 0, then sleep 3, then s d1 On 1.
Hint 2
The loop below: read, display, sleep 2, j start.
Hint 3
The comment on line 0 makes sure no sleep ends up there.
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