Advertisement
Guest User

Untitled

a guest
Aug 26th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. import serial
  2. import matplotlib.pyplot as plt
  3. from drawnow import *
  4.  
  5. values = []
  6.  
  7. plt.ion()
  8. cnt=0
  9.  
  10. serialArduino = serial.Serial('COM3', 9600)
  11.  
  12. def plotValues():
  13. plt.title('Serial value from Arduino')
  14. plt.grid(True)
  15. plt.ylabel('Values')
  16. plt.plot(values, 'rx-', label='values')
  17. plt.legend(loc='upper right')
  18.  
  19. #pre-load dummy data
  20. for i in range(0,26):
  21. values.append(0)
  22.  
  23. while True:
  24. while (serialArduino.inWaiting()==0):
  25. pass
  26. valueRead = serialArduino.readline()
  27.  
  28. #check if valid value can be casted
  29. try:
  30. valueInInt = int(valueRead)
  31. print(valueInInt)
  32. if valueInInt <= 1024:
  33. if valueInInt >= 0:
  34. values.append(valueInInt)
  35. values.pop(0)
  36. drawnow(plotValues)
  37. else:
  38. print ("Invalid! negative number")
  39. else:
  40. print ("Invalid! too large")
  41. except ValueError:
  42. print ("Invalid! cannot cast")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement