Help Changing Code to read Data from two of the same sensors

Hi All,

I am attempting to change some example code to collect data from two of the same sensors. I am using two DF Robot Analog Heart Rate Sensors, running on a Feather. The code I am working with is:

/!
@file heart_test.ino
* @copyright DFRobot, 2016
* @copyright GNU Lesser General Public License
* @author jianghao
* @version V1.0
* @date 20160-07-8
/
/
!
* @brief Library for DFRobot's IR Position RAW_DATA
* @author jianghao
/
#include "HeartSpeed.h"
HeartSpeed heartspeed(A1); ///<The serial port for at observe pulse.
//HeartSpeed heartspeed(A1,RAW_DATA); ///<The serial port mapper, observation of ECG diagram.
/
Print the position result */
void mycb(uint8_t rawData, int value)
{
if(rawData){
Serial.println(value);
}else{
Serial.print("HeartRate Value = "); Serial.println(value);
}
}
void setup() {
Serial.begin(115200);
heartspeed.setCB(mycb); ///Callback function.
heartspeed.begin();///The pulse test.
}
void loop() {
}

I've tried writing a second callback function named mycb2, but am unsure exactly how to declare the second analog pin.

#include "HeartSpeed.h"
HeartSpeed heartspeed(A1);
HeartSpeed2 heartspeed2(A2);
//HeartSpeed heartspeed(A1,RAW_DATA); ///<The serial port mapper, observation of ECG diagram.
/* Print the position result */
void mycb1(uint8_t rawData, int value1)
{
if (rawData) {
Serial.write(value1);
} else {
Serial.write("HeartRate Value Joe = "); Serial.write(value1);
}
}
void mycb2(uint8_t rawData, int value2)
{
if (rawData) {
Serial.write(value2);
} else {
Serial.write("HearRate Value Paul = "); Serial.write(value2);
}
}
void setup() {
Serial.begin(115200);
heartspeed.setCB(mycb1); ///Callback function.
heartspeed.begin();///The pulse test.
heartspeed2.setCB(mycb2);
heartspeed2.begin();
}
}
void loop() {
}

My biggest questions:
-what pin is uint8_t calling?
-Should I be defining both pins at the beginning of the script?
-Does the library currently even allow for me to modify the script for two sensors?

Any advice would be greatly appreciated!

thank u!!!

ddisci:
-what pin is uint8_t calling?

uint8_t is a variable type, it does not call anything!

ddisci:
-Should I be defining both pins at the beginning of the script?

What pins? Please be very specific when asking questions because we are not as familiar with your project as you are.

ddisci:
-Does the library currently even allow for me to modify the script for two sensors?

Arduino programs are generally called sketches, but that's neither here nor there. You can attach two sensors by instantiating a second instance of the sensor's class (HeartSpeed). Here's an example:

HeartSpeed heartspeed_one(A1); //sensor 1
HeartSpeed heartspeed_two(A2); //sensor 2

You can either duplicate your existing code to handle both instances of the HeartSpeed class or have your functions accept the HeartSpeed class as a parameter for more general processing.

Power_Broker:
What pins? Please be very specific when asking questions because we are not as familiar with your project as you are.

Sorry about that pins A1 & A2

Arduino programs are generally called sketches, but that's neither here nor there. You can attach two sensors by instantiating a second instance of the sensor's class (HeartSpeed). Here's an example:

HeartSpeed heartspeed_one(A1); //sensor 1

HeartSpeed heartspeed_two(A2); //sensor 2




You can either duplicate your existing code to handle both instances of the HeartSpeed class or have your functions accept the HeartSpeed class as a parameter for more general processing.

So would it look something like this? This is compiling and uploading, but only giving me data from "Paul" and not "Joe"

[/code]

#include "HeartSpeed.h"

HeartSpeed heartspeed_one(A1); // Sensor 1
HeartSpeed heartspeed_two(A2); // Sensor 2

/* Print the position result */
void mycb1(uint8_t rawData, int value_one)
{
if (rawData) {
Serial.print(value_one);
} else {
Serial.print("HeartRate Value Joe = "); Serial.println(value_one);
}
}

void mycb2(uint8_t rawData, int value_two)
{
if (rawData) {
Serial.print(value_two);
} else {
Serial.print("HearRate Value Paul = "); Serial.println(value_two);
}
}
void setup() {
Serial.begin(115200);
heartspeed_one.setCB(mycb1); ///Callback function.
heartspeed_one.begin();///The pulse test.
heartspeed_two.setCB(mycb2);
heartspeed_two.begin();
}

void loop() {

}

[/code]

Looked at the source code for the library and this seems to be the culprit:

/*!
*  @brief Start program
*
*  @brief Set the timer, 5 seconds at a time
*
*  @return void
*/
void HeartSpeed::begin(void)
{
  MsTimer2::set(5,recv,this);
  MsTimer2::start();
}

Basically, only the last heartbeat sensor that begin() is called for will work. This is because all sensors try to use Timer2. Unless there is another library or you write your own logic, you're SOL

Power_Broker:
Looked at the source code for the library and this seems to be the culprit:

/*!

@brief Start program
*
@brief Set the timer, 5 seconds at a time
*
@return void
*/
void HeartSpeed::begin(void)
{
  MsTimer2::set(5,recv,this);
  MsTimer2::start();
}




Basically, only the last heartbeat sensor that begin() is called for will work. This is because all sensors try to use Timer2. Unless there is another library or you write your own logic, you're SOL

How difficult do you think it would be to write my own logic? Would I have to edit the library?

ddisci:
How difficult do you think it would be to write my own logic? Would I have to edit the library?

You won't know until you try...

Give it a shot if you can't find an alternate library.

All the begin method to that library does in set a timer and call the recv method every 5 milliseconds. Then every second filtered data is passed back via the callback function.

In theory you could create two HeartSpeed objects with different callbacks, as you have done, and call recv yourself.

void loop() {

  heartspeed_one.recv(&heartspeed_one);
  heartspeed_two.recv(&heartspeed_two);
  delay(5);

}

That won't work, though, because of the way the library is written. You would need to move the data declared at the top of the cpp file into the class. So the private section of header file starts like this:

private:
    

 
    int heartRate[20]={0};     ///<Heart rate total sample
    int sample[sampleSize]={0};
    int average_switch=1;
    int sum_for_collect;
    float average_num;
    int sum_high=0;
    int sum_low=0;
    int num_sum_high=0;
    int num_sum_low=0;///<Two types of situations sum
    
    HEARTCBFUNC cb;

        
 uint8_t rawData;       ///<Serial port mapping switch

Everything before the rawData variable declaration came from the cpp file. Then, back in the cpp file, you would prefix all the references to those variables with _this, for example:

_this->sample[_this->sampleNum]=data;

You might not get as precise timing that way, but you could always incorporate the MsTimer2 library separately for the same performance as the original library.

The biggest obstacle in converting the library is the amount of memory the Kalman filter uses. Two library objects will not fit in the memory of an Uno but a Mega should work.