Stress detecting wearable project, simple coding question

Hi all,

I'm doing a stress detection wearable project for school.

It is a neck band (or stole to be more precise) which measures your pulse, calculates your heart rate variability (HRV) and then if the variability goes down (indicating stress) vibration motors go off on the top of each shoulder (on the traps).

I'm building this on the pulse sensor amped code for arduino GitHub - WorldFamousElectronics/PulseSensor_Amped_Arduino: PulseSensor Arduino code for BPM and Processing-Visualizer

I'm trying to write a function takes the Inter Beat Interval (IBI) and compares them in pairs to test the variability in the results. Then if the comparison varies by more than 50ms a hit counter increases by 1. After 5 minutes the number of hits is compared with the total number of comparisons
Here is it written out if what I typed doesn't make sense (where I'm looking to calculate pNN50):
!! NN50, the number of pairs of successive NNs that differ by more than 50 ms.
pNN50, the proportion of NN50 divided by total number of NNs. !!

I wrote some basic code first:

IBIRecorded = false;
variationHits = 0;

 
 if (IBIRecorded == false){  
      currentIBI = IBI;             // Record the starting IBI

      IBIRecorded = true;   }

 if (IBIRecorded == true){
 Recorded IBI = currentIBI; 
      IBIRecorded = false;
 }

 if (currentIBI - recordedIBI) > 50 || (currentIBI - recordedIBI) < -50 {
 variationHits = variationHits + 1;
    }

This code doesn't have the total number comparison in it but I stopped working on it this way because I was worried that the comparison would happen every time the code ran (rather than each time a new IBI value is inputted). - I didn't have access to the hardware at the time to test it.

To try to find a way out of that I have tried to make a modified version of the following part of the interrupt handler:

 // keep a running total of the last 10 IBI values
      word runningTotal = 0;                  // clear the runningTotal variable    

      for(int i=0; i<=8; i++){                // shift data in the rate array
        rate[i] = rate[i+1];                  // and drop the oldest IBI value 
        runningTotal += rate[i];              // add up the 9 oldest IBI values
      }

      rate[9] = IBI;                          // add the latest IBI to the rate array
      runningTotal += rate[9];                // add the latest IBI to runningTotal
      runningTotal /= 10;                     // average the last 10 IBI values 
      BPM = 60000/runningTotal;               // how many beats can fit into a minute? that's BPM!
      QS = true;                              // set Quantified Self flag 
      // QS FLAG IS NOT CLEARED INSIDE THIS ISR

and changed this to

volatile int rate[2];                    // array to hold last 2 IBI values
 int variationHits = 0;
 
 
 word runningTotal = 0;                  // clear the runningTotal variable    

      for(int i=0; i<=1; i++){                // shift data in the rate array
        rate[i] = rate[i+1];                  // and drop the oldest IBI value 
        runningTotal += rate[i];              // add up the oldest IBI value
      }

      rate[1] = IBI;                          // add the latest IBI to the rate array
      runningTotal += rate[1];                // add the latest IBI to runningTotal

      if (rate[0] - rate[1]) > 50 || (rate[0] - rate[1]) < -50 {
 variationHits = variationHits + 1;
    }

Looking at it I think there is no need to use an array if I'm comparing only 2 values each time.

The bit I'm not sure about is how to make this into a more simple code with variables rather than the array.
How can I write the code so that the comparison only happens when a new value is sent via the sensor?
How can I count the total number of comparisons made?

Thanks for any help!

Measuring the stress for a given (non-overlapping) period could be done like this

class stressCheck {
  public:
    int nofChecks;
    int nofNN50;
    unsigned long lastDuration;
    unsigned long startedAt;
    stressCheck() {
      reset();
    }
    void reset() {
      nofChecks = 0;
      nofNN50 = 0;
    }
    void start() {
      reset();
      startedAt = millis();
    }
    bool lookAt(unsigned long ibi) {
      if (nofChecks++ == 0) { // first sample
        lastDuration = ibi;
      } else if (abs(ibi - lastDuration) > 50) {
        nofNN50++;
        return true;
      }
      return false;
    }
    int proMille() {
      if (nofChecks == 0) {
        return 0;
      }
      return (int)(1000L * nofNN50 / nofChecks);
    }
    unsigned long runningFor() {
      if (nofChecks == 0) {
        return 0;
      }
      return millis() - startedAt;
    }
};

stressCheck sc;
unsigned long lastFeed;
unsigned long lastPrint;

void setup() {
  Serial.begin(115200);
  sc.start();
}

void loop() {
  int proM;

  if (sc.runningFor() > 5 * 60 * 1000L) {
    proM = sc.proMille();
    if (proM > 50) {  // >5%
      Serial.print(proM);
      Serial.println("%% in 5 minutes");
    }
    sc.start();   // crudely just start a new period
  }
  if (millis() - lastFeed > 1000) {
    sc.lookAt(random(20, 80));
    lastFeed = millis();
  }
  if (millis() - lastPrint > 5000) {
    Serial.print(sc.proMille());
    Serial.print("%% ");
    Serial.print(sc.nofChecks);
    Serial.print(" total ");
    Serial.print(sc.nofNN50);
    Serial.print(" NN50 in ");
    Serial.print(sc.runningFor() / 1000);
    Serial.println(" s");
    lastPrint = millis();
  }
}

Most of the code is the test environment.