Stress detecting wearable project, simple coding question


hi all,

i'm doing stress detection wearable project school.

it neck band (or stole more precise) measures pulse, calculates heart rate variability (hrv) , if variability goes down (indicating stress) vibration motors go off on top of each shoulder (on traps).

i'm building on pulse sensor amped code arduino https://github.com/worldfamouselectronics/pulsesensor_amped_arduino

i'm trying write function takes inter beat interval (ibi) , compares them in pairs test variability in results. if comparison varies more 50ms hit counter increases 1. after 5 minutes number of hits compared total number of comparisons
here written out if typed doesn't make sense (where i'm looking calculate pnn50):
!! nn50, number of pairs of successive nns differ more 50 ms.
pnn50, proportion of nn50 divided total number of nns. !!

i wrote basic code first:

code: [select]
ibirecorded = false;
variationhits = 0;

 
 if (ibirecorded == false){ 
      currentibi = ibi;             // record 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 total number comparison in stopped working on way because worried comparison happen every time code ran (rather each time new ibi value inputted). - didn't have access hardware @ time test it.

to try find way out of have tried make modified version of following part of interrupt handler:
code: [select]
// keep running total of last 10 ibi values
      word runningtotal = 0;                  // clear runningtotal variable   

      for(int i=0; i<=8; i++){                // shift data in rate array
        rate[i] = rate[i+1];                  // , drop oldest ibi value
        runningtotal += rate[i];              // add 9 oldest ibi values
      }

      rate[9] = ibi;                          // add latest ibi rate array
      runningtotal += rate[9];                // add latest ibi runningtotal
      runningtotal /= 10;                     // average last 10 ibi values
      bpm = 60000/runningtotal;               // how many beats can fit minute? that's bpm!
      qs = true;                              // set quantified self flag
      // qs flag not cleared inside isr


and changed to

 
code: [select]
volatile int rate[2];                    // array hold last 2 ibi values
 int variationhits = 0;
 
 
 word runningtotal = 0;                  // clear runningtotal variable   

      for(int i=0; i<=1; i++){                // shift data in rate array
        rate[i] = rate[i+1];                  // , drop oldest ibi value
        runningtotal += rate[i];              // add oldest ibi value
      }

      rate[1] = ibi;                          // add latest ibi rate array
      runningtotal += rate[1];                // add latest ibi runningtotal

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


looking @ think there no need use array if i'm comparing 2 values each time.

the bit i'm not sure how make more simple code variables rather array.
how can write code comparison happens when new value sent via sensor?
how can count total number of comparisons made?

thanks help!

measuring stress given (non-overlapping) period done this
code: [select]

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 start 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 code test environment.


Arduino Forum > Using Arduino > Project Guidance > Stress detecting wearable project, simple coding question


arduino

Comments