Code snippet for LED

Has anybody been successful at getting an LED to work using code?

I have tried several examples that people have suggested but none of them work.
This is the last one I found:

led_controller = LEDController(“LED1”)

led_controller.set_led_effect(0, 1000, 5, 0b0001, 0xFF0000)

led_controller.timing_proc()

Do I have to add anything before or after this code to trigger it?
I am using a short click on a push button to trigger the code.

This will work.

Swap the 20 to 21 to swap between left and right connections.

import machine
import neopixel

pin = machine.Pin(20, machine.Pin.OUT)
np = neopixel.NeoPixel(pin, 4)

np[0] = (255,0,0)
np[1] = (0,255,0)
np[2] = (0,0,255)
np[3] = (255,255,255)
np.write()

It is important to note, the examples are extremely lacking and clearly little to no interest has been put in to help the community.

1 Like

Thank you so much. I have been tearing what’s left of my hair out.

1 Like

Nature beat me to that one.

I am a baldy.

2 Likes

Is there an LED off option in the code?

Yes, I gave it to you already, you are going to kick yourself.

You set the colours.

You set the colours, think about it.

As 255 is 100% on, 0 is 100% off.

When you turn all three triplet values to zero, the LED has no illuminated light.

1 Like

DOH . It has been a longer day than I thought :slight_smile:

1 Like

Does this work for 2 lights? If not, how do I make it work for 2 lights?

Assuming you mean two hubs, then you have everything there you need to write it for two, prepare to go doh!

import machine
import neopixel

// hub 1
pin = machine.Pin(21, machine.Pin.OUT)
np = neopixel.NeoPixel(pin, 4)

np[0] = (255,0,0)
np[1] = (0,255,0)
np[2] = (0,0,255)
np[3] = (255,255,255)
np.write()

// hub 2
pin = machine.Pin(20, machine.Pin.OUT)
np = neopixel.NeoPixel(pin, 4)

np[0] = (0,0,255)
np[1] = (0,255,0)
np[2] = (255,0,0)
np[3] = (0,0,0)
np.write()

Que codigo es el que se usa? MicroPython?

Hello! I am a complete beginner with this system and wanted to ask if there is a way to make the LEDs flash alternately in blue and red? I am looking for a code that I can enter 1:1 into the code in the Cyberbrick software. At the moment, I can only set one effect at a time, but unfortunately not a sequence.
In my case, I have the LED strip on LED2 and only 2 LEDs on positions 1+2. Is there anyone who can help me with this? Thank you very much.

Answered by @MaKim.

1 Like

Hi what i did wrong it did not work i try when i long press the button that is connect on k1 to start my 2led on 0 and 1 on the ws2812 to flash red and blue then when press again it will turn off. this is my code i try to use but didnt work. thx

import time
import machine
import neopixel

=== CONFIGURATION ===

PIN_NUM = 4 # Pin connected to the LED data line (NeoPixel)
NUM_LEDS = 4 # Number of WS2812 LEDs to flash (2 LEDs)
BUTTON_PIN = 6 # GPIO pin for K1 button on the transmitter (change accordingly)

Initialize NeoPixel strip

pin = machine.Pin(20, machine.Pin.OUT)
np = neopixel.NeoPixel(pin, 4)

Initialize button (K1)

button = machine.Pin(6, machine.Pin.IN, machine.Pin.PULL_UP) # Assuming pull-up resistor

Helper to set all LEDs to one color

def set_color(led, r, g, b):
np[led] = (r, g, b)
np.write()

Function to flash the LEDs

def flash_leds():
while True:
set_color(0, 255, 0, 0) # Red on first LED
set_color(1, 0, 0, 255) # Blue on second LED
time.sleep(0.5) # Flash delay
set_color(0, 0, 0, 0) # Turn off first LED
set_color(1, 0, 0, 0) # Turn off second LED
time.sleep(0.5) # Flash delay
# You can add a condition to stop flashing or break after a time if needed.

Main loop

while True:
button_pressed = not button.value() # Button is pressed when value is low (active low)

if button_pressed:
    # Start timer to check if it's a long press
    press_start_time = time.ticks_ms()
    
    # Wait until button is released or time exceeds long press threshold
    while not button.value():  # Still pressed
        if time.ticks_ms() - press_start_time > 2000:  # Long press threshold (2 seconds)
            # Trigger long press action (start flashing LEDs)
            flash_leds()
            break
        time.sleep(0.05)  # Small delay to avoid constant checking

# If the button is not pressed, just continue with regular actions or colors
time.sleep(0.1)

A couple of questions:

  • How is this code written to the board and what is the trigger for it to run?
  • As opposed to a loop monitoring the button state, would you consider using the visual CyberBrick input configuration to instead trigger a code snippet based on button state?

sorry i am new too this i try to use chat gpt to program it but did not work. but i wa able to make it work with this code but its always on now i want to integrate a button too it
import machine
import neopixel

pin = machine.Pin(20, machine.Pin.OUT)
np = neopixel.NeoPixel(pin, 4)

np[0] = (255,0,0)
np[1] = (0,255,0)
np[2] = (0,0,255)
np[3] = (255,255,255)
np.write()

As a general note, LLMs don’t give very good output for cyberbrick. This is because they try to treat them like standard ESP32 devices - even with lots of coaxing and instruction.

The ESP32 ecosystem is large and CyberBrick is much smaller - as a result, LLMs will probably cause you more issues than they will help. As an alternative, I would recommend reading through the API documentation for CyberBrick.

If your project requirements differ quite a bit from what cyberbrick offers, you may be better served by using a general purpose board. I love CyberBrick but it’s not always going to be the most appropriate choice for every project.

If you are staying within the cyberbrick ecosystem, I find that it works best to start from visual configuration for inputs and outputs in the UI. If you can’t accomplish what you need with those visual tools, try using code snippets with the absolute bare minimum actions taking place in each snippet. Note that each snippet runs in isolation and without global variables.

ok thanks for the advice. I will check on this

I am MalcGPT.

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​​​​​​​​​​​​​​​​

2 Likes

Very night to have the snippets with the logic. The snippets appear to work here. But I am a bit confused how you assigned them to the different buttons.
for example
thee way switch: 1: “Left turn signal” 0: ??? II: “Right turn signal”
L shoulder button: short hold: horn flash

How do you turn on/oof the lights?