Robin2:
Does the Arduino program work properly when it outputs to the Serial Monitor ?
A common problem when people write PC programs is that they do not take account of the fact that the Arduino resets when the PC program opens the serial port. It should only open the serial port at the start and then keep it open until completely finished with the Arduino. As you have only posted a few lines of your Python program I have no idea if that is your problem.
This Python-Arduino demo may give you some ideas.
...R
Hi, thanks for your reply. The Arduino does indeed output perfect data when the ser.readline() function is not called. By perfect, I mean I see one line after another, and just two values separated by a comma. Being generated like that all day. But the second readline is invoked, it skews that data. Indeed, the port is opened at the start of my program, and a short while after, readline is called. Please take a look at this program, in addition to the Arduino code and please help me understand why this is occurring. My background is in mechanics, so I am having a difficult time with computing. Your help is greatly appreciated.
Python program:
import serial
import time
import pylab
import matplotlib.pyplot as plt
import numpy as np
import sys
import os
import csv
from pylab import *
from collections import OrderedDict
#time load
timestr = time.strftime("%Y_%m_%d")
#spacer
spacer = "_"
spacer2 = " "
#user inputs name
last_name = raw_input('Enter last name: ')
first_name = raw_input('Enter first name: ')
name = last_name + first_name
file_name = name + spacer + timestr
file_name_str = file_name+'.csv'
#viewable names
viewable_name = first_name + spacer2 + last_name
viewable_filename = viewable_name + spacer + timestr
#change directory on computer to data folder
path = '/home/pi/Desktop/pulseox/data'
os.chdir(path)
text_file = open(file_name, "w")
full_path = path + "/" + file_name_str
#establish serial connection with ACM0
ser = serial.Serial('/dev/ttyACM0', 115200)
#establish variables
xL = [ ]
bpmL = [ ]
spo2L = [ ]
xL_short = [ ]
bpmL_short = [ ]
spo2L_short = [ ]
array_data = xL, bpmL, spo2L
array_data_short = xL_short, bpmL_short, spo2L_short
#graph attributes
f, (ax1, ax2) = plt.subplots(1, 2, sharey = True)
plt.ion()
#axes
ax1.yaxis.grid()
ax2.yaxis.grid()
#declare zeroth values
x = 0
x_short = 0
ser.readline()
while True:
data_in = ser.readline()
print data_in
data_in = data_in.strip('\n')
bpm,spo2 = data_in.split(",")
bpm_short ,spo2_short = bpm, spo2
#convert string vals to float
x = float(x)
bpm = float(bpm)
spo2 = float(spo2)
x_short = float(x_short)
bpm_short = float(bpm_short)
spo2_short = float(spo2_short)
#print to terminal
print "Time : %s" % (x)
~~ print "HR [BPM]: %s" % (bpm)~~
~~ print "SPO2 [%%]: %s" % (spo2) ~~
~~ print~~
#append vectors
~~ xL.append(x)~~
~~ bpmL.append(bpm)~~
~~ spo2L.append(spo2)~~
~~ xL_short.append(x_short)~~
~~ bpmL_short.append(bpm_short)~~
~~ spo2L_short.append(spo2_short)~~
#print values to plot
~~ ax1.plot(xL_short, bpmL_short, marker = 'o', linestyle = '-', color = 'red' ,label="Pulse Rate (BPM)")~~
~~ ax1.plot(xL_short, spo2L_short, marker = 'o', linestyle = '-', color="blue",label = "SPO2 (%)")~~
~~ ax2.plot(xL, bpmL, marker = 'o', linestyle = '-', color = 'red' ,label="Pulse Rate (BPM)")~~
~~ ax2.plot(xL, spo2L, marker = 'o', linestyle = '-', color="blue",label = "SPO2 (%)")~~
~~ plt.pause(0.1)~~
~~ time.sleep(0.05)~~
#legend
~~ handles, labels = plt.gca().get_legend_handles_labels()~~
~~ by_label = OrderedDict(zip(labels, handles))~~
~~ ax1.legend(by_label.values(),by_label.keys(),loc='lower right',fancybox = True, framealpha = 0.5)~~
~~ ax2.legend(by_label.values(),by_label.keys(),loc='lower right',fancybox = True, framealpha = 0.5)~~
#limit plot1 axis to 30 sec
~~ ax1.set_xlim([0,30])~~
#update time
~~ x = x+1~~
~~ x_short = x_short + 1~~
~~ if x_short >= 30:~~
~~ x_short = 0~~
~~ ax1.cla()~~
~~ xL_short = [ ]~~
~~ bpmL_short = [ ]~~
~~ spo2L_short = [ ]~~
#plt.title("Pulse [BPM] & SPo2 [%] v. Time ", fontsize = "16")
#plt.xlabel("Time ", fontsize = "14")
~~ #plt.ylabel("Pulse (red) [BPM] & SPo2 (blue) [%]", fontsize = "14")
ax1.yaxis.grid()
ax2.yaxis.grid()
#write to .csv~~
~~ with open(full_path, 'w') as f:
writer = csv.writer(f)
for t, b, s in zip(array_data[0], array_data[1], array_data[2]):
writer.writerow([t, b, s])~~