import machine
import utime

oneboard_led = machine.Pin(25, machine.Pin.OUT)

green_led = machine.Pin(2, machine.Pin.OUT)
orange_led = machine.Pin(6, machine.Pin.OUT)
red_led = machine.Pin(10, machine.Pin.OUT)

humain_green_led = machine.Pin(14, machine.Pin.OUT)
humain_red_led = machine.Pin(11, machine.Pin.OUT)

BP1 = machine.Pin(18, machine.Pin.IN)

def is_on():
    for _ in range(3):
        oneboard_led.value(1)
        utime.sleep(0.5)
        oneboard_led.value(0)
        utime.sleep(0.5)

def traffic_light(state):
    if state == 1:
        green_led.value(1)
        orange_led.value(0)
        red_led.value(0)
        humain_red_led.value(1)
        humain_green_led.value(0)
        # print("GREEN : Stop humains go cars !")
    elif state == 2:
        green_led.value(0)
        orange_led.value(1)
        red_led.value(0)
        humain_red_led.value(1)
        humain_green_led.value(0)
        # print("ORANGE : Stop humains go cars !")
    elif state == 3:
        green_led.value(0)
        orange_led.value(0)
        red_led.value(1)
        humain_red_led.value(0)
        humain_green_led.value(1)
        # print("RED : Stop cars go humains !")
    else:
        print("The traffic_light is not working !")
        green_led.value(0)
        orange_led.value(0)
        red_led.value(0)

def normal_function():
    traffic_light(1)
    utime.sleep(5)

    traffic_light(2)
    utime.sleep(2)

    traffic_light(3)
    utime.sleep(5)

    traffic_light(2)
    utime.sleep(2)

def pressed_button():
    traffic_light(2)
    utime.sleep(2)

    traffic_light(3)
    print("green Humains !")
    utime.sleep(5)

    traffic_light(2)
    utime.sleep(2)

def main():
    is_on()

    previous_state = 0

    while True:
        """print(BP1.value())
        if BP1.value() == 0:
            normal_function()
        elif BP1.value() == 1:
            pressed_button()"""
        current_state = BP1.value()
        
        # Detect a press-and-release
        if previous_state == 0 and current_state == 1:
            print("Bouton pressed!")
            pressed_button()
        
        previous_state = current_state
        
        if current_state == 0:
            normal_function()

if __name__ == "__main__":
  main()



BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT