print("Hello, Pi Pico!")
from machine import Pin
import utime
import time
Pb_Switch = Pin(15, Pin.IN, Pin.PULL_DOWN)
trigger = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
relay = Pin(16, Pin.OUT)
def Pb_Switch_INT(pin):
global Pb_Switch_State
global warning_LED
global interrupt_occurred
Pb_Switch.irq(handler=None)
if (Pb_Switch.value() == 1) and (Pb_Switch_State == 0):
Pb_Switch_State = 1
warning_LED.value(1)
interrupt_occurred = True
relay.value(0)
print("Interrupt Enabled")
elif (Pb_Switch.value() == 0) and (Pb_Switch_State == 1):
Pb_Switch_State = 0
warning_LED.value(0)
interrupt_occurred = False
print("Interrupt Disabled")
Pb_Switch.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=Pb_Switch_INT)
def ultra():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0343) / 2
print("The distance from the object is ", distance, "cm")
if distance < 100:
relay.value(1)
else:
relay.value(0)
Pb_Switch.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=Pb_Switch_INT)
Pb_Switch_State = Pb_Switch.value()
warning_LED = Pin(28, Pin.OUT)
warning_LED.value(0)
interrupt_occurred = False
while True:
if not interrupt_occurred:
ultra()
utime.sleep(1)