# Import libraries
from machine import Pin
from time import sleep
# Car class definition
class Car:
def __init__(self, light_pin, horn_pin, light_button_pin, horn_button_pin):
# Initialize the LED, Buzzer, and buttons
self.light = Pin(light_pin, Pin.OUT)
self.horn = Pin(horn_pin, Pin.OUT)
self.light_button = Pin(light_button_pin, Pin.IN, Pin.PULL_UP)
self.horn_button = Pin(horn_button_pin, Pin.IN, Pin.PULL_UP)
def light_control(self):
if self.light_button.value() == 0:
self.light.on()
else:
self.light.off()
def horn_control(self):
if self.horn_button.value() == 0:
self.horn.on()
else:
self.horn.off()
# Setup GPIO pins
light_pin = 2 # Pin for LED (light)
horn_pin = 15 # Pin for Buzzer (horn)
light_button_pin = 0 # Pin for button to control light
horn_button_pin = 4 # Pin for button to control horn
# Create an instance of the Car class
my_car = Car(light_pin, horn_pin, light_button_pin, horn_button_pin)
# Main loop to check button presses and control light/horn
while True:
my_car.light_control() # Check light button and control light
my_car.horn_control() # Check horn button and control horn
sleep(0.1) # Short delay to debounce the buttons