The control of the WS2812B RGB LED headlight modules is done with this script. Here the functions lights(x) for the low beam, highBeam(x) for the high beam, breakLight(x) for the brake light, reverseLight(x) for the reverse light and indicator(y) for the turn signal are available. X can take the values 0 and 1 (off & on). Y, i.e. the function of the indicators, can take over the values 0, 1, 2 and 3 (Off, Left, Right, hazard lights).
from microbit import *
import neopixel
from utime import ticks_ms, ticks_diff
np = neopixel.NeoPixel(pin0, 8)
headlights = (0, 3)
backlights = (5, 6)
led_white = (60, 60, 60)
led_red = (60, 0, 0)
led_off = (0, 0, 0)
led_red_br = (255, 0, 0)
led_orange = (100, 35, 0)
indicator_left = (1, 4)
indicator_right = (2, 7)
indicator_warning = (1, 2, 4, 7)
def lightsON():
for x in headlights:
np[x] = led_white
for x in backlights:
np[x] = led_red
np.show()
def lightsOFF():
for x in headlights:
np[x] = led_off
for x in backlights:
np[x] = led_off
np.show()
def lightsBreakON():
temp = np[backlights[1]]
for x in backlights:
np[x] = led_red_br
np.show()
return (temp)
def lightsBreakOFF():
for x in backlights:
np[x] = led_off
np.show()
def lightsBackON():
temp = np[backlights[0]]
np[backlights[0]] = led_white
np.show()
return (temp)
def lightsBackOFF():
np[backlights[0]] = led_off
np.show()
def lightsIndicator(direction, last_ind_act):
if ticks_diff(ticks_ms(), last_ind_act) >= 400 and np[direction[0]] == led_off:
for x in direction:
np[x] = led_orange
np.show()
return ticks_ms()
elif ticks_diff(ticks_ms(), last_ind_act) >= 400 and np[direction[0]] == led_orange:
for x in direction:
np[x] = led_off
np.show()
return ticks_ms()
else:
return last_ind_act
while True:
# Application example
lightsON() #Light on
sleep(1000)
lightsBreakON() #Brake light on
sleep(1000)
lightsBreakOFF() #Brake light off
sleep(1000)
lightsBackON() #Reversing lights on
sleep(1000)
lightsBackOFF() #Reversing lights off
sleep(1000)
lightsOFF() #Light off
sleep(1000)