14
14
import socketpool
15
15
import wifi
16
16
import board
17
+ import digitalio
17
18
import alarm
18
19
import neopixel
19
20
import adafruit_hcsr04
22
23
import adafruit_requests
23
24
import adafruit_max1704x
24
25
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
+
25
40
# Initialize the sonar sensor
26
41
sonar = adafruit_hcsr04 .HCSR04 (trigger_pin = board .A0 , echo_pin = board .A1 )
27
42
46
61
47
62
# Operating hours (24-hour format with minutes, e.g., "6:35" and "16:00")
48
63
OPENING_TIME = "6:00"
49
- CLOSING_TIME = "22 :30"
64
+ CLOSING_TIME = "15 :30"
50
65
# Normal operation check interval
51
- NORMAL_CHECK_MINUTES = 5
66
+ NORMAL_CHECK_MINUTES = 10
52
67
# Sleep duration in seconds during operating hours
53
68
SLEEP_DURATION = 60 * NORMAL_CHECK_MINUTES
54
69
# Display duration in seconds
@@ -64,6 +79,7 @@ def parse_time(time_str):
64
79
65
80
def get_average_distance ():
66
81
"""Take multiple distance readings and return the average."""
82
+ power_sensor_on () # Power on the sensor before taking measurements
67
83
distances = []
68
84
for _ in range (NUM_SAMPLES ):
69
85
try :
@@ -73,6 +89,7 @@ def get_average_distance():
73
89
except RuntimeError :
74
90
print ("Error reading distance" )
75
91
continue
92
+ power_sensor_off () # Power off the sensor after measurements
76
93
77
94
# Only average valid readings
78
95
if distances :
@@ -248,6 +265,7 @@ def set_pixel_color(distance):
248
265
sleep_seconds = SLEEP_DURATION # Use normal interval if we couldn't get readings
249
266
250
267
# Prepare for deep sleep
268
+ power_sensor_off () # Make sure sensor is powered off before sleep
251
269
pixel .brightness = 0 # Turn off NeoPixel
252
270
253
271
# Flush the serial output before sleep
0 commit comments