/
hc-sr04_distance_sensor.py
74 lines (61 loc) · 2.19 KB
/
hc-sr04_distance_sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Copyright (c) 2020 Alan Yorinks All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
Version 3 as published by the Free Software Foundation; either
or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
import sys
import time
from telemetrix import telemetrix
"""
This program continuously monitors an HC-SR04 Ultrasonic Sensor
It reports changes to the distance sensed.
"""
TRIGGER_PIN = 9
ECHO_PIN = 10
# indices into callback data
REPORT_TYPE = 0
TRIG_PIN = 1
DISTANCE = 2
TIME = 3
# A callback function to display the distance
def the_callback(data):
"""
The callback function to display the change in distance
:param data: [report_type = PrivateConstants.SONAR_DISTANCE, trigger pin number, distance, timestamp]
"""
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[3]))
print(f'Sonar Report: Trigger Pin: {data[1]} Distance: {data[2]} Time: {date}')
def sonar(my_board, trigger_pin, echo_pin, callback):
"""
Set the pin mode for a sonar device. Results will appear via the
callback.
:param my_board: a telemetrix instance
:param trigger_pin: Arduino pin number
:param echo_pin: Arduino pin number
:param callback: The callback function
"""
# set the pin mode for the trigger and echo pins
my_board.set_pin_mode_sonar(trigger_pin, echo_pin, callback)
# wait forever
while True:
try:
time.sleep(.01)
except KeyboardInterrupt:
my_board.shutdown()
sys.exit(0)
board = telemetrix.Telemetrix()
try:
sonar(board, TRIGGER_PIN, ECHO_PIN, the_callback)
board.shutdown()
except (KeyboardInterrupt, RuntimeError):
board.shutdown()
sys.exit(0)