Using multiple sensors on blynk

I’m using this code for fall detection using mpu6050 and a pulse sensor on esp8266. the issue is fall detections doesn’t work well (slows down) with pulse sensor and doesn’t detect falls. any solutions?



/*********************

  You’ll need:
   - Blynk IoT app (download from App Store or Google Play)
   - ESP8266 board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *********************/

/* Fill-in information from Blynk Device Info here */


#define BLYNK_TEMPLATE_ID "TMPL6C0esv-iD"
#define BLYNK_TEMPLATE_NAME "FALL DETECT"
#define BLYNK_AUTH_TOKEN "LS5buyVsN2HHu6jMCWEnLgMB02tR-t7o"

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include<Wire.h>


WidgetLED gir(V0);

const int MPU_addr = 0x68; // I2C address of the MPU-6050
 int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
 float ax = 0, ay = 0, az = 0, gx = 0, gy = 0, gz = 0;
 boolean fall = false; //stores if a fall has occurred
 boolean trigger1 = false; //stores if first trigger (lower threshold) has occurred
 boolean trigger2 = false; //stores if second trigger (upper threshold) has occurred
 boolean trigger3 = false; //stores if third trigger (orientation change) has occurred
 byte trigger1count = 0; //stores the counts past since trigger 1 was set true
 byte trigger2count = 0; //stores the counts past since trigger 2 was set true
 byte trigger3count = 0; //stores the counts past since trigger 3 was set true
 int angleChange = 0;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "T
char pass[] = "

const int pulsePin = A0; // Pulse sensor connected to analog pin A0

// Variables
int pulseValue = 0;
int thresholdHigh = 85; // High threshold for pulse rate
int thresholdLow = 50;  // Low threshold for pulse rate

// // Variables for pulse rate calculation
int pulseSensorValue = 0;
int signal;
int threshold = 60;

void setup()
{
  // Debug console
  Serial.begin(115200);
  delay(100);
  
    // Initialize I2C with SDA = D2 (GPIO 4), SCL = D1 (GPIO 5)
   // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Wire.begin(D2, D1);
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  // You can also specify server:
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);
}

void loop()
{
  Blynk.run();
  fell();   //fall detection

  HR();   // Heart RAte

 // Temp();  // Temperature
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}



void fell() {
  mpu_read();
  ax = (AcX - 2050) / 16384.00;
  ay = (AcY - 77) / 16384.00;
  az = (AcZ - 1947) / 16384.00;
  gx = (GyX + 270) / 131.07;
  gy = (GyY - 351) / 131.07;
  gz = (GyZ + 136) / 131.07;
  // calculating Amplitute vactor for 3 axis
  float Raw_Amp = pow(pow(ax, 2) + pow(ay, 2) + pow(az, 2), 0.5);
  int Amp = Raw_Amp * 10;  // Mulitiplied by 10 bcz values are between 0 to 1
  Serial.println(Amp);
  //SerialBT.println(Amp);
  if (Amp <= 2 && trigger2 == false) { //if AM breaks lower threshold (0.4g)
    trigger1 = true;
    Serial.println("TRIGGER 1 ACTIVATED");
    //SerialBT.println("TRIGGER 1 ACTIVATED");
  }
  if (trigger1 == true) {
    trigger1count++;
    if (Amp >= 12) { //if AM breaks upper threshold (3g)
      trigger2 = true;
      //Serial.println("TRIGGER 2 ACTIVATED");
      //SerialBT.println("TRIGGER 2 ACTIVATED");
      trigger1 = false; trigger1count = 0;
    }
  }
  if (trigger2 == true) {
    trigger2count++;
    angleChange = pow(pow(gx, 2) + pow(gy, 2) + pow(gz, 2), 0.5); Serial.println(angleChange);
    if (angleChange >= 30 && angleChange <= 400) { //if orientation changes by between 80-100 degrees
      trigger3 = true; trigger2 = false; trigger2count = 0;
      Serial.println(angleChange);
      Serial.println("TRIGGER 2 ACTIVATED");
      //SerialBT.println(angleChange);
      //SerialBT.println("TRIGGER 2 ACTIVATED");
    }
  }
  if (trigger3 == true) {
    trigger3count++;
    if (trigger3count >= 10) {
      angleChange = pow(pow(gx, 2) + pow(gy, 2) + pow(gz, 2), 0.5);
      //delay(10);
      //Serial.println(angleChange);
      //SerialBT.println(angleChange);
      if ((angleChange >= 0) && (angleChange <= 10)) { //if orientation changes remains between 0-10 degrees
        fall = true; trigger3 = false; trigger3count = 0;
        //Serial.println(angleChange);
      }
      //SerialBT.println(angleChange); }
      else { //user regained normal orientation
        trigger3 = false; trigger3count = 0;
        //Serial.println("TRIGGER 3 DEACTIVATED");
        //SerialBT.println("TRIGGER 3 DEACTIVATED");
      }
    }
  }
  if (fall == true) { //in event of a fall detection
    Serial.println("FALL DETECTED");
    //SerialBT.println("FALL DETECTED");
    fall = false;
     gir.on();
     delay(1000);
     gir.off();
    
  }
  if (trigger2count >= 6) { //allow 0.5s for orientation change
    trigger2 = false; trigger2count = 0;
    //Serial.println("TRIGGER 2 DECACTIVATED");
    //SerialBT.println("TRIGGER 2 DECACTIVATED");
  }
  if (trigger1count >= 6) { //allow 0.5s for AM to break upper threshold
    trigger1 = false; trigger1count = 0;
    //Serial.println("TRIGGER 1 DECACTIVATED");
    //SerialBT.println("TRIGGER 1 DECACTIVATED");
  }
  delay(100);
}

void mpu_read() {
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr, 14, true); // request a total of 14 registers
  AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
}



void HR()
{signal = analogRead(pulsePin);

  // Detect if the signal is above the threshold
  if (signal > threshold) {
    pulseSensorValue = map(signal, threshold, 1023, 50, 120); // Map the pulse sensor value to a range
  } else {
    pulseSensorValue = 0; // If no signal is detected, set pulse rate to 0
  }

  // Display the pulse rate on the serial monitor
  // Serial.print("Pulse Rate: ");
  // Serial.println(pulseSensorValue);
 Blynk.virtualWrite(V2,pulseSensorValue );
  // Check the pulse rate and log warnings if necessary
  // if (pulseSensorValue > thresholdHigh) {
  //   Serial.println("Warning: High Pulse Rate!");
  // } else if (pulseSensorValue > 0 && pulseSensorValue < thresholdLow) { // Make sure it is not zero to avoid false alarm
  //   Serial.println("Warning: Low Pulse Rate!");
  // }

  // Small delay before next reading
  delay(1000);

}

You’re calling two funcations from your void loop, which is effectively the same as having the code from both of those functions directly in your void loop.
In addition, both of those functions contain blocking delay() commands.

The comment in your void loop reminds you not to do this…

Your note in your ‘HR` function….

Should really read “Block all code execution for one second…”

You need to use Bl6nkTimer instead…

Pete.

Thanks pete