- Muchas notas - Fran Acién

20250507 - Coding pirates micropython workshop

Knight rider

Challenge Description:
Create a back-and-forth sweeping light effect like the Knight Rider car, triggered by a button press.

NUM_LEDS = 12

light_sequence = [0, 1, 2, 3, 4, 5, 11, 10, 9, 8, 7, 6]
reversed_light_sequence = [6, 7, 8, 9, 10, 11, 5, 4, 3, 2, 1, 0]

def clearLeds():
    for i in range(NUM_LEDS):
        bitbot.set_pixel_color(i, 0x000000)

def knight_rider():
    # Move light from left to right
    for led_num in light_sequence:
        bitbot.set_pixel_color(led_num, 0xff0000)
        basic.pause(50) # 0.05s
        clearLeds()

    for rev_led_num in reversed_light_sequence:
        bitbot.set_pixel_color(rev_led_num, 0xff0000)
        basic.pause(50) # 0.05s
        clearLeds()

def on_button_pressed_a():
    for i in range(5):
        knight_rider()

input.on_button_pressed(Button.A, on_button_pressed_a)


def blink():
    bitbot.set_pixel_color(0, 0xff0000)
    control.wait_micros(100000)
    bitbot.set_pixel_color(0, 0x000000)
    control.wait_micros(100000)

def on_button_pressed_b():
    for i in range(10):
        blink()

input.on_button_pressed(Button.B, on_button_pressed_b)

def on_forever():
    pass
basic.forever(on_forever)

Color wheel

Animate a spinning rainbow effect across the LEDs, rotating the colors like a color wheel when a button is pressed

NUM_LEDS = 12

# Predefined rainbow color list (must match NUM_LEDS length)
rainbow_colors = [
    0xFF0000,  # Red
    0xFF7F00,  # Orange
    0xFFFF00,  # Yellow
    0x00FF00,  # Green
    0x00FFFF,  # Cyan
    0x0000FF,  # Blue
    0x8B00FF,  # Violet
    0xFF1493,  # Deep Pink
    0x00FA9A,  # Medium Spring Green
    0xFFD700,  # Gold
    0x40E0D0,  # Turquoise
    0xDA70D6,  # Orchid
]

def clearLeds():
    for i in range(NUM_LEDS):
        bitbot.set_pixel_color(i, 0x000000)

def rainbow_wheel(wait_ms, steps):
    colors = rainbow_colors[:]
    for j in range(steps):
        for i in range(NUM_LEDS):
            bitbot.set_pixel_color(i, colors[i])
        basic.pause(wait_ms)

        # Rotate colors right by 1
        last_color = colors.pop()
        colors.insert(0, last_color)

    clearLeds()

def on_button_pressed_a():
    rainbow_wheel(100, 100)  # 100ms delay, 100 steps

input.on_button_pressed(Button.A, on_button_pressed_a)

def on_forever():
    pass
basic.forever(on_forever)

Color chase

Make every third light turn on in a fun moving pattern when you press a button — like dancing lights taking turns!

NUM_LEDS = 12

def clearLeds():
    for i in range(NUM_LEDS):
        bitbot.set_pixel_color(i, 0x000000)

def theater_chase(color, wait_ms):
    for a in range(10):  # repeat 10 times
        for b in range(3):  # phase offset: 0, 1, 2
            clearLeds()
            for c in range(b, NUM_LEDS, 3):
                bitbot.set_pixel_color(c, color)
            basic.pause(wait_ms)

def on_button_pressed_a():
    theater_chase(0x00FF00, 100)  # Green chase with 100ms delay

def on_button_pressed_b():
    theater_chase(0xFF0000, 100)  # Green chase with 100ms delay

input.on_button_pressed(Button.A, on_button_pressed_a)
input.on_button_pressed(Button.B, on_button_pressed_b)

def on_forever():
    pass
basic.forever(on_forever)

Using light sensor

Print the value of the light sensor using the function serial.write_line()

def on_forever():
    light_value = bitbot.read_light(BBLightSensor.LEFT)
    serial.write_line(str(light_value))
    basic.pause(500)  # wait half a second before the next reading

basic.forever(on_forever)

Read the light sensor value and adjust the LED brightness proportionally — lower light results in higher LED brightness.

NUM_LEDS = 12  # Number of LEDs
MAX_BRIGHTNESS = 300


# Converts a brightness value (0.0 to 1.0) to a white color
def brightness_to_color(brightness):
    value = int(255 * brightness)
    return (value << 16) | (value << 8) | value  # 0xRRGGBB

def on_forever():
    light_value = bitbot.read_light(BBLightSensor.LEFT)
    serial.write_line("Light value: " + str(light_value))

    # Calculate brightness from darkness
    brightness = (MAX_BRIGHTNESS - light_value) / MAX_BRIGHTNESS
    brightness = max(0, min(brightness, 1))  # clamp between 0 and 1

    color = brightness_to_color(brightness)

    for i in range(NUM_LEDS):
        bitbot.set_pixel_color(i, color)

    basic.pause(100)  # update 10 times per second

basic.forever(on_forever)

Challenges 20 May

  • Turn on the lights when is dark
  • Line follower
  • Light History Logger : Print the light level to the serial monitor every 500 ms
  • Use the accelerometer to activate lights

Example code:

def on_forever():
    left = bitbot.read_light(BBLightSensor.LEFT)
    right = bitbot.read_light(BBLightSensor.RIGHT)
    threshold = 150  # Adjust based on your surface and lighting

    if left < threshold and right < threshold:
        # Both sensors on dark line → go forward
        bitbot.goms(BBDirection.FORWARD, 60, 0)
    elif left < threshold and right >= threshold:
        # Left on line, right off line → turn left
        bitbot.goms(BBDirection.LEFT, 40, 0)
    elif right < threshold and left >= threshold:
        # Right on line, left off line → turn right
        bitbot.goms(BBDirection.RIGHT, 40, 0)
    else:
        # Both off the line → stop
        bitbot.stop(BBStopMode.BRAKE)

    basic.pause(50)

basic.forever(on_forever)