-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpollution.py
More file actions
53 lines (43 loc) · 1.28 KB
/
pollution.py
File metadata and controls
53 lines (43 loc) · 1.28 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
import serial
import matplotlib.pyplot as plt
from drawnow import *
import atexit
values = []
plt.ion()
cnt=0
serialArduino = serial.Serial('PORT NAME', BAUD RATE)
def plotValues():
plt.title('Serial value from Arduino')
plt.grid(True)
plt.ylabel('Values')
plt.plot(values, 'rx-', label='values')
plt.legend(loc='upper right')
def doAtExit():
serialArduino.close()
print("Close serial")
print("serialArduino.isOpen() = " + str(serialArduino.isOpen()))
atexit.register(doAtExit)
print("serialArduino.isOpen() = " + str(serialArduino.isOpen()))
#pre-load dummy data
for i in range(0,26):
values.append(0)
while True:
while (serialArduino.inWaiting()==0):
pass
print("readline()")
valueRead = serialArduino.readline(500)
#check if valid value can be casted
try:
valueInInt = int(valueRead)
print(valueInInt)
if valueInInt <= 1024:
if valueInInt >= 0:
values.append(valueInInt)
values.pop(0)
drawnow(plotValues)
else:
print("Invalid! negative number")
else:
print("Invalid! too large")
except ValueError:
print("Invalid! cannot cast")