Created
July 19, 2018 21:16
-
-
Save aula9/54cf1325f2a5390295f4ddbd6a48265e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter as tk | |
import RPi.GPIO as GPIO | |
import time | |
GPIO.setwarnings(False) | |
GPIO.setmode(GPIO.BOARD) | |
TRIGPIN = 11 | |
ECHO = 15 | |
print ("Distance Measurement In Progress") | |
GPIO.setup(TRIGPIN,GPIO.OUT) | |
GPIO.setup(ECHO, GPIO.IN, pull_up_down= GPIO.PUD_DOWN) | |
root = tk.Tk() | |
root.title("Calculating Distance") | |
GPIO.output(TRIGPIN, False) | |
print ("Delay for sensor stability") | |
time.sleep(2) | |
GPIO.output(TRIGPIN, True) | |
time.sleep(0.00001) | |
GPIO.output(TRIGPIN, False) | |
while GPIO.input(ECHO)==0: | |
pulse_start= 0 | |
pulse_start= time.time() | |
while GPIO.input(ECHO)==1: | |
pulse_end= 0 | |
pulse_end= time.time() | |
duration = pulse_end-pulse_start | |
distance = duration * 34029 | |
distance = distance / 2 | |
distance = round(distance, 2) | |
print ("Distance:",distance,"cm") | |
label = tk.Label(root,width=40, fg="yellow" , bg = "purple") | |
label.config(font=("Courier", 36)) | |
label.config(text=str(distance)) | |
label.pack() | |
button = tk.Button(root, text='Stop', width=50, command=root.destroy) | |
button.pack() | |
root.mainloop() | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment