-
Notifications
You must be signed in to change notification settings - Fork 1
/
objectspeedcallback.py
73 lines (63 loc) · 1.66 KB
/
objectspeedcallback.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import config
from log import log
import time
from gpiozero import Button
logFile = log(config.logFilePath)
first = Button(config.firstInput)
last = Button(config.lastInput)
firstTime = None
def firstEvent():
"""
Notes time when an object passes in front of the first light sensor
"""
try:
global firstTime
if firstTime == None:
firstTime = time.time()
except:
print(f"firstEvent Error; firstTime: {firstTime}")
def lastEvent():
"""
Compares current time to time when object passed in front of the first light sensor and calculates the speed
"""
try:
global firstTime
if firstTime != None:
#print("bottom activated")
timeDiff = time.time() - firstTime
final = config.lightSensorDistance/timeDiff/12
print(timeDiff)
# conversions
if (config.conversion == 'fpm'):
final *= 60
print("fpm")
elif (config.conversion == 'mph'):
final = final * (3600/5280)
print("mph")
elif (config.conversion != 'fps'):
final = config.customConversion(final)
print("custom")
final = round(final, 2)
newArr = logFile.read("all")
newArr.insert(0,{"value": final, "time": log.time()})
logFile.write("all", newArr)
print(final)
firstTime = None
except:
print(f"lastEvent Error; firstTime: {firstTime}")
def setup():
# """
# Checks to see if an object is in front of the light sensors. Runs associated function/s
# """
last.when_pressed = lastEvent
first.when_pressed = firstEvent
def reset(x):
global firstTime
if (firstTime != None):
if (time.time() - firstTime > x):
print("firstTime back to None")
firstTime = None
else:
firstTime = firstTime
# except:
# print(f'reset Error; firstTime {firstTime}')