-
Notifications
You must be signed in to change notification settings - Fork 75
/
gui_sensor_reading.py
57 lines (46 loc) · 1.41 KB
/
gui_sensor_reading.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
from Tkinter import *
import RPi.GPIO as GPIO
import time
trigger_pin = 18
echo_pin = 23
GPIO.setmode(GPIO.BCM)
GPIO.setup(trigger_pin, GPIO.OUT)
GPIO.setup(echo_pin, GPIO.IN)
def send_trigger_pulse():
GPIO.output(trigger_pin, True)
time.sleep(0.0001)
GPIO.output(trigger_pin, False)
def wait_for_echo(value, timeout):
count = timeout
while GPIO.input(echo_pin) != value and count > 0:
count = count - 1
def get_distance():
send_trigger_pulse()
wait_for_echo(True, 10000)
start = time.time()
wait_for_echo(False, 10000)
finish = time.time()
pulse_len = finish - start
distance_cm = pulse_len / 0.000058
distance_in = distance_cm / 2.5
return (distance_cm, distance_in)
class App:
def __init__(self, master):
self.master = master
frame = Frame(master)
frame.pack()
label = Label(frame, text='Distance (inches)', font=("Helvetica", 32))
label.grid(row=0)
self.reading_label = Label(frame, text='12.34', font=("Helvetica", 110))
self.reading_label.grid(row=1)
self.update_reading()
def update_reading(self):
cm, inch = get_distance()
reading_str = "{:.2f}".format(inch)
self.reading_label.configure(text=reading_str)
self.master.after(500, self.update_reading)
root = Tk()
root.wm_title('Range Finder')
app = App(root)
root.geometry("400x300+0+0")
root.mainloop()