# Import Pin from the machine module and sleep from the utime module
from machine import Pin
from utime import sleep_ms

# Set up the button and LED pins
bouton_pin = Pin(2, mode=Pin.IN, pull=Pin.PULL_UP)
led_pin = Pin(15, mode=Pin.OUT)

# Initialize variables
compteur = 0
etat_led = False
etatBoutonSauvegarde = 1

while True:
    # Read the button state
    etatBouton = bouton_pin.value()
    
    # Check for a press (high to low transition)
    if etatBoutonSauvegarde == 1 and etatBouton == 0:
        sleep_ms(20)  # Debounce delay
        compteur += 1  # Increment counter only on press
        print("Compteur =", compteur)
        
        # Toggle LED state
        etat_led = not etat_led
        if etat_led:
            led_pin.on()
        else:
            led_pin.off()

    # Update saved button state
    etatBoutonSauvegarde = etatBouton