import machine
from machine import Pin
import time
# Initialize the pins for the LEDs
green_led = Pin(21, Pin.OUT)
yellow_led = Pin(22, Pin.OUT)
red_led = Pin(23, Pin.OUT)
leds = [green_led, yellow_led, red_led]
#green_led --> leds[0]
# Initialize the pins for the buttons
green_button = Pin(25, Pin.IN, Pin.PULL_UP)
yellow_button = Pin(26, Pin.IN, Pin.PULL_UP)
red_button = Pin(27, Pin.IN, Pin.PULL_UP)
white_button = Pin(14, Pin.IN, Pin.PULL_UP)
# Define a variable to keep track of the LED control mode
manual_control = False
# Define the timer callback functions to turn off the LEDs
def turnOff_redled(timer):
red_led.off()
def turnOff_yellowled(timer):
yellow_led.off()
def turnOff_greenled(timer):
green_led.off()
# Define the timer object with a period of 500 milliseconds (0.5 seconds)
timer = machine.Timer(0)
def turn_greenLED():
green_led.on()
timer.deinit()
red_led.off()
yellow_led.off()
timer.init(period=1000, mode=machine.Timer.ONE_SHOT, callback=turn_redLED)
def turn_yellowLED():
yellow_led.on()
timer.deinit()
red_led.off()
green_led.off()
timer.init(period=1000, mode=machine.Timer.ONE_SHOT, callback=turn_greenLED)
def turn_redLED():
red_led.on()
timer.deinit()
green_led.off()
yellow_led.off()
timer.init(period=1000, mode=machine.Timer.ONE_SHOT, callback=turn_yellowLED)
# Turn on the automatic LED control by default
turn_redLED()
n = 0
# Loop forever
while True:
if not white_button.value():
# Change the LED control mode
manual_control = not manual_control
if manual_control:
print("Manual LED control enabled")
timer.deinit()
for led in leds:
led.off()
else:
print("Automatic LED control enabled")
turn_redLED()
time.sleep(0.5)
if manual_control :
if green_button.value() == 0 :
green_led.toggle()
if yellow_button.value() == 0 :
yellow_led.toggle()
if red_button.value() == 0 :
red_led.toggle()
else :
# Turn on green LED for 1 second
if green_button.value() == 0 :
turn_greenLED()
# Turn on yellow LED for 2 seconds
elif yellow_button.value() == 0 :
turn_yellowLED()
# Turn on Red LED for 3 seconds
elif red_button.value() == 0 :
turn_redLED()
if n % 5000 == 0:
print(f" n = {n}")
n += 1