Commit 0263650d authored by P.M.P.C Bandara's avatar P.M.P.C Bandara

New Distance measurement file added

parent 68f9a4c1
import RPi.GPIO as GPIO
import time
import signal
import sys
import pandas as pd
from csv import writer
# use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BCM)
from datetime import date
today = date.today()
# set GPIO Pins
pinTrigger = 18
pinEcho = 25
t=1
def close(signal, frame):
print("\nTurning off ultrasonic distance detection...\n")
GPIO.cleanup()
sys.exit(0)
signal.signal(signal.SIGINT, close)
# set GPIO input and output channels
GPIO.setup(pinTrigger, GPIO.OUT)
GPIO.setup(pinEcho, GPIO.IN)
while t==1:
# set Trigger to HIGH
field_names = ['Date', 'Height']
GPIO.output(pinTrigger, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(pinTrigger, False)
startTime = time.time()
stopTime = time.time()
# save start time
while 0 == GPIO.input(pinEcho):
startTime = time.time()
# save time of arrival
while 1 == GPIO.input(pinEcho):
stopTime = time.time()
# time difference between start and arrival
TimeElapsed = stopTime - startTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
distancef = 60.0-distance
print ("Distance: %.1f cm" % distancef)
time.sleep(1)
row_contents = [today,distancef]
with open('new.csv','a+',newline='') as write_obj:
csv_writer = writer(write_obj)
csv_writer.writerow(row_contents)
print('a')
t=t+1
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment