from machine import Pin
from time import sleep
import uasyncio
# Inputs
pin_DLR = Pin(0, mode=Pin.IN, pull=Pin.PULL_DOWN) # 1. Daytime Running lights input
pin_Side = Pin(1, mode=Pin.IN, pull=Pin.PULL_DOWN) # 2. Sidelights input
pin_Dip = Pin(2, mode=Pin.IN, pull=Pin.PULL_DOWN) # 3. Dipped beam input
pin_Brake = Pin(3, mode=Pin.IN, pull=Pin.PULL_DOWN) # 4. Brake lights input
pin_Indicator_L = Pin(4, mode=Pin.IN, pull=Pin.PULL_DOWN) # 5. Indicate left input
pin_Indicator_R = Pin(5, mode=Pin.IN, pull=Pin.PULL_DOWN) # 6. Indicate Right input
pin_Hazard = Pin(6, mode=Pin.IN, pull=Pin.PULL_DOWN) # 7. Hazards input
pin_Reverse = Pin(7, mode=Pin.IN, pull=Pin.PULL_DOWN) # 8. Reversing input
pin_Main = Pin(21, mode=Pin.IN, pull=Pin.PULL_DOWN) # pb. Main beam input
# Outputs
pou_DLR = Pin(8, mode=Pin.OUT) # Daytime rinning lights output
pou_Side = Pin(9, mode=Pin.OUT) # Sidelights output
pou_Dip = Pin(10, mode=Pin.OUT) # Dipped beam output
pou_Brake = Pin(11, mode=Pin.OUT) # Brake lights output
pou_Indicator_L = Pin(12, mode=Pin.OUT) # Left indicators output
pou_Indicator_R = Pin(13, mode=Pin.OUT) # Right indicators output
pou_Reverse = Pin(14, mode=Pin.OUT) # Reverse output
pou_Main = Pin(15, mode=Pin.OUT) # Main beam output
async def IndicatorController():
while True:
# Indicators on
pou_Indicator_L.value(pin_Indicator_L.value() + pin_Hazard.value())
pou_Indicator_R.value(pin_Indicator_R.value() + pin_Hazard.value())
# Wait 0.5s
await uasyncio.sleep_ms(500)
# Indicators off
pou_Indicator_L.value(0)
pou_Indicator_R.value(0)
# Wait 0.5s
await uasyncio.sleep_ms(500)
async def LightController():
HighBeam = False
while True:
# Set the output value of the DLR
pou_DLR.value(pin_DLR.value())
# Set the output value of the Sidelights
pou_Side.value(pin_Side.value())
# Set the output value of the Dipped beam
pou_Dip.value(pin_Dip.value())
# Set the output value of the Brake lights
pou_Brake.value(pin_Brake.value())
# Set the output value of the Reverse Lights
pou_Reverse.value(pin_Reverse.value())
# Check if the dipped beam are on, if so toggle the main beam else pulse the beam to match the switch/button
if pin_Main.value() == 1:
HighBeam = not HighBeam
if pin_Main.value() == 0 and pin_Dip.value() == 0:
HighBeam = False
pou_Main.value(HighBeam)
await uasyncio.sleep_ms(1)
async def main():
uasyncio.create_task(IndicatorController())
uasyncio.create_task(LightController())
while True:
await uasyncio.sleep(1)
uasyncio.run(main())