Control
Controlling the Flow, Repetition, and Timing of Your Program
What it is
The Control category contains blocks that determine when instructions run, whether they run, how many times they repeat, when they stop, and how the program handles time-related operations.
The category begins with fundamental programming concepts such as:
- Delays
- Conditions
- Loops
- Repetition
- Loop control
It also contains more advanced system-control tools for:
- System running time
- Board reset
- Timers
- Watchdog Timer (WDT)
- Memory management
What it does
Control blocks organize the flow of execution.
They allow a program to:
- Wait before continuing.
- Execute instructions only when a condition is true.
- Choose between different actions.
- Repeat instructions a selected number of times.
- Repeat while a condition remains true.
- Repeat until a condition becomes true.
- Work through numerical ranges.
- Work through collections of items.
- Leave or continue a loop.
- Measure elapsed system time.
- Configure hardware timers.
- Reset the D1 R32.
- Use the watchdog timer.
- Work with memory-management operations.
Example
A program could:
- Turn an LED ON.
- Wait 1 second.
- Turn the LED OFF.
- Wait 1 second.
- Repeat the sequence 10 times.
Think of Control blocks as the traffic controller of your program. They decide when instructions wait, run, repeat, change direction, or stop.
Control Blocks
The actual Clu-Blocks Pro code generation confirms several options that were not visible when the drop-downs and configuration menus were closed.
The category can be organized into five conceptual families:
WAIT → DECIDE → REPEAT → TIME → SYSTEM CONTROL
Block Guide
| Block | Block Image | What It Does | What the Options Mean |
|---|---|---|---|
| Delay | Pauses execution before the program continues. | The example generates a 1-second delay using time.sleep(1). | |
| if | Executes instructions when a condition is true. | The gear configuration can produce if, if / else, and if / elif / else structures. | |
| for each...range | Repeats instructions using a numerical range. | The visible example generates range(0, 3, 1), using start 0, end 3, step 1, and variable i. | |
| repeat while / until | Repeats instructions according to a condition. | Confirmed options include while and the inverse-condition form generated as while not (...). | |
| for each item in | Processes the items in a supplied collection one at a time. | Each current element is represented by item. | |
| repeat times | Repeats instructions a specified number of times. | The example repeats 10 times. | |
| pass | Intentionally performs no operation. | Generates pass. | |
| Loop Control | Controls what happens inside a running loop. | Confirmed operations are break and continue. | |
| System Running Time | Provides the system running-time value. | The visible ms configuration generates time.ticks_ms(). | |
| end program (reset board) | Resets the board. | Generates machine.reset(). | |
| RESET | Performs the board reset operation. | Also generates machine.reset(). | |
| Timer Setup | Creates a hardware Timer object. | Generates tim = machine.Timer(). | |
| Timer Configuration | Configures the timer period, operating mode, and callback-related operation. | The visible example generates a 1000 ms, ONE_SHOT timer. | |
| Time Difference | Calculates elapsed/difference time using the system ticks. | Generates time.ticks_diff(time.ticks_ms(), 0) in the supplied example. | |
| WDT Setup | Configures the Watchdog Timer timeout. | The example generates a 2000 ms timeout. | |
| WDT Feeding | Feeds the configured Watchdog Timer. | Generates wdt.feed(). | |
| Garbage Collection | Runs the garbage collector. | Generates gc.collect(). | |
| Free Memory | Provides the memory value returned by Clu-Blocks Pro's memory block. | The actual generated operation is gc.mem_free(). |
1. Delay
The Delay block stops normal program execution for the specified time before allowing the next instruction to run.
time.sleep(1)Delay means: "Wait here before continuing."
2. Making Decisions with if
The if block allows the program to decide whether a group of instructions should execute.
if False:
passIf the condition is not true, the instructions inside the if section are skipped.
3. if / else
The gear configuration of the if block allows an else section to be added.
if False:
pass
else:
passThis introduces two possible paths through the program.
if / elsemeans: choose between two alternatives.
4. if / elif / else
The real generation also confirms that the block can create an additional conditional branch:
if False:
pass
elif False:
pass
else:
passThis allows the program to choose between multiple possibilities, rather than only two.
Understanding the if Progression
For beginners, I would teach these structures progressively:
- One decision: IF
- Two alternatives: IF → ELSE
- Multiple alternatives: IF → ELIF → ELSE
This avoids introducing the complete structure before students understand a simple condition.
5. Repeating Through a Numerical Range
for i in range(0, 3, 1):
passThe block therefore contains three fundamental range values:
| Parameter | Example |
|---|---|
| Start | 0 |
| End | 3 |
| Step | 1 |
The variable shown is: i
The value of i changes as the loop progresses through the range.
This becomes useful when students need both repetition and a changing numerical value.
6. Repeat While
while False:
passThe instructions continue repeating according to the condition controlling the loop.
Think of WHILE as: "Keep doing this while the condition remains true."
7. Repeat Until
The same loop block also provides the inverse-condition behavior:
while not (False):
passFor students, this can be presented conceptually as:
Repeat UNTIL the condition becomes true
The important distinction is:
- WHILE: Keep repeating while the condition is true.
- UNTIL: Keep repeating until the condition becomes true.
This is a significant improvement over our original documentation because the real generation now confirms both behaviors.
8. For Each Item
for item in []:
passThe variable: item
This type of loop becomes more useful later when students begin working with lists and collections.
9. Repeat a Specific Number of Times
for _count in range(10):
passStudents do not need to understand the generated _count variable initially.
The important block-level idea is simply: Repeat these instructions 10 times.
Use this loop when the number of repetitions is already known.
10. pass
passpass intentionally performs no operation.
It is useful when the program structure requires an instruction but nothing needs to happen there yet.
passmeans: "Do nothing here, but keep this program structure valid."
11. break
breakbreak exits the current loop.
BREAK = get out of the loop.
12. continue
The same loop-control block can also generate:
continueThis behaves differently from break.
Loop Running → CONTINUE → Move to the next loop cycle
| Operation | Main Idea |
|---|---|
| break | Leave the loop. |
| continue | Continue with the next loop cycle. |
This distinction was missing from the previous version because the closed drop-down only showed break out of loop.
13. System Running Time
The visible ms configuration generates:
time.ticks_ms()This provides the system tick value in milliseconds.
Unlike Delay, reading the system running time does not simply mean pausing the program.
This creates an important distinction:
- Delay
- Stop and wait.
- System Running Time
- Read a time-related value while program logic can continue using it.
14. Measuring a Time Difference
time.ticks_diff(time.ticks_ms(), 0)This introduces an important pattern:
- Earlier Time
- Program Continues
- Current Time
- Calculate Difference
This allows students to begin working with elapsed time without relying only on Delay.
15. Resetting the Board
The category contains two separate visible reset-related blocks.
end program (reset board)
and:
RESET
machine.reset()Therefore, the manual can now state clearly that although the two visible blocks have different labels, both generate a board reset operation in the supplied Clu-Blocks Pro generation.
16. Timer Setup
tim = machine.Timer()This creates the timer represented by: tim
The following Timer block then configures that timer.
17. Timer Configuration
tim.init(period=1000,mode=machine.Timer.ONE_SHOT,callback=None)The visible example therefore includes:
- Timer:
tim - Period:
1000 - Mode:
ONE_SHOT - Callback:
None
The visible mode is: One Shot
The central beginner concept is that the Timer is configured with a period and mode.
Because the provided real-code reference confirms ONE_SHOT but does not provide another generated Timer mode, I would still not invent additional timer modes in the manual.
18. Watchdog Timer (WDT)
The category provides two related WDT blocks.
WDT means: Watchdog Timer
This belongs to the advanced system-control portion of the category.
WDT Setup
wdt = machine.WDT(timeout=2000)The timeout is:2000 ms
The block creates the Watchdog Timer represented by: wdt
WDT Feeding
wdt.feed()The two blocks therefore form a clear pair: WDT Setup → WDT Feed
Advanced Concept: Students do not need WDT when first learning Control. Introduce the Watchdog Timer when the course reaches system reliability and more advanced program behavior.
19. Garbage Collection
gc.collect()This performs the garbage-collection operation provided by MicroPython.
20. Free Memory
This is another section where the generated MicroPython gives us a useful correction.
The block visible in the interface was labeled approximately: get the allocated memory(byte)
but the actual generated operation you provided is:
gc.mem_free()Therefore, for the manual, I recommend explaining the block according to what the code actually returns:
Provides the free-memory value reported by
gc.mem_free().
This is more accurate than describing it simply as “allocated memory.”
Delay vs. Timer vs. System Time
These three concepts should not be confused.
| Tool | Main Purpose |
|---|---|
| Delay | Pause execution. |
| System Running Time | Obtain a running-time/tick value. |
| Time Difference | Calculate a difference between time values. |
| Timer | Configure a hardware timer operation. |
A simple student model is:
- DELAY → WAIT
- RUNNING TIME → READ TIME
- TIME DIF → MEASURE DIFFERENCE
- TIMER → SCHEDULE A TIMER OPERATION
Understanding the Main Loop Types
| Loop | Use It When... |
|---|---|
| repeat N times | You know how many repetitions you need. |
| for each...range | You need repetition across a numerical range. |
| while | Repetition depends on a condition remaining true. |
| until | Repetition continues until a condition becomes true. |
| for each item | You need to process items in a collection. |
Quick Guide for Students
- Do I need the program to wait? Use Delay.
- Do I need to ask a question before performing an action? Use if.
- Do I need exactly two possible paths? Use if / else.
- Do I need several conditional paths? Use if / elif / else.
- Do I know how many times the instructions should repeat? Use repeat times.
- Do I need to go through a numerical range? Use for each...range.
- Should repetition depend on a condition? Use while / until.
- Do I need to process items from a collection? Use for each item.
- Do I need to exit a loop? Use break.
- Do I want to skip to the next loop cycle? Use continue.
- Do I need elapsed-time information? Use System Running Time + Time Dif.
- Do I need a hardware Timer? Use Timer Setup + Timer Configuration.
- Do I need advanced system reliability? Look at WDT.
- Do I need memory-management operations? Look at Garbage Collection and Free Memory.
Quick Reference
| Family | Operation | Main Idea |
|---|---|---|
| Wait | Delay | Pause execution. |
| Decision | if | Run instructions conditionally. |
| Decision | if / else | Choose between two paths. |
| Decision | if / elif / else | Choose among multiple conditional paths. |
| Repeat | Range | Iterate through numerical values. |
| Repeat | While | Repeat while condition is true. |
| Repeat | Until | Repeat until condition becomes true. |
| Repeat | For Each Item | Process items in a collection. |
| Repeat | Repeat N Times | Repeat a known number of times. |
| Loop Control | pass | Intentionally perform no operation. |
| Loop Control | break | Exit a loop. |
| Loop Control | continue | Move to the next loop cycle. |
| Time | System Running Time | Obtain ticks_ms(). |
| Time | Time Difference | Calculate a ticks difference. |
| System | Reset | Reset the D1 R32. |
| Timer | Timer Setup | Create machine.Timer(). |
| Timer | Timer Configuration | Configure period and timer mode. |
| WDT | WDT Setup | Configure the Watchdog Timer. |
| WDT | Feed | Feed the Watchdog Timer. |
| Memory | Garbage Collection | Run gc.collect(). |
| Memory | Free Memory | Obtain gc.mem_free(). |
