Trying to implement a Heartbeat funcion but Delta is too huge

Good evening everyone,
this is my first post here since I am at my wits end and I thought "maybe the wise people of the Arudino forum might be able to help me"

Basically I have a Funduino Pulse sensor connected to a ESP32S3 by Xiao and I am getting an analog Signal, calculating a Threshold, then comparing it to a new Signal and then checking if its bigger then the Threshold. Then delta gets calculated and I get a BPM. However my issue is that Delta is always huge so my BPM is just like 5 or 0, 19 if I´m lucky haha. I have some other things connected aswell since my goal was essentially to creat a "less smart smartwatch" but for now I will share my Pulse() and some stuff from my loop and main

my Pulse():
summe = 0;

  sQuadrate = 0;

  final_abweichung = 0;

  int Threshold;



   static unsigned long lastThresholdUpdate = 0;

  if (millis() - lastThresholdUpdate > 500) {// nur allle 2 Sekunden neu

        lastThresholdUpdate = millis();

 // Signal = analogRead(PulsSensorPin);

  for (int i = 0; i < Messwerte; i++) {

    signale\[i\] = analogRead(PulsSensorPin); // Arduino (where the ADC range is 0-1023) but in case of ESP32 the ADC values are 0-4095.

    delay(5); // Kleine Pause

    }

  }



  for (int i = 0; i < Messwerte; i++){

    summe += signale\[i\];

  }



  schnitt = summe / Messwerte; // schnitt berechnen

  for (int i = 0; i < Messwerte; i ++){

    abweichung\[i\] = sq(signale\[i\] - schnitt); // Abweichungen quadrieren

  }

  

  for (int i = 0; i < Messwerte; i++){

    sQuadrate += abweichung\[i\]; // Summe der Quadrate

  }



  sQuadrate_neu = sQuadrate / Messwerte;

  final_abweichung = sqrt(sQuadrate_neu); // finale abweichung sqrt



  //Threshold = (schnitt + final_abweichung) \* 0.8;

Threshold = schnitt  + final_abweichung \* 0.1;

Serial.println("Threshold");

Serial.println(Threshold);

    



int aktuellesSignal = analogRead(PulsSensorPin);

//int aktuellesSignal = signale\[Messwerte - 1\];





static bool warUeberThreshold = false;



Serial.print("aktuelles signal: ");

    Serial.println(aktuellesSignal);



if

 (aktuellesSignal >= Threshold && !warUeberThreshold ){ //&& warUeberThreshold == false



  warUeberThreshold = true;



  unsigned long currentTime = millis();

  unsigned long delta = currentTime - lastBeatTime;

  Serial.println("delata");

  Serial.println(delta);

  if (delta > 300 ) {  // schützt vor Doppelzählung && delta < 1500



    BPM = 60000 / delta;

    lastBeatTime = currentTime;



    Serial.print("BPM: ");

    Serial.println(BPM);

  } 

}

and some of my main and loop:
int PulsSensorPin = A2; 
int Signal;                // holds the incoming raw data. Signal value can range from 0-4095


const int Messwerte = 200; 

int signale\[Messwerte\]; 

int abweichung\[Messwerte\];

int sQuadrate;

int sQuadrate_neu;

int final_abweichung;

int schnitt;

int summe;

loop:
//Clock();

    Puls();

    //Pedometer();



    static unsigned long lastPedUpdate = 0;

    if (millis() - lastPedUpdate > 10000) {

        lastPedUpdate = millis();

        Pedometer();

    }



    static unsigned long lastOxiUpdate = 0;

    if (millis() - lastOxiUpdate > 10000) {

        lastOxiUpdate = millis();

        Oximeter();

    }

    //delay(10); 

    displayBatteryLevel();



    //myMessage.now = rtc.now();

    myMessage.zeit = rtc.now().unixtime();

    myMessage.oxygen = spo2;

    myMessage.heartrate = hr;

    strncpy(myMessage.schritte, report, sizeof(myMessage.schritte));

    //myMessage.schritte \[256\] = report;

    myMessage.puls = BPM;

    myMessage.gespeicherteSteps = savedSteps;

    myMessage.volt = voltage;

    myMessage.prozent = percent;




    myMessage.test = 5;

    esp_err_t result = esp_now_send(receiverAddress, (uint8_t \*) &myMessage, sizeof(myMessage));

    Serial.println("result:");

    Serial.println(result);

    if (result != ESP_OK) {

        Serial.println("Sending error");

    }

all the data gets send over to a reciever device so I can dispaly the data on a little screen, thats where the ESPNow comes from.

I hope there is someone that can offer some advice and if you need more info lmk :slight_smile:
Thank you all in advance!!!

My advice would be to post a sketch that demonstrates the problem you're having.

Snippets in general are wastes of time. Snippets that don't even show the data type of the variable you claim to have issues with, even more so.

Show a complete sketch that demonstrates the problem.

Please post the complete sketch and examples of the output (using code tags).

You can check whether the sensor is working as expected by following this tutorial closely.

Posting an annotated schematic along with the code would help us help you. Be sure to include all power sources and links to technical information on the hardware items.