Mini-Tutorial: The Magic of Control Blocks
Let’s learn how robots make decisions, repeat actions, and organize their thoughts—just like us!
🧠 What are control blocks?
Control blocks are like the brain of your robot. They help it:
- Decide what to do (
if) - Repeat things (
for,while) - Organize tasks (
def) - React to surprises (interrupts and callbacks)
🟡 1. if — The Decision Maker
“If something happens… then do this!”
Example Mission:
Turn on a light when someone presses a button.
MicroPython Code:
import machine
pin2 = machine.Pin(2, machine.Pin.OUT) # LED
pin4 = machine.Pin(4, machine.Pin.IN) # Button
if pin4.value() == 1:
pin2.value(1)
print("Button pressed: LED ON")
else:
pin2.value(0)
print("Button not pressed: LED OFF")
🎯 Your robot is learning how to make choices!
🔁 2. for — The Repeater
“Do this again and again, for each number or item.”
Example Mission:
Turn on 5 lights one by one.
MicroPython Code:
for i in range(0, 5):
print("Turning on LED", i)
# Imagine each LED lighting up in order!
🎯 Your robot is learning how to loop through actions!
🔄 3. while — The Watcher
“Keep doing this while something is true.”
Example Mission:
Keep the light on while the button is held down.
MicroPython Code:
while True:
if pin4.value() == 1:
pin2.value(1)
else:
pin2.value(0)
🎯 Your robot is learning how to stay alert and respond!
🧰 4. def — The Organizer
“Save this task with a name so you can use it anytime.”
Example Mission:
Create a shortcut to turn on the light.
MicroPython Code:
def turn_on_led():
pin2.value(1)
print("LED turned on!")
🎯 Your robot is learning how to organize its skills!
🚀 Why this matters
These blocks are the foundation of every robot project.
They help your robot think, act, and solve problems—just like you do.
