from machine import Pin
from time import sleep_ms

led_rouge = Pin(19, Pin.OUT)
led_vert = Pin(20, Pin.OUT)
led_jaune = Pin(18, Pin.OUT)

direction = Pin(0, Pin.OUT)
step = Pin(1, Pin.OUT)
ms1 = Pin(4, Pin.OUT)
ms2 = Pin(3, Pin.OUT)
ms3 = Pin(2, Pin.OUT)
enable = Pin(5, Pin.OUT)       

button_rouge = Pin(13, Pin.IN, Pin.PULL_UP)     
button_vert = Pin(12, Pin.IN, Pin.PULL_UP)      
button_jaune = Pin(11, Pin.IN, Pin.PULL_UP)     
button_stop = Pin(9, Pin.IN, Pin.PULL_UP)       
button_blanc = Pin(10, Pin.IN, Pin.PULL_UP)     

compteur = 0                                    


capteur_gauche = Pin(8, Pin.IN, Pin.PULL_UP)     
capteur_droite = Pin(7, Pin.IN, Pin.PULL_UP)     

def tourner_sens_horaire():
    direction.value(1)
    step.value(1)
    led_vert.value(1)
    sleep_ms(delay)
    step.value(0)
    sleep_ms(delay)
        
def tourner_sens_trigonometrique():
    direction.value(0)
    step.value(1)
    led_rouge.value(1)
    sleep_ms(delay)
    step.value(0)
    sleep_ms(delay)

def LED_eteintes():
    led_vert.value(0)
    led_rouge.value(0)

def eteindre_driver():
    enable.value(1)       

def allumer_driver():
    enable.value(0)       

    
led_rouge.value(0)
led_vert.value(0)
led_jaune.value(0)
    
direction.value(0)
step.value(0)
ms1.value(0)
ms2.value(0)
ms3.value(0)
enable.value(0)       
    
delay = 1       
pas = 0



while True:  
    if button_jaune.value() == 0:
        print("EMERGENCY STOP!! You need to reset the machine")
        eteindre_driver()
        led_jaune.value(1)
        compteur = 1
    if button_blanc.value() == 0:
        print("Machine reset")
        allumer_driver()
        led_jaune.value(0)
        compteur = 0
    if button_rouge.value() == 0 and button_vert.value() == 1 and compteur == 0:
        while not capteur_gauche.value() == 0 and not button_jaune.value() == 0 and not button_stop.value() ==0:
            tourner_sens_trigonometrique()
            pas -= 1
            degre = pas * 360/200
            print("tourner_sens_trigonométrique():", pas, "not, which corresponds to", degre, "°" )
    if button_rouge.value() == 1 and button_vert.value() == 0 and compteur == 0:
        while not capteur_droite.value() == 0 and not button_jaune.value() == 0 and not button_stop.value() ==0:
            tourner_sens_horaire()
            pas += 1
            degre = pas * 360/200
            print("tourner_sens_horaire():", pas, "not, which corresponds to", degre, "°" )
    else:
        LED_eteintes()
    
    if button_stop.value() == 0:
        print("STOP")

    if capteur_gauche.value() == 0:
        print("STOP!! Left sensor engaged")
    if capteur_droite.value() == 0:
        print("STOP!! Left sensor engaged")
    
    if button_rouge.value() == 0 and compteur == 1:
        print("You need to reset (white button)")
    if button_vert.value() == 0 and compteur == 1:
        print("You need to reset (white button)")
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
pico:GP0
pico:GP1
pico:GND.1
pico:GP2
pico:GP3
pico:GP4
pico:GP5
pico:GND.2
pico:GP6
pico:GP7
pico:GP8
pico:GP9
pico:GND.3
pico:GP10
pico:GP11
pico:GP12
pico:GP13
pico:GND.4
pico:GP14
pico:GP15
pico:GP16
pico:GP17
pico:GND.5
pico:GP18
pico:GP19
pico:GP20
pico:GP21
pico:GND.6
pico:GP22
pico:RUN
pico:GP26
pico:GP27
pico:GND.7
pico:GP28
pico:ADC_VREF
pico:3V3
pico:3V3_EN
pico:GND.8
pico:VSYS
pico:VBUS
r1:1
r1:2
r2:1
r2:2
led1:A
led1:C
led2:A
led2:C
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
A4988
drv1:ENABLE
drv1:MS1
drv1:MS2
drv1:MS3
drv1:RESET
drv1:SLEEP
drv1:STEP
drv1:DIR
drv1:GND.1
drv1:VDD
drv1:1B
drv1:1A
drv1:2A
drv1:2B
drv1:GND.2
drv1:VMOT
stepper1:A-
stepper1:A+
stepper1:B+
stepper1:B-
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
led3:A
led3:C
r3:1
r3:2
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r
btn5:1.l
btn5:2.l
btn5:1.r
btn5:2.r
btn6:1.l
btn6:2.l
btn6:1.r
btn6:2.r
btn7:1.l
btn7:2.l
btn7:1.r
btn7:2.r