import machine
from machine import Pin, PWM
import time
test_led = machine.Pin(2, machine.Pin.OUT)
"""""for i in range (1,11):
blueled.value(0)
time.sleep(0.5)
blueled.value(1)
time.sleep(0.5)
print("done!")"""
infra = Pin(11, Pin.IN)
servo = PWM(Pin(12, mode=Pin.OUT))
servo.freq(50)
"""i = 0
#servo just rotates 5 times
while True:
servo.value(26)
time.sleep(1)
servo.value(123)
time.sleep(1)
i += 1
if i == 5:
break"""
"""
#checking with LED
while True:
if infra.value() == 1:
print("moved")
blueled.value(1) #high to LED
else:
print("still")
blueled.value(0) #LED off
time.sleep(0.1) #short delay"""
"""#checking the simulated motion and changing the servo rotation
while True:
if infra.value() == 1:
print("movement detected")
servo.duty(123) #rotate to 180 degrees (123)
#rotate 90 degrees (75)
time.sleep(1)
servo.duty(23) #back to starting position
time.sleep(1)
else:
print("no movement detected")
servo.duty(26)
time.sleep(0.5)"""
# State variable to track movement detection
movement_detected = False
# Main loop
while True:
if infra.value() == 1 and not movement_detected:
print("Movement detected")
servo.duty(123) # Rotate to 180 degrees
test_led.value(1) # Turn on LED
movement_detected = True # Mark movement as detected
time.sleep(1) # Optional delay
elif infra.value() == 0 and movement_detected:
print("No movement detected, ready to detect again")
servo.duty(26) # Back to starting position
test_led.value(0) # Turn off LED
movement_detected = False # Reset detection flag
time.sleep(0.1) # Small delay to avoid excessive CPU usageLoading
esp32-s3-devkitc-1
esp32-s3-devkitc-1