Alright, so following your suggestions I've modified my two sets of code. The problem I'm having now is that Processing is not receiving the data. My code creates a nice text file in the sketch directory, but the text file is empty. I think it's a problem with how I'm printing my BPM, but I can't be sure whether I've set up Processing correctly in order to receive data. Any help would be appreciated!
New Processing code:
import processing.serial.*;
Serial port;
PrintWriter output;
int BPM; // HOLDS HEART RATE VALUE FROM ARDUINO
void setup() {
// Create a new file in the sketch directory
output = createWriter("BPM.txt");
// GO FIND THE ARDUINO
println(Serial.list()); // print a list of available serial ports
// choose the number between the [] that is connected to the Arduino
port = new Serial(this, Serial.list()[1], 115200); // make sure Arduino is talking serial at this baud rate
port.bufferUntil('\n'); // set buffer full flag on receipt of carriage return
}
void serialEvent(Serial port){
String inData = port.readStringUntil('\n');
inData = trim(inData); // cut off white space (carriage return)
BPM = int(inData); // convert the string to usable int
// port.write(1); // Try to write to serial port to test if Processing can communicate with correct serial port
}
void draw() { // Could use void loop() here instead?
output.println(BPM); // Write the BPM to the file
}
void keyPressed() {
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}]
The only part of the old Arduino code that I modified was this part (the void loop()):
void loop(){
// sendDataToProcessing('S', Signal); // send Processing the raw Pulse Sensor data
// if (QS == true){ // Quantified Self flag is true when arduino finds a heartbeat
// fadeRate = 255; // Set 'fadeRate' Variable to 255 to fade LED with pulse
// sendDataToProcessing('B',BPM); // send heart rate with a 'B' prefix
// sendDataToProcessing('Q',IBI); // send time between beats with a 'Q' prefix
// QS = false; // reset the Quantified Self flag for next time
// }
//
// ledFadeToBeat();
//sendDataToProcessing('B', BPM); // send Processing the raw Pulse Sensor data
Serial.println(BPM); // Trying again to print to Serial
delay(100); // take a break
}