from machine import Pin, time_pulse_us
import time
import random
from gate import set_angle
TRIG_PIN = 5
ECHO_PIN = 18
trig = Pin(TRIG_PIN, Pin.OUT)
echo = Pin(ECHO_PIN, Pin.IN)
def measure_distance():
trig.value(0)
time.sleep_us(2)
trig.value(1)
time.sleep_us(10)
trig.value(0)
try:
duration = time_pulse_us(echo, 1, 30000)
except OSError:
return -1
if duration < 0:
return -1
return (duration * 0.0343) / 2
# Main Loop
while True:
real_dist = measure_distance()
random_dist = random.randint(1, 40)
confidence = random.randint(0, 100)
if real_dist > 0:
distance = (real_dist + random_dist) / 2
print("Distance =", distance, "| Confidence =", confidence)
if distance < 20 and confidence >= 50:
print("Gate Open")
set_angle(90)
else:
print("Gate Closed")
set_angle(0)
else:
print("Sensor Error -> Gate Closed")
set_angle(0)
time.sleep(1)