Skip to content

Commit 6cf9790

Browse files
Kaan OzenKaan Ozen
authored andcommitted
fix(vl53l1x): Address review comments on proximity alert example.
1 parent 67b0689 commit 6cf9790

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""Proximity alert using VL53L1X distance sensor and buzzer.
2+
3+
Beeps the buzzer when an object is detected within 200 mm. The pitch
4+
increases as the object gets closer.
5+
"""
6+
17
from time import sleep_ms
28

39
from machine import I2C, Pin
@@ -6,11 +12,11 @@
612

713
DISTANCE_THRESHOLD_MM = 200
814

9-
# sensor configuration
15+
# Sensor configuration
1016
i2c = I2C(1)
1117
tof = VL53L1X(i2c)
1218

13-
#buzzer initialisation
19+
# Buzzer initialisation
1420
buzzer_tim = Timer(1, freq=1000)
1521
buzzer_ch = buzzer_tim.channel(4, Timer.PWM, pin=Pin("SPEAKER"))
1622
buzzer_ch.pulse_width_percent(0)
@@ -21,19 +27,19 @@
2127
print("Distance: {} mm".format(distance))
2228

2329
if distance < DISTANCE_THRESHOLD_MM:
24-
#frequency ranging from 1500 (really close) to 500 (at 200mm)
25-
freq = (1500- (distance * 5))
26-
#security limits
30+
# Frequency ranging from 1500 (really close) to 500 (at 200mm)
31+
freq = 1500 - (distance * 5)
32+
# Security limits
2733
freq = max(500, min(1500, freq))
2834

2935
buzzer_tim.freq(freq)
3036
buzzer_ch.pulse_width_percent(50)
3137
else:
3238
buzzer_ch.pulse_width_percent(0)
3339

34-
3540
sleep_ms(50)
3641

37-
3842
except KeyboardInterrupt:
39-
buzzer_ch.pulse_width_percent(0) #Properly cut the sound when the user presses CTLR+C
43+
pass
44+
finally:
45+
buzzer_ch.pulse_width_percent(0)

0 commit comments

Comments
 (0)