Pulse Oximeter Readings issue

Hello Everyone,
I am making a sleep monitoring IoT system for which I am using a microphone amplifier, accelerometer and a Pulse Oximeter. All the sensors work properly after I tested them individually but when I combine all the sensors together, I am only managing to get the readings of the accelerometer and the microphone amplifier, the pulse oximeter is not producing any readings.

Pasting the code below:-

#include <Wire.h>                
#include "MAX30100_PulseOximeter.h"

#define REPORTING_PERIOD_MS     1000
 
PulseOximeter pox;                   
uint32_t tsLastReport = 0;   

int ADXL345 = 0x53; 
int X_out, Y_out, Z_out;

const int sampleWindow = 50;
unsigned int sample;


 
void onBeatDetected()
{
    Serial.println("Beat!");
}
 
void setup()
{
    Serial.begin(9600);
    Serial.print("Initializing pulse oximeter..");
 
    if (!pox.begin()) {
        Serial.println("FAILED");
        for(;;);
    } else {
        Serial.println("SUCCESS");
    }
    pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
 
    
    pox.setOnBeatDetectedCallback(onBeatDetected);

    Wire.begin();

    Wire.beginTransmission(ADXL345);
    Wire.write(0x2D);  
  
    Wire.write(8);
    Wire.endTransmission();
    delay(10);
}
 
void loop()
{
    pox.update();
    if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
        Serial.print("Heart rate:");
        Serial.print(pox.getHeartRate());
        Serial.print("bpm / SpO2:");
        Serial.print(pox.getSpO2());
        Serial.println("%");
 
        tsLastReport = millis();               
    }
    
    Wire.beginTransmission(ADXL345);
    Wire.write(0x32);

    Wire.endTransmission(false);
    Wire.requestFrom(ADXL345, 6, true); 

    X_out = ( Wire.read() | Wire.read() << 8); 
    Y_out = ( Wire.read() | Wire.read() << 8); 
    Z_out = ( Wire.read() | Wire.read() << 8); 

    Serial.print(X_out);
    Serial.print(" ");
    Serial.print(Y_out);
    Serial.print(" ");
    Serial.println(Z_out);
    delay(100);

    unsigned long startMillis= millis(); 
    unsigned int peakToPeak = 0;   
 
    unsigned int signalMax = 0; 
    unsigned int signalMin = 1024;
 
    while (millis() - startMillis < sampleWindow)
    {
      sample = analogRead(A0); 


      if (sample < 1024)  // toss out spurious readings
      {
         if (sample > signalMax)
         {
            signalMax = sample;  // To find the max sample
         }
         else if (sample < signalMin)
         {
            signalMin = sample;  // To find the min sample
         }
      }
    }
    peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
    double volts = (peakToPeak * 5.0) / 1024;  // convert to volts
 
    Serial.print("loudness : ");
    Serial.println(volts);

    delay(1000);
    
}

I haven't used arduino before so I am a complete newbie, so any help provided by you'll would be of great help to me.
Thanks

Nice You used code tags.
Which Arduino? There are some 10 more or less different ones.
Please post links to the data sheets of the devices involved.

Does your working sample pox code have a long delay in its loop function like your amalgamation?

Perhaps the sensor needs to be updated more frequently that once a second.

I’ll look at the sensor; post the code that tests and confirms the pox sensor operation OK.

edit: OK, I’m back.

    // Make sure to call update as fast as possible
    pox.update();

from the example. You’ll have to figure out how to not block your loop from running faster… I think, and you should anyway, I am of the opinion that.

a7

I am using the arduino uno.
MAX4466 - Microphone Amplifier

MAX30100 - Pulse Oximeter

ADXL345 - Accelerometer

#include <Wire.h>

#include "MAX30100_PulseOximeter.h"
 
#define REPORTING_PERIOD_MS     1000
 
PulseOximeter pox;
uint32_t tsLastReport = 0;
 
void onBeatDetected()
{
    Serial.println("Beat!");
}
 
void setup()
{
    Serial.begin(9600);
    Serial.print("Initializing pulse oximeter..");
 
    // Initialize the PulseOximeter instance
    // Failures are generally due to an improper I2C wiring, missing power supply
    // or wrong target chip
    if (!pox.begin()) {
        Serial.println("FAILED");
        for(;;);
    } else {
        Serial.println("SUCCESS");
    }
     pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
 
    // Register a callback for the beat detection
    pox.setOnBeatDetectedCallback(onBeatDetected);
}
 
void loop()
{
    // Make sure to call update as fast as possible
    pox.update();
    if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
        Serial.print("Heart rate:");
        Serial.print(pox.getHeartRate());
        Serial.print("bpm / SpO2:");
        Serial.print(pox.getSpO2());
        Serial.println("%");
 
        tsLastReport = millis();

    }
        
}

This is the pulse oximeter code i used. MAX30100 works properly here but as soon as the program gets longer it doesn't seem to work.
You may be right about it needing to update itself faster. Do you have any idea on how I should go about this?

Yes.

This, from your code, is a classic pattern to make something happen on a schedule, rather than every time the loop, well, loops.

    if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
        Serial.print("Heart rate:");
        Serial.print(pox.getHeartRate());
        Serial.print("bpm / SpO2:");
        Serial.print(pox.getSpO2());
        Serial.println("%");
 
        tsLastReport = millis();               
    }

It is the essential mechanism employed to keep your loop from blocking.

Use a similar pattern eliminate the large

 delay(1000);

you have, which looks like all you doing with it is keeping these lines

    Serial.print("loudness : ");
    Serial.println(volts);

from spamming you serial output with a crap-ton of printing.

Wrap those lines in a similar mechanism to make the print only as often as you need or want… then your loop will run at top speed, and all the actions will be governed by the millis()/"lastTime" logic.

Look at the code fragment I put up top here in this post, study it to see how it works.

It is a straight ahead technigue widely used, well worth getting your head around.

Bark if you need help, but it should be within you grasp.

a7

This device can use I2C for communication. If the lib is using I2C or any other method, I can't say.
Pay attention to the replies from @ alto777

Another perhaps simpler experiment would be to just put

delay(1000);

at the end of the example loop code.

If stalling or slowing the loop is your problem, this would make it happen in the example as well.

a7