Advertisement
orneto

ler o sensor do lab

May 18th, 2023 (edited)
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. import time
  2. import serial
  3. import matplotlib.pyplot as plt
  4. import matplotlib.animation as animation
  5.  
  6. interrupted = False
  7.  
  8. def on_key_press(event):
  9.     global interrupted
  10.     if event.key == 'i':
  11.         interrupted = True
  12.  
  13.  
  14. def animate(i, dataList, ser, frequencia):
  15.     ser.write(frequencia)
  16.     arduinoData_string = ser.readline().decode('ascii')
  17.  
  18.     try:
  19.         arduinoData_float = float(arduinoData_string)
  20.         dataList.append(arduinoData_float)
  21.  
  22.     except:
  23.         pass
  24.  
  25.     dataList = dataList[-50:]
  26.  
  27.     ax.clear()
  28.     ax.plot(dataList)
  29.  
  30.     ax.set_ylim([0, 1200])
  31.     ax.set_title("Leitura do sensor")
  32.     ax.set_ylabel("ValorLido")
  33.  
  34.  
  35. dataList = []
  36.  
  37. fig = plt.figure()
  38. ax = fig.add_subplot(111)
  39.  
  40. ser = serial.Serial("COM6", 9600)
  41. time.sleep(2)
  42. freq=input("digite a frequencia(1, 2, 3, 4, 5): ")
  43. if freq == 1:
  44.     intervalo=200
  45. elif valor == 2:
  46.     intervalo=400
  47. elif valor == 3:
  48.     intervalo = 600
  49. elif valor == 4:
  50.     intervalo = 800
  51. else:
  52.     intervalo=1000
  53. while
  54. ani = animation.FuncAnimation(fig, animate(frequencia=freq), frames=100, fargs=(dataList, ser), interval=intervalo)
  55. fig.canvas.mpl_connect('key_press_event', on_key_press)
  56. plt.show()
  57. ser.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement