Site Loader
Rock Street, San Francisco

/* definition of variables */#define PROCESSING_VISUALIZER 1int pin1 = 0;                int pin2 = 13; // blinks at each beat                        volatile int BPM;                   volatile int Signal; // holds incoming raw data           volatile int timeBetweenBeats = 600;             volatile boolean Pulse = false; volatile boolean beatHeard = false; volatile int rate10;     //array that holds last ten time in between beat values                  volatile unsigned long sampleCounter = 0;          volatile unsigned long lastBeatTime = 0;           volatile int peakInWave =512;                      volatile int troughInWave = 512;                     volatile int thresh = 530;  // find instant moment of heart beat             volatile int amplitude = 0;                   volatile boolean firstBeat = true;        volatile boolean secondBeat = false;  static int outputType = PROCESSING_VISUALIZER; //display to processing IDE/* serial handling code  */void serialOutput(){   sendDataToSerial(‘S’, Signal);}void serialOutputWhenBeatHappens(){  sendDataToSerial(‘B’,BPM);   // send heart rate with a ‘B’ prefix  sendDataToSerial(‘Q’,timeBetweenBeats);   // send time between beats with a ‘Q’ prefix  }void sendDataToSerial(char symbol, int data ){   //allows data to be processed by processing IDE    Serial.print(symbol);    Serial.println(data);  }/* setup function */void setup(){  pinMode(pin2 ,OUTPUT);                     Serial.begin(115200); //configured Baud Rate            interruptSetup(); //calls function that reads sensor every 2mS                 }/* loop function */void loop(){    serialOutput() ;  if (beatHeard == true){   //heart rate and time in between each beat can now be determined         serialOutputWhenBeatHappens();      beatHeard = false; //reset for the next time                        }                          }   /* the arduino uses Timer2 by default, but this ineteferes with PWM on pins 3 and 11   this initializes Timer2 to throw an interrupt every 2 milliseconds  */void interruptSetup(){    TCCR2A = 0x02;       TCCR2B = 0x06;       OCR2A = 0X7C;       TIMSK2 = 0x02;       sei();             }/* Timer2 interrupt service routine */ISR(TIMER2_COMPA_vect){  //triggered when Timer2 counts to 124                       cli();   //disable interrupts while we do this                                     Signal = analogRead(pin1  );                sampleCounter += 2;   //keep track of time                        int noise = sampleCounter – lastBeatTime;  //monitors time since the last beat       /* finds peak and trough of wave */  if(Signal < thresh && noise > (timeBetweenBeats/5)*3){        if (Signal < troughInWave){                             troughInWave = Signal;                            } } if(Signal > thresh && Signal > peakInWave){    //thresh condition helps avoid noise         peakInWave = Signal;                              }                                          /* looks for the heart beat */ if (noise > 250){                                     if ( (Signal > thresh) && (Pulse == false) && (noise > (timeBetweenBeats/5)*3) ){     Pulse = true;                                    digitalWrite(pin2,HIGH);                     timeBetweenBeats = sampleCounter – lastBeatTime;              lastBeatTime = sampleCounter;   // keeps track of time for next pulse                if(secondBeat){                              secondBeat = false;                          for(int i=0; i<=9; i++){                      ratei = timeBetweenBeats;        }     }     if(firstBeat){                               firstBeat = false;                         secondBeat = true;                         sei();  // enable interrupts again                                   return;                                   }      /* keeps running total of the last 10 in between beat values */      word runningTotal = 0;                        for(int i=0; i<=8; i++){                        ratei = ratei+1;    // store data in the rate array                      runningTotal += ratei;                   }      rate9 = timeBetweenBeats;   // add the latest in between beat value to the rate array                            runningTotal += rate9;                      runningTotal /= 10;           // average the last ten in between beat values            BPM = 60000/runningTotal;                     beatHeard = true;                                        }  }  /* when values are going down, beat is now over */  if (Signal < thresh && Pulse == true){       digitalWrite(pin2,LOW);                Pulse = false;                             amplitude = peakInWave - troughInWave;                               thresh = amplitude/2 + troughInWave;                        peakInWave = thresh;                                troughInWave = thresh;  }  if (noise > 2500){                               thresh = 530;                              peakInWave = 512;                                   troughInWave = 512;                                   lastBeatTime = sampleCounter;              firstBeat = true;                          secondBeat = false;                      }  sei();  // enables interrupts                             }  

Post Author: admin

x

Hi!
I'm Eric!

Would you like to get a custom essay? How about receiving a customized one?

Check it out