LED dimmer — Smart Car
Set LED … ON/OFF has only two settings. Set LED … to … % has a hundred. This program fades LED 1 smoothly up and back down, while LED 2 sits at full brightness next to it as a ruler to compare against.
This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Car.
The program
Section titled “The program”Read the program as text
- MoBot Start
- Print to serial
"LED 1 fades. LED 2 is a full-brightness ruler to compare it against." - repeat forever
- Set LED 2 ON
- Set LED 1 to 0 %
- wait 0.4 seconds
- Set LED 1 to 10 %
- wait 0.4 seconds
- Set LED 1 to 30 %
- wait 0.4 seconds
- Set LED 1 to 60 %
- wait 0.4 seconds
- Set LED 1 to 100 %
- wait 0.8 seconds
- Set LED 1 to 60 %
- wait 0.4 seconds
- Set LED 1 to 30 %
- wait 0.4 seconds
- Set LED 1 to 10 %
- wait 0.4 seconds
- Set LED 1 to 0 %
- Set LED 2 OFF
- wait 1 seconds
What you need
Section titled “What you need”- A Smart Car. Nothing to plug in — both LEDs are on the board.
- A USB cable.
- A dim room helps a lot. In bright sunlight 30 % and 60 % look the same.
How to run it
Section titled “How to run it”- Open 04-led-dimmer.mobot, plug in, press Send to Device.
- Put the robot somewhere shaded and watch the two LEDs side by side.
What you should see
Section titled “What you should see”[MOBOT] LED 1 fades. LED 2 is a full-brightness ruler…appears once.- LED 2 switches hard on and stays on for most of the cycle.
- LED 1 climbs through five steps: off → barely there → dim → medium → as bright as LED 2. Then back down the same way.
- At the bottom of every cycle both LEDs go out together for one second, then it all starts again.
- At 100 %, LED 1 should look the same brightness as LED 2. If it looks dimmer, the two LEDs are not the same part — not a program fault.
The test passes if you can see at least three different brightnesses on LED 1, and if it reaches LED 2’s brightness at the top.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
mobot_ledbright |
0-100 %. Anything above 100 is treated as 100, below 0 as 0. |
mobot_ledon |
plain ON / OFF — really just 100 % and 0 %. |
repeat_forever, wait |
the fade. |
The Python code
Section titled “The Python code”The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.
# LED dimmer — robotics (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 04-led-dimmer.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.# The firmware 2.x version is 04-led-dimmer.main.py.
from machine import Pinimport time
print("[MOBOT]", 'LED 1 fades. LED 2 is a full-brightness ruler to compare it against.')while True: robot.led2.set(1) robot.led1.set_brightness(0) time.sleep(0.4) robot.led1.set_brightness(10) time.sleep(0.4) robot.led1.set_brightness(30) time.sleep(0.4) robot.led1.set_brightness(60) time.sleep(0.4) robot.led1.set_brightness(100) time.sleep(0.8) robot.led1.set_brightness(60) time.sleep(0.4) robot.led1.set_brightness(30) time.sleep(0.4) robot.led1.set_brightness(10) time.sleep(0.4) robot.led1.set_brightness(0) robot.led2.set(0) time.sleep(1)# LED dimmer — robotics## This is what MoBot puts on the board as main.py when you open# 04-led-dimmer.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.
from machine import Pinimport time
print("[MOBOT]", 'LED 1 fades. LED 2 is a full-brightness ruler to compare it against.')while True: mobotROBOTICS.led2.setState(1) mobotROBOTICS.led1.setLedBrightness(0) time.sleep(0.4) mobotROBOTICS.led1.setLedBrightness(10) time.sleep(0.4) mobotROBOTICS.led1.setLedBrightness(30) time.sleep(0.4) mobotROBOTICS.led1.setLedBrightness(60) time.sleep(0.4) mobotROBOTICS.led1.setLedBrightness(100) time.sleep(0.8) mobotROBOTICS.led1.setLedBrightness(60) time.sleep(0.4) mobotROBOTICS.led1.setLedBrightness(30) time.sleep(0.4) mobotROBOTICS.led1.setLedBrightness(10) time.sleep(0.4) mobotROBOTICS.led1.setLedBrightness(0) mobotROBOTICS.led2.setState(0) time.sleep(1)Try changing it
Section titled “Try changing it”- Swap the LED numbers over: make LED 2 fade and LED 1 the ruler.
- Make every
wait0.05 — the fade becomes a smooth breath instead of steps. - Type 150 into a brightness block. Nothing breaks: the robot quietly reads it as 100.