I am a newbie in arduino programming and constructed a circuit in order to measure GSR and Heart rate.For heart rate measuring I used an algorithm and a circuit similar to pulsesensor.com.The measuring of heart rate was not stable (sometimes it was functioning well and sometimes the values were very high). I tried to use PWM and the measuring seemed to be much more stable.Could you explain to me if PWM can affect the way that the arduino is functioning?

/*This program reads data from the Pulse Sensor. Serial Protocol initiates datastring with coded ascii character, ends each message with carriage returnWe named the variable that holds the heart rate (BPM) after the group Quantified Self.*/

// VARIABLESunsigned long time; // Holds current time for pulse rate calculationunsigned long lastTime; // Used for calculating time between beatsint Sensor; // Holds current analog Sensor Readingint lastSensor=0; // Used to find waveform directionint Peak; // Holds value of peak in waveformint Trough; // Holds value of trough in waveformint beats[10]; // Array to collect time between beats for calculating BPMint beatCounter = 0; // Used to hold position in beats arrayint QuantifiedSelf; // Used to hold the heart rate value (BPM)int drop; // Holds the amplitude of waveformint Fade = 0; // Fade variable will set PWM int fadeRate = 10; // when arduino finds a heartbeat, it will fade an LED on pin 11 (PWM)

Serial.begin(57600); // start the hardware serial block and set the baud rate

lastTime = millis(); // initialize lastTime variable

}

void loop(){ // USE WITH LED ON PIN 11 FOR FADE EFFECT Fade -= fadeRate; // Fade variable set to 255 when heart beat is found Fade = constrain(Fade,0,191); // these lines fade the LED analogWrite(dimLED,Fade);

// KEEP TRACK OF THE DIRECTION OF THE WAVEFORM if (falling == false){ // if the sensor values are rising if (Sensor < lastSensor-1){ // if current reading is less than last reading - noise falling = true; // a peak has been reached

I can't see why PWM would change whether or not the code will detect heart rate. I'm not sure that your heartbeat detection method is a very effective way of doing it but, for sure, there's a bug. In getBPM() you need to initialize mean to zero:

int mean = 0; // used in averagingotherwise it is averaging the four readings plus a random value. It is possible that whatever you added to do PWM has changed what is on the stack when getBPM is called and this made it work better.

I've been browsing the web and came across some other code. Have you seen or tried either of these?http://code.google.com/p/pulse-sensor/downloads/list?saved=1&ts=1324407501

See Step 1 for link to code. You'd have to extract the heartbeat detection part.http://makeprojects.com/Project/Beating-Heart-Headband/1622/1