Hello everyone,
First of all, thank you for taking your time helping me. I'm working on a project which gets a bunch of health data and sends it to a web server. I'm stuck trying to have 2 sensors work together.
I'm using MLX90614 for the temperature, and MAX30100 for the oxygen saturation and bpm, and the Arduino Mega 2560. Both sensors offer I2C communication, which I'm using in order to reduce pins (I think that it's compulsory to use I2C for both sensors), because I'll have to add more sensors in the future. I'm using the library for the MAX30100, I looked into it and I think it's way to difficult to use it without the library.
My problem is that I'm not being able to use them together. When I run the script with the Arduino, I get the correct data for the MLX90614, but the MAX30100 return 0.00 for both SPO2 and BPM. I know it has something to do with the begin() of both sensors, because both work fine separately with the same code, but when I begin both sensors I get this error.
I've tried to shutdown and begin the sensors within the loop() , but seems like the sensors will only begin if I use the functiosn begin() in the setup() function.
The code I will attach gives data for MLX90614, but 0.00 for MAX30100. As you will see, I've commented code lines that I thought that would work, but only makes the MLX90614 give 1034 as value, and 0.00 for MAX30100.
So, my cuestion is, ¿How can I make them work together?¿what is the problem with I2C and using 2 sensors with them libraries?¿Is it about the clock for I2C?
I feel like I'm missing something, because some things are not making sense (I have to begin the mlx, if I use delay() in any of the measurment functions, everything fails...etc)
Thank you so so much for you help, any recommendation will be welcomed.
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include "MAX30100_PulseOximeter.h"
Adafruit_MLX90614 mlx = Adafruit_MLX90614(); /Temperature
PulseOximeter pox; // SPO2 and BPM
#define MLX90614_TOBJ1 0x07
void setup() {
Serial.begin(9600);
Wire.begin();
pox.begin();
//If Idon't begin mlx here, it won't work even tho I'm not using it in the rest of the code, and the function only does Wire.begin()
mlx.begin();
}
uint16_t measure_temperature(){
float start = millis();
uint16_t ret=0.0;
while((millis()-start)<800){
Wire.beginTransmission(90); // start transmission to device
Wire.write(MLX90614_TOBJ1); // sends register address to read from
Wire.endTransmission(false); // end transmission
Wire.requestFrom(90, (size_t)3); // send data n-bytes read
ret = Wire.read(); // receive DATA
ret |= Wire.read() << 8; // receive DATA
uint8_t pec = Wire.read();
ret *= .02;
ret -= 273.15;
}
//Wire.endTransmission(true);
return ret;
}
float * measure_max30100(){
//pox.begin();
pox.update();
float spo2 = .00;
float bpm = .00;
static float result[2];
uint32_t tsLastReport = 0;
float start = millis();
if((millis()-tsLastReport)>5000){
spo2 = pox.getSpO2();
bpm = pox.getHeartRate();
tsLastReport = millis();
}
result[0] = spo2;
result[1] = bpm;
//pox.shutdown();
return result;
}
void loop() {
//pox.begin();
boolean ready = true;
if(ready==true){
//Temperature
float temperature_measurment = 0.00;
temperature_measurment = measure_temperature();
//SPO2 y BPM
float* measurment_spo2_bpm = measure_max30100();
float spo2 = measurment_spo2_bpm[0];
float bpm = measurment_spo2_bpm[1;]
//check errors
if(temperature_measurment >60 || temperature_measurment <25){ Serial.println("¡ERROR TEMPERATURE! " );}
if(spo2>100&&spo2<85){Serial.println("¡ERROR SPO2! " );}
if(bpm>200&&bpm<40){Serial.println("¡ERROR BPM! " );}
else{
//print Temperature
Serial.print("Temperature: " );
Serial.println(temperature_measurment );
//print SPO2 y BPM
Serial.print("SPO2: " );
Serial.println(spo2);
Serial.print("BPM: " );
Serial.println(bpm);
}
}
//pox.shutdown();
//while(1){}
}