Thrown-together program to collect data from an arduino, Workshop W20,
Summer '11 AAPT meeting, Omaha Nebraska.
This program is matched to the Arduino program 'senddata.pde'.
Program should be called with two parameters: the number of points to
collect (at a rate of 10/s) and the filename where the data should be saved.
Plotting the data is done by default, but can be commented out in the last
import serial, sys, time
import pylab as pl
port = '/dev/tty.usbmodem621'
N = int(sys.argv)
print '255 points max: 255 requested from Arduino.'
filename = sys.argv
# start the serial port
ser = serial.Serial(port, 57600, timeout=2)
# The following line is necessary to give the arduino time to start
# accepting stuff.
times = pl.zeros([N])
measurements = pl.zeros([N])
# Tell the Arduino how many points to grab
# the number will be sent as a char, thus the limit of 255.
# Now grab that many
for i in range(N):
# read a line from the serial port
line = ser.readline()
# split the line into milliseconds and value
brokenline = line.split()
times[i] = int(brokenline)
measurements[i] = int(brokenline)
# change times to seconds (currently in millis())
times = pl.array([(times[i]-times)/1000.0 for i in range(N)])
# Save the data
file = open(filename, 'w')
for j in range(N):
file.write('%f\t%d\n' % (times[j], measurements[j]) )
# plot data
pl.plot(times, measurements, 'ro-')
pl.ylabel('Raw A/D value (10-bit)')
Generated by GNU enscript 1.6.4.