Hi all !
Until now, I've always managed to solve my issues with Google's help.
But I am stuck for 2 days now.
I have a Leostick (LeoStick Getting Started Guide | Freetronics ) which is a "mini copy" of Arduino Leonardo.
I need to have 2 sensors on it.
Ear-clip sensor & GSR Sensor
Ear-clip : http://www.seeedstudio.com/wiki/Grove_-_Ear-clip_Heart_Rate_Sensor
Gsr : http://www.seeedstudio.com/wiki/Grove_-_GSR_Sensor
Both are functioning great alone.
But when I merge my code, I have an issue.
I have value with the Ear-Clip that's not the real value.
Let me explain :
When I test only the ear-clip, my BPM is like 70 but varying between 60 to 75.
I also used "Runtastic Heart Rate" App on Android and the value are the correct value ( minus 1 or 2 but that's not a big problem)
But... When I test with GSR & ear-clip > Boom : the value for BPM is like 74 everytime except some moment where BPM is 59 or 99 (always the same value)
#include <RunningMedian.h>
int GSR_PIN = 1;
#define Heart 2 //Attach the Grove Ear-clip sensor to digital pin 2.
#define LED 13 //Attach an LED to digital pin 4
boolean calibrating = false;
boolean beat = false; /* This "beat" variable is used to control the timing of the Serial communication
so that data is only sent when there is a "change" in digital readings. */
RunningMedian samplesB = RunningMedian(5);
RunningMedian samplesI = RunningMedian(5);
int totalTime = 0; // totalTime: Is the variable used to identify the total time between beats
int lastTime = 0; // lastTime: Is the variable used to remember when the last beat took place
int beatCounter = 0; // beatCounter: Is used to keep track of the number of beats (in order to calculate the average BPM)
int totalBeats = 10; // totalBeats: Tells the computer that we want to calculate the average BPM using 10 beats.
int BPM[10]; // BPM[]: Is the Beat Per Minute (BPM) array - to hold 10 BPM calculations
int sumBPM = 0; // sumBPM: Is used to sum the BPM[] array values, and is then used to calculate the average BPM.
int avgBPM = 0; // avgBPM: Is the variable used to hold the average BPM calculated value.
int counter=0;
int gsrValue;
//==SETUP==========================================================================================
void setup() {
Serial.begin(9600); //Initialise serial communication
pinMode(Heart, INPUT); //Set digital pin 2 (heart rate sensor pin) as an INPUT
pinMode(LED, OUTPUT); //Set digital pin 4 (LED) to an OUTPUT
counter=0;
}
//==LOOP============================================================================================
void loop() {
// The code for Ear-Clip
if(digitalRead(Heart)>0){ //The heart rate sensor will trigger HIGH when there is a heart beat
if(!beat){ //Only send data when it first discovers a heart beat - otherwise it will send a high value multiple times
beat=true; //By changing the beat variable to true, it stops further transmissions of the high signal
digitalWrite(LED, HIGH); //Turn the LED on
//Serial.println(1023); //Send the high value to the computer via Serial communication.
trigger(millis()); // millis() creates a timeStamp of when the beat occured.
}
} else { //If the reading is LOW,
if(beat){ //and if this has just changed from HIGH to LOW (first low reading)
beat=false; //change the beat variable to false (to stop multiple transmissions)
digitalWrite(LED, LOW); //Turn the LED off.
//Serial.println(0); //then send a low value to the computer via Serial communication.
}
}
// The mini-code for GSR
gsrValue=analogRead(GSR_PIN);
Serial.print("g");
Serial.println(gsrValue);
delay(200);
}
void trigger(int time){ // This method is used to calculate the Beats per Minute (BPM) and to store the last 10 BPMs into the BPM[] array.
totalTime = time - lastTime; // totalTime = the current beat time minus the last time there was a beat.
samplesI.add(totalTime);
lastTime = time; // Set the lastTime variable to the current "time" for the next round of calculations.
BPM[beatCounter] = 60000/totalTime;
counter++;
samplesB.add(BPM[beatCounter]);
if(samplesB.getCount()>4 && samplesB.getMedian()>40){
Serial.print("I");
Serial.println((int)samplesI.getMedian());
Serial.print("B");
Serial.println((int) samplesB.getMedian());
}
// Calculate BPM from the totalTime. 60000 = 1 minute.
beatCounter++; // Increment the beatCounter
if (beatCounter>totalBeats-1){ // Reset the beatCounter when the total number of BPMs have been stored into the BPM[] array.
beatCounter=0; // This allows us to keep the last 10 BPM calculations at all times.
}
}
I would also add that when I raise the delay, value changes for BPM or BPM disappears.
I used RunningMedian library to have more accuracy on Ear-Clip sensor (which captures IBI : interbeat interval in mS and BPM )