Hello dear community!
Today a post about controlling LEDs in the CyberBrick PC App with ‘Shippets’ as event modules in the Code (Beta) section…
Explanation first:
My problem was controlling the LEDs of an RC car with turn signals and headlights/taillights. In the CyberBrick App (PC), the lights would always turn off when blinking and couldn’t be controlled independently. Hence the attempt to control them accordingly using code.
Since this is only possible in CyberBrick in an ‘event-based’ manner, the possibilities to realize this with MicroPython were difficult and limited.
Especially since I wanted to ensure that the lighting and turn signals work with both lights on and off (the challenge here was checking the preconditions / middle LEDs on/off)
I’m happy to share the ‘Shippets’ here to perhaps save others with similar challenges the work.
SHIPPET 1 Turn on headlights and taillights
from leds import LEDController
led_vorne = LEDController("LED1") # Front LED
led_hinten = LEDController("LED2") # Rear LED
# Stop effects!
led_vorne.repeat_count = 0
led_hinten.repeat_count = 0
# Set LEDs MANUALLY
led_vorne.np[1] = led_vorne.np[2] = (255, 255, 255) # White
led_hinten.np[1] = led_hinten.np[2] = (128, 0, 0) # Red 50%
led_vorne.np.write()
led_hinten.np.write()
SHIPPET 2 Turn off front & rear lights
from leds import LEDController
led_vorne = LEDController("LED1") # Front LED
led_hinten = LEDController("LED2") # Rear LED
# Stop effects
led_vorne.repeat_count = 0
led_hinten.repeat_count = 0
# Turn off LEDs
led_vorne.np[1] = led_vorne.np[2] = (0, 0, 0)
led_hinten.np[1] = led_hinten.np[2] = (0, 0, 0)
led_vorne.np.write()
led_hinten.np.write()
SHIPPET 3 Right turn signal
from leds import LEDController
from machine import Timer
led_vorne = LEDController("LED1") # Front LED
led_hinten = LEDController("LED2") # Rear LED
# Stop effects
led_vorne.repeat_count = 0
led_hinten.repeat_count = 0
try:
blinker_timer
except:
blinker_timer = Timer(0)
blinker_timer.deinit()
def blink_rechts(t): # Blink right
# Read status from LEDs
licht_an = led_vorne.np[1] != (0, 0, 0) # Light on
# Toggle right turn signal (LED 3)
if led_vorne.np[3] == (0, 0, 0):
led_vorne.np[3] = (255, 80, 0)
led_hinten.np[3] = (255, 80, 0)
else:
led_vorne.np[3] = (0, 0, 0)
led_hinten.np[3] = (0, 0, 0)
# Left turn signal off (LED 0)
led_vorne.np[0] = (0, 0, 0)
led_hinten.np[0] = (0, 0, 0)
# Middle LEDs depending on status
if licht_an:
led_vorne.np[1] = led_vorne.np[2] = (255, 255, 255)
led_hinten.np[1] = led_hinten.np[2] = (128, 0, 0)
else:
led_vorne.np[1] = led_vorne.np[2] = (0, 0, 0)
led_hinten.np[1] = led_hinten.np[2] = (0, 0, 0)
led_vorne.np.write()
led_hinten.np.write()
blinker_timer.init(period=500, mode=Timer.PERIODIC, callback=blink_rechts)
SHIPPET 4 Left turn signal
from leds import LEDController
from machine import Timer
led_vorne = LEDController("LED1") # Front LED
led_hinten = LEDController("LED2") # Rear LED
# Stop effects
led_vorne.repeat_count = 0
led_hinten.repeat_count = 0
try:
blinker_timer
except:
blinker_timer = Timer(0)
blinker_timer.deinit()
def blink_links(t): # Blink left
# Read status from LEDs
licht_an = led_vorne.np[1] != (0, 0, 0) # Light on
# Toggle left turn signal (LED 0)
if led_vorne.np[0] == (0, 0, 0):
led_vorne.np[0] = (255, 80, 0)
led_hinten.np[0] = (255, 80, 0)
else:
led_vorne.np[0] = (0, 0, 0)
led_hinten.np[0] = (0, 0, 0)
# Right turn signal off (LED 3)
led_vorne.np[3] = (0, 0, 0)
led_hinten.np[3] = (0, 0, 0)
# Middle LEDs depending on status
if licht_an:
led_vorne.np[1] = led_vorne.np[2] = (255, 255, 255)
led_hinten.np[1] = led_hinten.np[2] = (128, 0, 0)
else:
led_vorne.np[1] = led_vorne.np[2] = (0, 0, 0)
led_hinten.np[1] = led_hinten.np[2] = (0, 0, 0)
led_vorne.np.write()
led_hinten.np.write()
blinker_timer.init(period=500, mode=Timer.PERIODIC, callback=blink_links)
SHIPPET 5 Turn off (hazard) turn signals
from leds import LEDController
from machine import Timer
led_vorne = LEDController("LED1") # Front LED
led_hinten = LEDController("LED2") # Rear LED
# Stop timer
try:
blinker_timer
except:
blinker_timer = Timer(0)
blinker_timer.deinit()
# Stop effects
led_vorne.repeat_count = 0
led_hinten.repeat_count = 0
# Read status of middle LEDs
licht_an = led_vorne.np[1] != (0, 0, 0) # Light on
# Turn off turn signal LEDs (LED 0 and 3)
led_vorne.np[0] = led_vorne.np[3] = (0, 0, 0)
led_hinten.np[0] = led_hinten.np[3] = (0, 0, 0)
# Restore middle LEDs
if licht_an:
led_vorne.np[1] = led_vorne.np[2] = (255, 255, 255)
led_hinten.np[1] = led_hinten.np[2] = (128, 0, 0)
else:
led_vorne.np[1] = led_vorne.np[2] = (0, 0, 0)
led_hinten.np[1] = led_hinten.np[2] = (0, 0, 0)
led_vorne.np.write()
led_hinten.np.write()
SHIPPET 6 Turn on hazard lights
from leds import LEDController
from machine import Timer
led_vorne = LEDController("LED1") # Front LED
led_hinten = LEDController("LED2") # Rear LED
# IMPORTANT: Stop effects!
led_vorne.repeat_count = 0
led_hinten.repeat_count = 0
try:
blinker_timer
except:
blinker_timer = Timer(0)
blinker_timer.deinit()
def blink_warn(t): # Hazard blink
# Read status from LEDs
licht_an = led_vorne.np[1] != (0, 0, 0) # Light on
# Toggle turn signals
if led_vorne.np[0] == (0, 0, 0):
led_vorne.np[0] = led_vorne.np[3] = (255, 80, 0)
led_hinten.np[0] = led_hinten.np[3] = (255, 80, 0)
else:
led_vorne.np[0] = led_vorne.np[3] = (0, 0, 0)
led_hinten.np[0] = led_hinten.np[3] = (0, 0, 0)
# Middle LEDs
if licht_an:
led_vorne.np[1] = led_vorne.np[2] = (255, 255, 255)
led_hinten.np[1] = led_hinten.np[2] = (128, 0, 0)
else:
led_vorne.np[1] = led_vorne.np[2] = (0, 0, 0)
led_hinten.np[1] = led_hinten.np[2] = (0, 0, 0)
led_vorne.np.write()
led_hinten.np.write()
blinker_timer.init(period=500, mode=Timer.PERIODIC, callback=blink_warn)
SHIPPET 7 Brake light
from leds import LEDController
import time
led_vorne = LEDController("LED1") # Front LED
led_hinten = LEDController("LED2") # Rear LED
# Stop effects
led_hinten.repeat_count = 0
# Read light status from FRONT (unambiguous!)
licht_war_an = led_vorne.np[1] != (0, 0, 0) # Light was on
# Brake lights to full brightness
led_hinten.np[1] = (255, 0, 0)
led_hinten.np[2] = (255, 0, 0)
led_hinten.np.write()
time.sleep(2)
# Back to previous state
if licht_war_an:
led_hinten.np[1] = (128, 0, 0) # Dimmed (50%)
led_hinten.np[2] = (128, 0, 0)
else:
led_hinten.np[1] = (0, 0, 0) # Off
led_hinten.np[2] = (0, 0, 0)
led_hinten.np.write()
SHIPPET 8 Reverse light
from leds import LEDController
led_hinten = LEDController("LED2") # Rear LED
# Stop effects
led_hinten.repeat_count = 0
# Reverse lights white 75%
led_hinten.np[1] = (191, 191, 191)
led_hinten.np[2] = (191, 191, 191)
led_hinten.np.write()
SHIPPET 9 Horn flash
from leds import LEDController
import time
led_vorne = LEDController("LED1") # Front LED
# Stop effects
led_vorne.repeat_count = 0
# Only save middle LEDs (LED 1 and 2)
led1_alt = led_vorne.np[1] # LED1 old
led2_alt = led_vorne.np[2] # LED2 old
# Flash 2x (only middle LEDs)
for i in range(2):
led_vorne.np[1] = (255, 255, 255)
led_vorne.np[2] = (255, 255, 255)
led_vorne.np.write()
time.sleep(0.15)
led_vorne.np[1] = (0, 0, 0)
led_vorne.np[2] = (0, 0, 0)
led_vorne.np.write()
time.sleep(0.1)
# Restore
led_vorne.np[1] = led1_alt
led_vorne.np[2] = led2_alt
led_vorne.np.write()
All shippets were assigned to corresponding ‘actions’ on the transmitter
(e.g. turn signals on 3-way switch / horn flash button / reverse light & brake light motor control / hazard lights button (long press))
Works quite decently so far,
except that the response time of the control is significantly slower which leads to a certain delay!
Nevertheless usable for lighting control. (Maybe someone with more experience has the possibility to optimize the code?)
That’s all from me about the CyberBrick App!
Regards, Birdy