import time
from machine import Pin
# LED and button setup
led_white = Pin(1, Pin.OUT)
led_yellow = Pin(3, Pin.OUT)
led_red = Pin(7, Pin.OUT)
led_blue = Pin(13, Pin.OUT)
btn_white = Pin(28, Pin.IN, Pin.PULL_UP)
btn_yellow = Pin(21, Pin.IN, Pin.PULL_UP)
btn_red = Pin(17, Pin.IN, Pin.PULL_UP)
btn_blue = Pin(16, Pin.IN, Pin.PULL_UP)
# Helper function to turn off all LEDs
def turn_off_all_leds():
led_white.value(0)
led_yellow.value(0)
led_red.value(0)
led_blue.value(0)
# Function to turn on all LEDs
def turn_on_all_leds():
led_white.value(1)
led_yellow.value(1)
led_red.value(1)
led_blue.value(1)
# Function to blink LEDs in sequence
def blink_in_sequence():
leds = [led_white, led_yellow, led_red, led_blue]
for led in leds:
led.value(1)
time.sleep(0.5) # Delay for 0.5 seconds
led.value(0)
# Main function
def main():
print("LED Control Program")
print("Commands: TURN ON, TURN OFF, BLINK, EXIT")
while True:
# Get user input
command = input("Enter command: ").strip().upper()
if command == "TURN ON":
turn_on_all_leds()
print("All LEDs are turned ON.")
elif command == "TURN OFF":
turn_off_all_leds()
print("All LEDs are turned OFF.")
elif command == "BLINK":
print("LEDs are blinking in sequence. Press Ctrl+C to stop.")
try:
while True:
blink_in_sequence()
except KeyboardInterrupt:
print("Blinking stopped.")
turn_off_all_leds()
elif command == "EXIT":
turn_off_all_leds()
print("Exiting the program. Goodbye!")
break
else:
print("Invalid command. Please try again.")
# Check for button presses to control individual LEDs
if btn_white.value() == 0:
print("White LED toggled via button.")
led_white.toggle()
time.sleep(0.2) # Debounce delay
if btn_yellow.value() == 0:
print("Yellow LED toggled via button.")
led_yellow.toggle()
time.sleep(0.2)
if btn_red.value() == 0:
print("Red LED toggled via button.")
led_red.toggle()
time.sleep(0.2)
if btn_blue.value() == 0:
print("Blue LED toggled via button.")
led_blue.toggle()
time.sleep(0.2)
# Run the program
main()