Heart-rate Pulse Sensor for Arduino

Hello

RE: http://forum.arduino.cc/index.php?topic=71386.0

Im trying to take heart beat using sparkfun pulse sensor and send as MIDI via arduino uno.

below is my code and and i cant tell what is wrong with it…
any help is appreciated.

//Modified the pulse code to send MIDI command whenever a heart beat is detected.

// VARIABLES
int pulsePin = 0; // Pulse Sensor purple wire connected to analog pin 0
int blinkPin = 13; // pin to blink led at each beat
int fadePin = 5; // pin to do fancy classy fading blink at each beat
int fadeRate = 0; // used to fade LED on with PWM on fadePin


// these variables are volatile because they are used during the interrupt service routine!
volatile int BPM; // used to hold the pulse rate
volatile int Signal; // holds the incoming raw data
volatile int IBI = 600; // holds the time between beats, the Inter-Beat Interval
volatile boolean Pulse = false; // true when pulse wave is high, false when it's low
volatile boolean QS = false; // becomes true when Arduoino finds a beat.


void setup(){
 pinMode(blinkPin,OUTPUT); // pin that will blink to your heartbeat!
 pinMode(fadePin,OUTPUT); // pin that will fade to your heartbeat!
 Serial.begin(31250); // we agree to talk fast!
 interruptSetup(); // sets up to read Pulse Sensor signal every 2mS
 // UN-COMMENT THE NEXT LINE IF YOU ARE POWERING The Pulse Sensor AT LOW VOLTAGE,
 // AND APPLY THAT VOLTAGE TO THE A-REF PIN
 //analogReference(EXTERNAL);
}

void loop(){
 //disable sending all the Signal data as that might take too long
 //sendDataToProcessing('S', Signal); // send Processing the raw Pulse Sensor data
 if (QS == true){ // Quantified Self flag is true when arduino finds a heartbeat
   //add command to play note only when HB has been detected
   noteOn(0x099, 0x51, 0x45);
   fadeRate = 255; // Set 'fadeRate' Variable to 255 to fade LED with pulse
   sendDataToProcessing('B',BPM); // send heart rate with a 'B' prefix
   sendDataToProcessing('Q',IBI); // send time between beats with a 'Q' prefix
   QS = false; // reset the Quantified Self flag for next time
 }

 ledFadeToBeat();

 delay(20); // take a break
}


void ledFadeToBeat(){
 fadeRate -= 15; // set LED fade value
 fadeRate = constrain(fadeRate,0,255); // keep LED fade value from going into negative numbers!
 analogWrite(fadePin,fadeRate); // fade LED
}

void sendDataToProcessing(char symbol, int data ){
 Serial.print(symbol); // symbol prefix tells Processing what type of data is coming
 Serial.println(data); // the data to send culminating in a carriage return
}

// plays a MIDI note. Doesn't check to see that
// cmd is greater than 127, or that data values are less than 127:
void noteOn(int cmd, int pitch, int velocity) {
 Serial.write(cmd);
 Serial.write(pitch);
 Serial.write(velocity);
}


void interruptSetup(){
// Initializes Timer2 to throw an interrupt every 2mS.
TCCR2A = 0x02; // DISABLE PWM ON DIGITAL PINS 3 AND 11, AND GO INTO CTC MODE
TCCR2B = 0x06; // DON'T FORCE COMPARE, 256 PRESCALER
OCR2A = 0X7C; // SET THE TOP OF THE COUNT TO 124 FOR 500Hz SAMPLE RATE
TIMSK2 = 0x02; // ENABLE INTERRUPT ON MATCH BETWEEN TIMER2 AND OCR2A
sei(); // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

// Here is the Midi Code :

/*

The circuit:
* digital transmit 1 connected to MIDI jack pin 5
* MIDI jack pin 2 connected to ground
* MIDI jack pin 4 connected to +5V through 220-ohm resistor

*/

void Midi() {
// Set MIDI baud rate:
Serial.begin(31250);
}

void PlayNote() {
// play note (0x51)

//Note on channel 10 (0x99), 0x51, middle velocity (0x45):
noteOn(0x099, 0x51, 0x45);
delay(2000);

}

// plays a MIDI note. Doesn't check to see that
// cmd is greater than 127, or that data values are less than 127:
void noteOn(int cmd, int pitch, int velocity) {
Serial.write(cmd);
Serial.write(pitch);
Serial.write(velocity);
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

What is it supposed to do? What does it instead?

I just managed to make this sensor work for me:

https://tkkrlab.nl/wiki/Arduino_KY-039_Detect_the_heartbeat_module

Your sensor is more advanced, is it? And it had some sample code, where a led blinks to your heartbeats, right? Can you get the led blink? Can you make a separate program, where you get a MIDI event work, like playing a note? If you have success in these two separate tasks, then your problem is in combining two things in your code. If you fail in either task, then your problem is not how to combine two things.