Heart Rate Monitor Updated Processing Sketch

Greetings, Been using the Arduino and Processing sketches that Casey provided on an UNO with a level converter and it worked “OK”. That is until upgrading to Processing 3.5.3… That broke it…

After educational guidance from the giant generous brains at Processing.org, I re-wrote the processing sketch. It works very well. I automated the serial port ID, put the code lines in the sections where they belong, increased the speed to 38400 bps and added one pixel to the Pos++ so it actually adds 2 pixels in the line draw. This smooths out the line. I’ve included the processing code below. Give it a whirl and if you like, replace the example code on Github. This is freeware. Kudos to Casey (even though he moved on) and the folks at Processing forum for pushing me to learn how this works and solving the problem. Note to makers expressing displeasure of high noise: Dno not let the electrode wires lay on your body. I also found that RFI from my Galaxy Note8 causes a squiggly line.

While this project begun as an educational project for me and the grandkids, it has morphed into a most useful device since I had a heart attack in February. I am most appreciative for the free software that has proved beneficial in daily monitoring of my health. NOTE: I fully understand this device and software is for educational purposes only, and are not intended for official medical use. But it is useful. Some info is better than none… When my chest was hurting I hooked myself up. When I saw the rhythm was so different than usual, I called 911. Thank GOD I did.

I’m working on an Android monitor for this via bluetooth or USB OTG.

Thank you!

John

p.s. Sorry for the long winded write-up

************* Arduino Sketch Below ********************

Edited by moderator to add code tags.

/******************************************************************************
Heart_Rate_Display.ino
Demo Program for AD8232 Heart Rate sensor.
Casey Kuhns @ SparkFun Electronics
6/27/2014
https://github.com/sparkfun/AD8232_Heart_Rate_Monitor
The AD8232 Heart Rate sensor is a low cost EKG/ECG sensor. This example shows
how to create an ECG with real time display. The display is using Processing.
This sketch is based heavily on the Graphing Tutorial provided in the Arduino
IDE. http://www.arduino.cc/en/Tutorial/Graph
Resources:
This program requires a Processing sketch to view the data in real time.
Development environment specifics:
IDE: Arduino 1.0.5
Hardware Platform: Arduino Pro 3.3V/8MHz
AD8232 Heart Monitor Version: 1.0
This code is beerware. If you see me (or any other SparkFun employee) at the
local pub, and you've found our code helpful, please buy us a round!
Distributed as-is; no warranty is given.
******************************************************************************/
void setup() {
// initialize the serial communication:
Serial.begin(9600);
pinMode(10, INPUT); // Setup for leads off detection LO +
pinMode(11, INPUT); // Setup for leads off detection LO -
}
void loop() {
if((digitalRead(10) == 1)||(digitalRead(11) == 1)){
Serial.println('!');
}
else{
// send the value of analog input 0:
Serial.println(analogRead(A0));
}
//Wait for a bit to keep serial data from saturating
delay(1);
} 
********************* End Arduino Sketch *******************************
********************* Processing Sketch Below *********************************** 
import processing.serial.*;

Serial s;        // The serial port
int xPos = 1;         // horizontal position of the graph
float height_old = 0;
float height_new = 0;
float inByte = 0;
static final int PORT_INDEX = 0, BAUDS = 38400;
String hrNumber = "";


void setup () {

  noLoop();
  final String[] ports = Serial.list();
  printArray(ports);
  new Serial(this, ports[PORT_INDEX], BAUDS).bufferUntil(ENTER);
  size(1900, 400);
}

  void serialEvent(final Serial s) {
    hrNumber = s.readString().trim();
    redraw = true;
    println(hrNumber);

  }

void draw () 
{
//  background(0xff);
  if (hrNumber.equals("!")) { 
    stroke(0, 0, 0xff); //Set stroke to blue ( R, G, B)
    inByte = 200;  // middle of the ADC range (Flat Line)
  }
  // If the data is good let it through
  else {
    stroke(0xff, 0, 0); //Set stroke to red ( R, G, B)
    inByte = float(hrNumber);
  }

  //Map and draw the line for new data point
  inByte = map(inByte, 0, 1023, 0, height);
  height_new = height - inByte; 
  line(xPos - 1, height_old, xPos, height_new);
  height_old = height_new;

  // at the edge of the screen, go back to the beginning:
  if (xPos >= width) {
    xPos = 0;
    background(0xdd);
  } else {
    // increment the horizontal position:
    xPos = xPos += 2;
  }
}
********************** END Processing Sketch ******************************

Hi John,

Thank you very much for sharing these tips and the updated Processing Sketch with us! I am sure this will be a very helpful troubleshooting post for other users. I will forward this updated Processing sketch to our tutorials team to evaluate it and hopefully, add it as an update or alternative to our current examples.

That is amazing to hear our little hobby-grade Heart Rate Monitor was able to provide some VERY useful information for you in such a scary scenario! Again, thank you so much for sharing this project and code. We really do appreciate it. If you wish to share an update on your next step with this project, I am sure many other users here would find it very useful.

Happy Hacking!

Hello John, glad to see your device contributed important info.! I have a much simpler, I think, problem, not knowing C-syntax. I’d like to split or maybe just duplicate the println output in the arduino ide script to a file on the raspberry pi which I can then ship off to an xcel spreadsheet for graphing. Can the arduino do that, or is it more involved than I imagine?

Many thanks for any and all help. Stefan

Hi,

Seems my question would have a better chance in another forum. Any suggestions you can offer?

Thanks,

Stefan

Hello John, glad to see your device contributed important info.! I have a much simpler, I think, problem, not knowing C-syntax. I’d like to split or maybe just duplicate the ‘println’ output in the arduino ide script to a file on the raspberry pi which I can then ship off to an xcel spreadsheet for graphing. Can the arduino do that, or is it more involved than I imagine?

Many thanks for any and all help. Stefan