import machine
import time
# Pin Definitions
TRIG_PIN_1 = 27 # First ultrasonic sensor trig pin
ECHO_PIN_1 = 26 # First ultrasonic sensor echo pin
TRIG_PIN_2 = 7 # Second ultrasonic sensor trig pin
ECHO_PIN_2 = 6 # Second ultrasonic sensor echo pin
SWITCH_PIN = 10 # Sliding switch pin
BUZZER_PIN = 16 # Piezo buzzer pin
# GPIO Setup
switch = machine.Pin(SWITCH_PIN, machine.Pin.IN, machine.Pin.PULL_DOWN)
buzzer = machine.Pin(BUZZER_PIN, machine.Pin.OUT)
# Mock Distance Function for Wokwi
def get_mock_distance(mode):
"""
Simulates different distances for testing purposes.
Indoor Mode: Returns a random value between 50 and 150 cm.
Outdoor Mode: Returns a random value between 150 and 500 cm.
"""
if mode == 0: # Indoor
return 75 # Simulate a distance of 75 cm
else: # Outdoor
return 403 # Simulate a distance of 403 cm
# Main Program
def main():
while True:
# Read mode from the sliding switch
mode = switch.value() # 0 = Indoor, 1 = Outdoor
mode_text = "Indoor" if mode == 0 else "Outdoor"
threshold = 90 if mode == 0 else 180 # Threshold for Sensor 2
# Simulate distance using mock function
distance = get_mock_distance(mode)
# Determine if the buzzer should beep
if distance < threshold:
buzzer.on()
buzzer_status = f"Buzzer ON - Object Detected at {distance:.2f} cm"
else:
buzzer.off()
buzzer_status = f"Buzzer OFF - No Object Detected (Distance: {distance:.2f} cm)"
# Display result in terminal
print(f"Mode: {mode_text} | {buzzer_status}")
time.sleep(1) # Wait before the next reading
# Run Main Program
try:
main()
except KeyboardInterrupt:
print("Program stopped")
buzzer.off() # Turn off buzzer when exiting