MLX90614 and MAX30102

i’m making a project using esp32 as well as MLX90614 and MAX30102 sensors. The problem I’m having when putting the two sensors together doesn’t work. even though if you run the sensors one by one it goes well and can be displayed on blynk. but when put together the sensor does not work as usual and can not be displayed in blynk. the following is the program
thank you.

#define BLYNK_PRINT Serial
#define MAX_BRIGHTNESS 255
#define REPORTING_PERIOD_MS 1000

#include <BlynkSimpleEsp32.h>
#include <Adafruit_MLX90614.h>
#include <Wire.h>
#include <Blynk.h>
#include <WiFi.h>
#include "MAX30105.h"
#include "spo2_algorithm.h"
#include "heartRate.h"

MAX30105 particleSensor;

//SENSOR MLX90614 SUHU TUBUH DAN LINGKUNGAN
Adafruit_MLX90614 mlx = Adafruit_MLX90614();

float ambient_temp = 0.0;
float object_temp  = 0.0;

BlynkTimer timer;

//SENSOR MAX30100 SATURASI UDARA
uint32_t irBuffer[100];
uint32_t redBuffer[100];
uint32_t tsLastReport = 0;

int32_t bufferLength;
int32_t spo2;
int32_t heartRate; 

int8_t validSPO2;
int8_t validHeartRate;

byte pulseLED = 2; //onboard led on esp32 nodemcu
byte readLED = 19;

long lastBeat = 0;

float beatsPerMinute;
float ledBlinkFreq;

int beatAvg = 0, sp02Avg = 0; 

//BAGIAN ATAS BERES BAGIAN VOID SETUP DAN VOID SENSOR MLX DAN MAX

char auth[] = "Or9lkhcUTOJqs4YYrGl-7mgL-PaoduAW";
char ssid[] = "ALI";
char pass[] = "ali101010";

void setup() {
  //SET UP MLX90614
  Serial.begin(9600);
  Serial.println("Adafruit MLX90614 test");
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  mlx.begin();
  timer.setInterval(300L,sensor1);
  
  //SET UP MAX30100 SATURASI OKSIGEN
  ledcSetup(0, 0, 8); // PWM Channel = 0, Initial PWM Frequency = 0Hz, Resolution = 8 bits
  ledcAttachPin(pulseLED, 0); //attach pulseLED pin to PWM Channel 0
  ledcWrite(0, 255);

  Serial.print("Initializing Pulse Oximeter..");
  
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  {
    Serial.println(F("MAX30105 was not found. Please check wiring/power."));
    while (1);
  }
  byte ledBrightness = 50; //Options: 0=Off to 255=50mA
  byte sampleAverage = 1; //Options: 1, 2, 4, 8, 16, 32
  byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
  byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
  int pulseWidth = 69; //Options: 69, 118, 215, 411
  int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
  
  particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange);
}

//SETUP BERES LANJUT LOOP

void sensor1(){
  ambient_temp = mlx.readAmbientTempC();
  Serial.print("Ambient Temp = ");
  Serial.println(ambient_temp);
  Blynk.virtualWrite(V4,ambient_temp); 
  object_temp = mlx.readObjectTempC();
  Serial.print("Object Temp = ");
  Serial.println(object_temp);
  Blynk.virtualWrite(V5,object_temp); 
}

void sensor2(){
  bufferLength = 100;
  for (byte i = 0 ; i < bufferLength ; i++)
  {
    while (particleSensor.available() == false) //do we have new data?
      particleSensor.check(); //Check the sensor for new data
  
    redBuffer[i] = particleSensor.getIR();
    irBuffer[i] = particleSensor.getRed();
    particleSensor.nextSample(); //We're finished with this sample so move to next sample
  
    Serial.print(F("red: "));
    Serial.print(redBuffer[i], DEC);
    Serial.print(F("\t ir: "));
    Serial.println(irBuffer[i], DEC);
  }

  maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);

  while (1)
  {
    
    for (byte i = 25; i < 100; i++)
    {
      redBuffer[i - 25] = redBuffer[i];
      irBuffer[i - 25] = irBuffer[i];
    }
    for (byte i = 75; i < 100; i++)
    {
      while (particleSensor.available() == false) //do we have new data?
        particleSensor.check(); //Check the sensor for new data
    
      digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read
    
      redBuffer[i] = particleSensor.getRed();
      irBuffer[i] = particleSensor.getIR();
      particleSensor.nextSample();
      long irValue = irBuffer[i];

      if (checkForBeat(irValue) == true)
      {
        long delta = millis() - lastBeat;
        lastBeat = millis();
        beatAvg = (beatAvg+beatsPerMinute)/2;

        if(beatAvg != 0)
          ledBlinkFreq = (float)(60.0/beatAvg);
        else
          ledBlinkFreq = 0;
        ledcWriteTone(0, ledBlinkFreq);
      }
      if (millis() - lastBeat > 10000)
      {
        beatsPerMinute = 0;
        beatAvg = (beatAvg+beatsPerMinute)/2;
        
        if(beatAvg != 0)
          ledBlinkFreq = (float)(60.0/beatAvg);
        else
          ledBlinkFreq = 0;
        ledcWriteTone(0, ledBlinkFreq);
      }
    }
    Serial.print(beatAvg, DEC);
    
    Serial.print(F("\t HRvalid="));
    Serial.print(validHeartRate, DEC);
    
    Serial.print(F("\t SPO2="));
    Serial.print( sp02Avg , DEC);
    
    Serial.print(F("\t SPO2Valid="));
    Serial.println(validSPO2, DEC);
    if (validSPO2 == 1 && spo2 < 100 && spo2 > 0)
    {
      sp02Avg = (sp02Avg+spo2)/2;
    }
    else
    {
      spo2 = 0;
      sp02Avg = (sp02Avg+spo2)/2;;
    }
    if(millis() - tsLastReport > REPORTING_PERIOD_MS){
      Blynk.virtualWrite(V7, beatAvg);
      Blynk.virtualWrite(V8, sp02Avg);

      tsLastReport = millis();
    }
  }
}

void loop() {
  Blynk.run();
  timer.run();
}

@Alsofila10 please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

sorry sir i don’t understand. because new to this

@Alsofila10 do you know how to copy and paste?
If so, then copy the triple backticks I provided and paste them onto a line of their own just before and just after the code that you’ve posted.

Pete.

thank you sir for the knowledge :slight_smile:

Your sensor2() is never being called, and if you do call it then it will enter an unbreakable loop because of the while(1) section of that function…

Pete.

is it like this sir?

while (sensor2)
  {
    
    for (byte i = 25; i < 100; i++)
    {
      redBuffer[i - 25] = redBuffer[i];
      irBuffer[i - 25] = irBuffer[i];
    }
    for (byte i = 75; i < 100; i++)
    {
      while (particleSensor.available() == false) //do we have new data?
        particleSensor.check(); //Check the sensor for new data
    
      digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read
    
      redBuffer[i] = particleSensor.getRed();
      irBuffer[i] = particleSensor.getIR();
      particleSensor.nextSample();
      long irValue = irBuffer[i];

      if (checkForBeat(irValue) == true)
      {
        long delta = millis() - lastBeat;
        lastBeat = millis();
        beatAvg = (beatAvg+beatsPerMinute)/2;

        if(beatAvg != 0)
          ledBlinkFreq = (float)(60.0/beatAvg);
        else
          ledBlinkFreq = 0;
        ledcWriteTone(0, ledBlinkFreq);
      }
      if (millis() - lastBeat > 10000)
      {
        beatsPerMinute = 0;
        beatAvg = (beatAvg+beatsPerMinute)/2;
        
        if(beatAvg != 0)
          ledBlinkFreq = (float)(60.0/beatAvg);
        else
          ledBlinkFreq = 0;
        ledcWriteTone(0, ledBlinkFreq);
      }
    }
    Serial.print(beatAvg, DEC);
    
    Serial.print(F("\t HRvalid="));
    Serial.print(validHeartRate, DEC);
    
    Serial.print(F("\t SPO2="));
    Serial.print( sp02Avg , DEC);
    
    Serial.print(F("\t SPO2Valid="));
    Serial.println(validSPO2, DEC);
    if (validSPO2 == 1 && spo2 < 100 && spo2 > 0)
    {
      sp02Avg = (sp02Avg+spo2)/2;
    }
    else
    {
      spo2 = 0;
      sp02Avg = (sp02Avg+spo2)/2;;
    }
    if(millis() - tsLastReport > REPORTING_PERIOD_MS){
      Blynk.virtualWrite(V7, beatAvg);
      Blynk.virtualWrite(V8, sp02Avg);

      tsLastReport = millis();
    }
  }

You shouldn’t be using ‘while’ loops with Blynk, but I think you’re probably out of your depth when it comes to programming these devices with Blynk.

You’d probably be better getting the two sensors working together without Blynk.

Pete.