Skip to content

Instantly share code, notes, and snippets.

@kijoyin
Last active August 19, 2018 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kijoyin/276b0c91801b9f1807fb20119440442b to your computer and use it in GitHub Desktop.
Save kijoyin/276b0c91801b9f1807fb20119440442b to your computer and use it in GitHub Desktop.
import time
import datetime
import RPi.GPIO as GPIO
def calculateSpeed():
global start
done = time.time()
elapsed = done - start
elaspesedMinute = elapsed*0.01666668
rpm = 1/elaspesedMinute
distance = rpm* 22
speed = distance/1
speedKmh = speed*.0006
print(speedKmh)
start = done
def detectSensor(channel):
# Called if sensor output changes
timestamp = time.time()
stamp = datetime.datetime.fromtimestamp(timestamp).strftime('%H:%M:%S')
if not GPIO.input(channel):
calculateSpeed()
def main():
detectSensor(14)
try:
# Loop until users quits with CTRL-C
while True :
time.sleep(0.1)
except KeyboardInterrupt:
# Reset GPIO settings
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
print("Setup GPIO pin as input on GPIO14")
GPIO.setup(14 , GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(14, GPIO.BOTH, callback=detectSensor, bouncetime=200)
start = time.time()
if __name__=="__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment