I need to graph the data of a sensor in python and when the figure is opened it does not respond. Is there a problem in the code?

1

I need to graph in real time the values I read from a sensor connected to a mega arduino. This is the arduino code:

void setup() {
Serial.begin(19200);
}

void loop() {

int num = analogRead(A0);
Serial.println(num);
delay(50);
}

and the code in python is as follows:

import serial,sys,time # import Serial Library
import matplotlib.pyplot as plt #import matplotlib library
import matplotlib.animation as animation

try:
    arduino =serial.Serial('COM4',115200)
except:
    print("Conecte el Arduino en el COM designado")
    print("Terminando programa...")
    try:
       arduino.close()
    except:
      print('No se cerro el puerto correctamente')
      pass
      sys.exit()

 fig  = plt.figure()
 ax1 = fig.add_subplot(111)
 xs = []
 ys= []
 cnt = 0
 time.sleep(2)

 def animate(i):
   datos = arduino.readlines()
   x= float(datos)
   y= time.clock()
   xs.append(x)
   ys.append(y)
   ax1.clear()
   ax1.plot(xs,ys)
   cnt=cnt+1
   if cnt > 50:
      xs.pop(0)
      ys.pop(0)
   plt.pause(0.00001)

 ani = animation.FuncAnimation(fig,animate, interval = 500)
 plt.show()

When I run the program, the figure window does not respond or shows any graph.

Thanks in advance

    
asked by Mario Andres Gomez 18.02.2018 в 02:53
source

0 answers