Commit 68f9a4c1 authored by P.M.P.C Bandara's avatar P.M.P.C Bandara

Distance 2

parent 435bcf06
import RPi.GPIO as GPIO
import time
import signal
import sys
import pygsheets
import pandas as pd
#authorization
gc = pygsheets.authorize(service_file='/Users/erikrood/desktop/QS_Model/creds.json')
df = pd.DataFrame()
# use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BCM)
// Include the Servo library
#include <Servo.h>
// Declare the Servo pin
int servoPin = 13;
// Create a servo object
Servo Servo1;
# set GPIO Pins
pinTrigger = 18
pinEcho = 25
t=1
SCOPES = ['AKfycbwETCQ_Kz6KOTWnQj5y1_xczrK6FVbYuhko9UZMM_kHUblLl-lddju9wz30J5p7EkHfpg']
def close(signal, frame):
print("\nTurning off ultrasonic distance detection...\n")
GPIO.cleanup()
sys.exit(0)
signal.signal(signal.SIGINT, close)
// Make servo go to 90 degrees
Servo1.write(90);
delay(1000);
Servo1.write(0);
delay(1000);
# set GPIO input and output channels
GPIO.setup(pinTrigger, GPIO.OUT)
GPIO.setup(pinEcho, GPIO.IN)
while t==1:
# set Trigger to HIGH
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 = 55.3-distance
df['height'] = [distancef]
#open the google spreadsheet (where 'PY to Gsheet Test' is the name of my sheet)
sh = gc.open('PY to Gsheet Test')
#select the first sheet
wks = sh[0]
#update the first sheet with df, starting at cell B2.
wks.set_dataframe(df,(1,1))
print ("Distance: %.1f cm" % distancef)
time.sleep(1)
t=t+1
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