1414import socketpool
1515import wifi
1616import board
17+ import digitalio
1718import alarm
1819import neopixel
1920import adafruit_hcsr04
2223import adafruit_requests
2324import adafruit_max1704x
2425
26+ # Initialize the power pin for the sensor
27+ sensor_power = digitalio .DigitalInOut (board .A2 )
28+ sensor_power .direction = digitalio .Direction .OUTPUT
29+ sensor_power .value = False # Start with sensor powered off
30+
31+ def power_sensor_on ():
32+ """Turn on power to the ultrasonic sensor and wait for it to stabilize."""
33+ sensor_power .value = True
34+ time .sleep (0.55 ) # Give sensor time to power up and stabilize
35+
36+ def power_sensor_off ():
37+ """Turn off power to the ultrasonic sensor."""
38+ sensor_power .value = False
39+
2540# Initialize the sonar sensor
2641sonar = adafruit_hcsr04 .HCSR04 (trigger_pin = board .A0 , echo_pin = board .A1 )
2742
4661
4762# Operating hours (24-hour format with minutes, e.g., "6:35" and "16:00")
4863OPENING_TIME = "6:00"
49- CLOSING_TIME = "22 :30"
64+ CLOSING_TIME = "15 :30"
5065# Normal operation check interval
51- NORMAL_CHECK_MINUTES = 5
66+ NORMAL_CHECK_MINUTES = 10
5267# Sleep duration in seconds during operating hours
5368SLEEP_DURATION = 60 * NORMAL_CHECK_MINUTES
5469# Display duration in seconds
@@ -64,6 +79,7 @@ def parse_time(time_str):
6479
6580def get_average_distance ():
6681 """Take multiple distance readings and return the average."""
82+ power_sensor_on () # Power on the sensor before taking measurements
6783 distances = []
6884 for _ in range (NUM_SAMPLES ):
6985 try :
@@ -73,6 +89,7 @@ def get_average_distance():
7389 except RuntimeError :
7490 print ("Error reading distance" )
7591 continue
92+ power_sensor_off () # Power off the sensor after measurements
7693
7794 # Only average valid readings
7895 if distances :
@@ -248,6 +265,7 @@ def set_pixel_color(distance):
248265 sleep_seconds = SLEEP_DURATION # Use normal interval if we couldn't get readings
249266
250267# Prepare for deep sleep
268+ power_sensor_off () # Make sure sensor is powered off before sleep
251269pixel .brightness = 0 # Turn off NeoPixel
252270
253271# Flush the serial output before sleep
0 commit comments