

import numpy
from matplotlib.pyplot import *
import matplotlib.animation as animation
from arduinoHX711 import *

ard = Arduino("COM3")


fe = 10.0
duree = 100
te = 1/fe
N = int(duree/te)

t = numpy.linspace(0,duree,N)
F = numpy.zeros(N)

fig,ax = subplots()
line0, = ax.plot(t,F)
ax.grid()
ax.set_xlabel("t (s)")
ax.set_ylabel("F (mg)")


ard.start_transmission()
n=0
temps = numpy.array([])
force = numpy.array([])
t = 0.0
def animate(i):
    global ax,line0,ard,n,N,te,temps,force,t
    if n<N:
        f = ard.lecture()
        temps = numpy.append(temps,t)
        force = numpy.append(force,f)
        t += te
        n += 1
        line0.set_xdata(temps)
        line0.set_ydata(force)
        ax.axis([0,temps[n-1],force.min(),force.max()])

ani = animation.FuncAnimation(fig,animate,N,interval=te*1000*0.8)
show()
ard.stop_transmission()
ard.close()
numpy.savetxt("force-1.txt",numpy.array([temps,force]).T,delimiter="\t",fmt="%.6e",header="t (s)\t F (mg)")   		 
    		 
        