diff --git a/Software/Heart_Rate_Display_Processing/Heart_Rate_Display/Heart_Rate_Display.pde b/Software/Heart_Rate_Display_Processing/Heart_Rate_Display/Heart_Rate_Display.pde index 3f11598..9e9aa69 100644 --- a/Software/Heart_Rate_Display_Processing/Heart_Rate_Display/Heart_Rate_Display.pde +++ b/Software/Heart_Rate_Display_Processing/Heart_Rate_Display/Heart_Rate_Display.pde @@ -14,9 +14,9 @@ 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 +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! @@ -26,110 +26,102 @@ Distributed as-is; no warranty is given. import processing.serial.*; -Serial myPort; // The serial port -int xPos = 1; // horizontal position of the graph +Serial myPort; // The serial port +int xPos_i = 1; // X increment +int xPos = xPos_i; // horizontal position of the graph float height_old = 0; float height_new = 0; float inByte = 0; int BPM = 0; int beat_old = 0; -float[] beats = new float[500]; // Used to calculate average BPM +int array_size = 10; // how many "beats" to store to calculate the average +float[] beats = new float[array_size]; // Used to calculate average BPM int beatIndex; -float threshold = 620.0; //Threshold at which BPM calculation occurs +float threshold = 620.0; // Threshold at which BPM calculation occurs boolean belowThreshold = true; PFont font; - -void setup () { +void setup() { // set the window size: - size(1000, 400); - + size(1500, 400); // increased width a little // List all the available serial ports println(Serial.list()); // Open whatever port is the one you're using. - myPort = new Serial(this, Serial.list()[2], 9600); + myPort = new Serial(this, Serial.list()[1], 9600); // don't generate a serialEvent() unless you get a newline character: myPort.bufferUntil('\n'); // set inital background: background(0xff); - font = createFont("Ariel", 12, true); + font = createFont("Arial", 12, true); } +void draw() { + // Map and draw the line for new data point + float mapByte = map(inByte, 0, 1023, 0, height); + height_new = height - mapByte; + + // at the edge of the screen, go back to the beginning: + if (xPos >= width) { + xPos = xPos_i; + background(0xff); + } else { + // increment the horizontal position: + xPos = xPos + xPos_i; + } -void draw () { - //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(0xff); - } - else { - // increment the horizontal position: - xPos++; - } - - // draw text for BPM periodically - if (millis() % 128 == 0){ - fill(0xFF); - rect(0, 0, 200, 20); - fill(0x00); - text("BPM: " + inByte, 15, 10); - } -} + line(xPos - xPos_i, height_old, xPos, height_new); + height_old = height_new; + // draw text for BPM periodically + if (millis() % 128 == 0) { + fill(0xFF); + rect(0, 0, 200, 20); + fill(0x00); + text("BPM: " + BPM, 15, 10); + } +} -void serialEvent (Serial myPort) -{ +void serialEvent(Serial myPort) { // get the ASCII string: String inString = myPort.readStringUntil('\n'); - if (inString != null) - { + if (inString != null) { // trim off any whitespace: inString = trim(inString); // If leads off detection is true notify with blue line - if (inString.equals("!")) - { - stroke(0, 0, 0xff); //Set stroke to blue ( R, G, B) - inByte = 512; // middle of the ADC range (Flat Line) + if (inString.equals("!")) { + stroke(0, 0, 0xff); // Set stroke to blue ( R, G, B) + inByte = 512.0; // 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(inString); - + else { + stroke(0xff, 0, 0); // Set stroke to red ( R, G, B) + inByte = float(inString); + // BPM calculation check - if (inByte > threshold && belowThreshold == true) - { + if (inByte > threshold && belowThreshold == true) { calculateBPM(); belowThreshold = false; - } - else if(inByte < threshold) - { + } else if (inByte < threshold) { belowThreshold = true; } } } } - -void calculateBPM () -{ - int beat_new = millis(); // get the current millisecond - int diff = beat_new - beat_old; // find the time between the last two beats - float currentBPM = 60000 / diff; // convert to beats per minute - beats[beatIndex] = currentBPM; // store to array to convert the average - float total = 0.0; - for (int i = 0; i < 500; i++){ - total += beats[i]; - } - BPM = int(total / 500); - beat_old = beat_new; - beatIndex = (beatIndex + 1) % 500; // cycle through the array instead of using FIFO queue + +void calculateBPM() { + int beat_new = millis(); // get the current millisecond + int diff = beat_new - beat_old; // find the time between the last two beats + if(diff > 0) { // solves the bug when diff equals zero + float currentBPM = 60000 / diff; // convert to beats per minute + beats[beatIndex] = currentBPM; // store to array to convert the average + float total = 0.0; + for (int i = 0; i < array_size; i++) { + total += beats[i]; + } + BPM = int(total / array_size); + beat_old = beat_new; + beatIndex = (beatIndex + 1) % array_size; // cycle through the array instead of using FIFO queue } +}