from machine import Pin
import time
# Define pins for button and LEDs
button_pin = 12
red_pin = 1
yellow_pin = 5
green_pin = 9
# Initialize Pins
button = Pin(button_pin, Pin.IN)
red = Pin(red_pin, Pin.OUT)
yellow = Pin(yellow_pin, Pin.OUT)
green = Pin(green_pin, Pin.OUT)
# Initialize previous state of the button
prev_button_state = 0
# Initialize current state for LED sequence
led_state = 0 # 0: Red, 1: Yellow, 2: Green
# Turn off all LEDs at the beginning
red.off()
yellow.off()
green.off()
while True:
# Read the current state of the button
current_button_state = button.value()
# Check if the button state has changed
if current_button_state != prev_button_state:
# If the button is pressed (transition from 1 to 0)
if current_button_state == 0:
# Toggle LEDs based on current state
if led_state == 0: # Red LED state
red.on()
yellow.off()
green.off()
led_state = 1 # Move to the next state (Yellow)
elif led_state == 1: # Yellow LED state
red.off()
yellow.on()
green.off()
led_state = 2 # Move to the next state (Green)
elif led_state == 2: # Green LED state
red.off()
yellow.off()
green.on()
led_state = 0 # Move back to the first state (Red)
time.sleep(0.2) # Debounce time to avoid multiple toggles
prev_button_state = current_button_state