MicroPython Lights
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 *
from neopixel import *
from utime import ticks_ms, ticks_diff
np = 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(self, headlights, led_white, backlights, led_red):
for x in self.headlights:
np[x] = self.led_white
for x in self.backlights:
np[x] = self.led_red
np.show()
def lightsOFF(np, headlights, backlights, led_off):
for x in headlights:
np[x] = led_off
for x in backlights:
np[x] = led_off
np.show()
def lightsBreakON(np, backlights, led_red_br, led_backlights_status):
temp = np[backlights[1]]
for x in backlights:
np[x] = led_red_br
np.show()
return(temp)
def lightsBreakOFF(np, led_backlights_status, backlights):
for x in backlights:
np[x] = led_backlights_status
np.show()
def lightsBackON(np, backlights, led_white, led_backlights_status):
temp = np[backlights[0]]
np[backlights[0]] = led_white
np.show()
return(temp)
def lightsBackOFF(np, backlights, led_backlights_status):
np[backlights[0]] = led_backlights_status
np.show()
def lightsIndicator(direction, last_ind_act, led_off, led_orange, np):
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