from machine import Pin
import time
# Car class to control headlight (LED) and horn (buzzer)
class Car:
def __init__(self):
# Set up LED and buzzer pins as output
self.led = Pin(2, Pin.OUT)
self.buzzer = Pin(15, Pin.OUT)
# Set up buttons as input with pull-up resistors
self.led_button = Pin(0, Pin.IN, Pin.PULL_UP)
self.buzzer_button = Pin(4, Pin.IN, Pin.PULL_UP)
def control_lights_and_horn(self):
# Check LED button, and toggle the LED state
if not self.led_button.value():
self.led.on()
print("LED ON - Headlight")
time.sleep(1)
self.led.off()
print("LED OFF - Headlight")
# Check buzzer button, and toggle the buzzer (horn)
if not self.buzzer_button.value():
self.buzzer.on()
print("Buzzer ON - Horn")
time.sleep(0.5)
self.buzzer.off()
print("Buzzer OFF - Horn")
my_car = Car()
while True:
my_car.control_lights_and_horn()
time.sleep(0.1)