-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPiControl.py
More file actions
72 lines (62 loc) · 2.16 KB
/
PiControl.py
File metadata and controls
72 lines (62 loc) · 2.16 KB
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
#! /usr/bin/env/python
import serial
import time
arduino_port = "/dev/ttyUSB0"
rate = 9600
try:
# Establish connection to the Arduino
arduino_serial = serial.Serial(arduino_port, rate, timeout=5)
arduino_serial.reset_input_buffer()
except Exception as e:
print("Arduino Connection Error")
def dispense(jar, amount):
try:
for x in range(5):
# Send 5 quick messages for the Arduino to pick up
arduino_serial.write(b'd%d%d' % (jar, amount))
arduino_serial.flush()
# Times out assuming that each rotation takes 1 second
dispense_timeout = time.time() + amount + 5
# Loop until the response is received or until 5 seconds
while time.time() < dispense_timeout:
if arduino_serial.inWaiting() > 0:
return True
return False
except Exception:
print("Arduino Connection Error")
return False
def fullness(jar):
try:
# Declare return value
jar_fullness = 0
# Send 5 quick messages for the Arduino to pick up
for x in range(5):
arduino_serial.write(b's%d' % jar)
arduino_serial.flush()
# Times out after five seconds
fullness_timeout = time.time() + 5
# Loop until the response is received or until 5 seconds
while time.time() < fullness_timeout:
if arduino_serial.inWaiting() > 0:
byte_fullness = arduino_serial.read(7)
arduino_serial.flushInput()
arduino_serial.reset_input_buffer()
jar_fullness = float(byte_fullness)
break
return int(jar_fullness)
except Exception:
print("Arduino Connection Error")
return 0
def test():
while True:
query = int(input("Dispense (1) or Get Fullness (2)? Exit (0): "))
if query == 1:
jar = int(input("Enter Jar: "))
amount = int(input("Enter Amount: "))
dispense(jar, amount)
elif query == 2:
jar = int(input("Enter Jar: "))
print(fullness(jar))
else:
arduino_serial.close()
break