Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.70 KB | None | 0 0
  1. volatile int rate[10];                    // array to hold last ten IBI values
  2. volatile unsigned long sampleCounter = 0;          // used to determine pulse timing
  3. volatile unsigned long lastBeatTime = 0;           // used to find IBI
  4. volatile int P =512;                      // used to find peak in pulse wave, seeded
  5. volatile int T = 512;                     // used to find trough in pulse wave, seeded
  6. volatile int thresh = 530;                // used to find instant moment of heart beat, seeded
  7. volatile int amp = 0;                   // used to hold amplitude of pulse waveform, seeded
  8. volatile boolean firstBeat = true;        // used to seed rate array so we startup with reasonable BPM
  9. volatile boolean secondBeat = false;      // used to seed rate array so we startup with reasonable BPM
  10. /*
  11.  
  12. void interruptSetup(){  // CHECK OUT THE Timer_Interrupt_Notes TAB FOR MORE ON INTERRUPTS
  13.   // Initializes Timer2 to throw an interrupt every 2mS.
  14.   TCCR2A = 0x02;     // DISABLE PWM ON DIGITAL PINS 3 AND 11, AND GO INTO CTC MODE
  15.   TCCR2B = 0x06;     // DON'T FORCE COMPARE, 256 PRESCALER
  16.   OCR2A = 0X7C;      // SET THE TOP OF THE COUNT TO 124 FOR 500Hz SAMPLE RATE
  17.   TIMSK2 = 0x02;     // ENABLE INTERRUPT ON MATCH BETWEEN TIMER2 AND OCR2A
  18.   sei();             // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED
  19. }
  20. */
  21.  
  22. // THIS IS THE TIMER 2 INTERRUPT SERVICE ROUTINE.
  23. // Timer 2 makes sure that we take a reading every 2 miliseconds
  24. void getPulse(){                         // triggered when Timer2 counts to 124
  25.   Serial.println("GET pULSE");
  26.   cli();                                      // disable interrupts while we do this
  27.   Signal = analogRead(pulsePin);              // read the Pulse Sensor
  28.   sampleCounter += 2;                         // keep track of the time in mS with this variable
  29.   int N = sampleCounter - lastBeatTime;       // monitor the time since the last beat to avoid noise
  30.  
  31.     //  find the peak and trough of the pulse wave
  32.   if(Signal < thresh && N > (IBI/5)*3){       // avoid dichrotic noise by waiting 3/5 of last IBI
  33.     if (Signal < T){                        // T is the trough
  34.       T = Signal;                         // keep track of lowest point in pulse wave
  35.     }
  36.   }
  37.  
  38.   if(Signal > thresh && Signal > P){          // thresh condition helps avoid noise
  39.     P = Signal;                             // P is the peak
  40.   }                                        // keep track of highest point in pulse wave
  41.  
  42.   //  NOW IT'S TIME TO LOOK FOR THE HEART BEAT
  43.   // signal surges up in value every time there is a pulse
  44.   if (N > 250){                                   // avoid high frequency noise
  45.     if ( (Signal > thresh) && (Pulse == false) && (N > (IBI/5)*3) ){
  46.       Pulse = true;                               // set the Pulse flag when we think there is a pulse
  47.       digitalWrite(D4,HIGH);                // turn on pin 13 LED
  48.       IBI = sampleCounter - lastBeatTime;         // measure time between beats in mS
  49.       lastBeatTime = sampleCounter;               // keep track of time for next pulse
  50.  
  51.       if(secondBeat){                        // if this is the second beat, if secondBeat == TRUE
  52.         secondBeat = false;                  // clear secondBeat flag
  53.         for(int i=0; i<=9; i++){             // seed the running total to get a realisitic BPM at startup
  54.           rate[i] = IBI;
  55.         }
  56.       }
  57.  
  58.       if(firstBeat){                         // if it's the first time we found a beat, if firstBeat == TRUE
  59.         firstBeat = false;                   // clear firstBeat flag
  60.         secondBeat = true;                   // set the second beat flag
  61.         sei();                               // enable interrupts again
  62.         return;                              // IBI value is unreliable so discard it
  63.       }
  64.  
  65.  
  66.       // keep a running total of the last 10 IBI values
  67.       word runningTotal = 0;                  // clear the runningTotal variable
  68.  
  69.       for(int i=0; i<=8; i++){                // shift data in the rate array
  70.         rate[i] = rate[i+1];                  // and drop the oldest IBI value
  71.         runningTotal += rate[i];              // add up the 9 oldest IBI values
  72.       }
  73.  
  74.       rate[9] = IBI;                          // add the latest IBI to the rate array
  75.       runningTotal += rate[9];                // add the latest IBI to runningTotal
  76.       runningTotal /= 10;                     // average the last 10 IBI values
  77.       BPM = 60000/runningTotal;               // how many beats can fit into a minute? that's BPM!
  78.       QS = true;                              // set Quantified Self flag
  79.       // QS FLAG IS NOT CLEARED INSIDE THIS ISR
  80.     }
  81.   }
  82.  
  83.   if (Signal < thresh && Pulse == true){   // when the values are going down, the beat is over
  84.     digitalWrite(D4,LOW);            // turn off pin 13 LED
  85.     Pulse = false;                         // reset the Pulse flag so we can do it again
  86.     amp = P - T;                           // get amplitude of the pulse wave
  87.     thresh = amp/2 + T;                    // set thresh at 50% of the amplitude
  88.     P = thresh;                            // reset these for next time
  89.     T = thresh;
  90.   }
  91.  
  92.   if (N > 2500){                           // if 2.5 seconds go by without a beat
  93.     thresh = 530;                          // set thresh default
  94.     P = 512;                               // set P default
  95.     T = 512;                               // set T default
  96.     lastBeatTime = sampleCounter;          // bring the lastBeatTime up to date
  97.     firstBeat = true;                      // set these to avoid noise
  98.     secondBeat = false;                    // when we get the heartbeat back
  99.   }
  100.  
  101.   sei();                                   // enable interrupts when youre done!
  102. }// end isr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement