# Project 2: Controlling a LED with a push button
import network
from machine import Pin
import utime
# Create a function to send data when a button is pressed (optional)
red_button_pin = Pin(33, Pin.IN)
yellow_button_pin = Pin(32, Pin.IN)
green_button_pin = Pin(35, Pin.IN)
blue_button_pin = Pin(34, Pin.IN)
sw_button_pin = Pin(25, Pin.IN) # Operation mode: Normal or DR
red_led = Pin(5, Pin.OUT)
yellow_led = Pin(18, Pin.OUT)
green_led = Pin(19, Pin.OUT)
blue_led = Pin(21, Pin.OUT)
last_red_button_state = 0 # Assuming the button is not pressed initially
last_yellow_button_state = 0 # Assuming the button is not pressed initially
last_green_button_state = 0 # Assuming the button is not pressed initially
last_blue_button_state = 0 # Assuming the button is not pressed initially
print("started")
while True:
# update the leds state
red_led_state = red_led.value()
yellow_led_state = yellow_led.value()
green_led_state = green_led.value()
blue_led_state = blue_led.value()
# update the buttons state
current_red_button_state = red_button_pin.value()
current_yellow_button_state = yellow_button_pin.value()
current_green_button_state = green_button_pin.value()
current_blue_button_state = blue_button_pin.value()
if current_red_button_state != last_red_button_state:
if red_led_state == 0 and current_red_button_state == 1:
red_led.on()
print("Red LED On")
elif red_led_state == 1 and current_red_button_state == 1:
red_led.off()
print("Red LED Off")
red_led_state = red_led.value()
# Update the last button state
last_red_button_state = current_red_button_state
if current_yellow_button_state != last_yellow_button_state:
if yellow_led_state == 0 and current_yellow_button_state == 1:
yellow_led.on()
print("Yellow LED On")
elif yellow_led_state == 1 and current_yellow_button_state == 1:
yellow_led.off()
print("Yellow LED Off")
yellow_led_state = yellow_led.value()
# Update the last button state
last_yellow_button_state = current_yellow_button_state
if current_green_button_state != last_green_button_state:
if green_led_state == 0 and current_green_button_state == 1:
green_led.on()
print("Green LED On")
elif green_led_state == 1 and current_green_button_state == 1:
green_led.off()
print("Green LED Off")
green_led_state = green_led.value()
# Update the last button state
last_green_button_state = current_green_button_state
if current_blue_button_state != last_blue_button_state:
if blue_led_state == 0 and current_blue_button_state == 1:
blue_led.on()
print("Blue LED On")
elif blue_led_state == 1 and current_blue_button_state == 1:
blue_led.off()
print("Bue LED Off")
blue_led_state = blue_led.value()
# Update the last button state
last_blue_button_state = current_blue_button_state