from picozero import Speaker
from machine import Pin, PWM
from time import sleep
# Speaker setup
speaker = Speaker(15)
# Servo setup on GPIO Pin 0 using PWM
servo_pin = Pin(0)
servo_pwm = PWM(servo_pin)
servo_pwm.freq(50)
def set_servo_angle(angle):
# Convert angle to duty cycle
min_duty = 1000 # 1 ms pulse width
max_duty = 9000 # 2 ms pulse width
duty = int(min_duty + (angle / 180.0) * (max_duty - min_duty))
servo_pwm.duty_u16(duty)
# Switch setup (assuming GPIO pins 10 to 15 for switches)
switch_pins = [10, 11, 12, 13, 14, 15]
switches = [Pin(pin, Pin.IN, Pin.PULL_UP) for pin in switch_pins]
# Define angles for each lock position
lock_angles = [0, 30, 60, 90, 120, 150] # Adjust angles as needed
def rotate_to_lock(lock_index):
angle = lock_angles[lock_index]
set_servo_angle(angle)
sleep(0.5) # Adjust as needed for servo movement time
# Initial position
current_lock = 0
rotate_to_lock(current_lock)
def beep_speaker(duration, frequency):
speaker.play(frequency, duration)
while True:
# Check each switch
print("Entering switch check loop")
for i in range(6):
if not switches[i].value(): # If switch is pressed (active low)
print("Switch", i, "pressed")
if i != current_lock: # If not already at this lock
rotate_to_lock(i)
current_lock = i
# Play a beep to indicate lock change
beep_speaker(0.1, 700) # Adjust frequency and duration as needed
sleep(0.5) # Pause after beep
# Buzzer logic to beep every 1 second
beep_speaker(0.1, 700)
sleep(1)