# Import hardware control libraries
from machine import Pin, ADC, PWM
import time
# Initialize ADC for the potentiometers on GPIO 27 and 28
pot1 = ADC(Pin(27))
pot2 = ADC(Pin(28))
# Initialize Digital Output pins for the LEDs on GPIO 0 and 1
led1 = Pin(0, Pin.OUT)
led2 = Pin(1, Pin.OUT)
# Initialize PWM for buzzers on GPIO 10 and 11 to control sound
buzzer1 = PWM(Pin(10))
buzzer2 = PWM(Pin(11))
# Function to turn off all outputs (LEDs and sound)
def silence():
led1.value(0) # Turn off LED 1
led2.value(0) # Turn off LED 2
buzzer1.duty_u16(0) # Set buzzer 1 volume to 0
buzzer2.duty_u16(0) # Set buzzer 2 volume to 0
# Main execution loop
while True:
# Read analog values from potentiometers (range 0-65535)
val1 = pot1.read_u16()
val2 = pot2.read_u16()
# Print values to the Serial Monitor
print(f"Pot1: {val1} | Pot2: {val2}")
# Condition 1: If both potentiometers are below a certain threshold, stay silent
if val1 < 20000 and val2 < 20000:
silence()
# Condition 2: If Pot 1 is significantly higher than Pot 2
elif val1 > (val2 + 1000):
led1.value(1) # Turn on LED 1
led2.value(0) # Turn off LED 2
buzzer1.freq(1000) # Set buzzer 1 frequency to 1000Hz
buzzer1.duty_u16(32768) # Play sound
buzzer2.duty_u16(0) # Mute buzzer 2
# Condition 3: If Pot 2 is significantly higher than Pot 1
elif val2 > (val1 + 1000):
led1.value(0) # Turn off LED 1
led2.value(1) # Turn on LED 2
buzzer2.freq(1500) # Set buzzer 2 frequency to 1500Hz
buzzer2.duty_u16(32768) # Play sound
buzzer1.duty_u16(0) # Mute buzzer 1
# If values are too close, turn everything off
else:
silence()
time.sleep(0.1)