Skip to content

Instantly share code, notes, and snippets.

@Babbleshack
Last active August 29, 2016 14:25
Show Gist options
  • Select an option

  • Save Babbleshack/2f63aaf55bb3c095cff300c04b9ea1d4 to your computer and use it in GitHub Desktop.

Select an option

Save Babbleshack/2f63aaf55bb3c095cff300c04b9ea1d4 to your computer and use it in GitHub Desktop.
Arduino takes 5 readings and averages them before writing to serial.
#/usr/bin/python3
"""
Arduino makes 5 readings and averages those readings,
the sensor is able to make 20 readings a second with 20 uS
delay.
This script therefore gets 4 readings a second and draws them to a plot
"""
import serial
from matplotlib import pyplot as plt
from matplotlib import animation
ser = serial.Serial(port='/dev/ttyACM0', baudrate=115200); #change this to use
#port you use (prolly COM1)
fig = plt.figure(figsize=(10,7)) # create figure (window)
ac1 = fig.add_subplot(1,1,1); # create subplot(x=1,y=1,numberOfSubplots=1) (this is the plot we will draw on)
def getDatum():
return int(serial.readline()) # read one line deliminated by '\n' and cast to int
def getData():
data = [] #create new data list
for i in range(0,4): # get four readings
datum = int(ser.readline())
print(datum)
data.append(datum)
return data
def animate(i):
data = getData()
ac1.clear()
ac1.grid(True)
ac1.plot(data)
anim = animation.FuncAnimation(fig, animate, interval=1000); #interval controls drawing interval in microsecs
#plt.grid(True)
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment