I really hate to give an opinion unless I have tried the code and I'm not in a position to do that at the moment.
However, I'll stick my neck out and do it anyway.
In my view, the code doesn't work. Here's why:
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
counter++; ==== counter gets updated by one every time through loop(), ok
delay_x(5); ==== we wait 5 units of time each time through the loop
while(sensorValue=0) ==== did you mean (sensorValue == 0) Otherwise, you just set the value of sensorValue to 0
{ ==== further, if you did set the value to 0 then the loop would never be executed
lowEdge=counter; ==== counter is never updated because we are hung up in the while loop
} ==== and we're hung up in the loop because sensorValue never changes while we are hung in the while loop
do
{
highEdge=counter;
}while(sensorValue>0); ==== again, we are hung up in the do loop waiting on sensorValue to change and it never will because we are hung in the loop
double bpm=60/(highEdge-lowEdge)*5*pow(10,-3);
}
I know you are trying to make a superloop out of the code and that's fine. But you can't get hung up in loops and avoid reading the sensor.
I would probably make a state machine using a SWITCH statement that let me test for changes in sensorValue and process counter readings without getting stuck in a loop.
Next, I would probably use a hardware timer so I didn't have to rely on the delay function. I could just read the timer whenever the sensor changed values.
I'm not sure you can count on only a single digit change in the A/D converter. Testing for 0 or >0 would seem to me to be a problem. I would want to spread out the values like < 5 and > 10 so I had a little hysteresis.
enum states {IDLE, FOUND_LEADING_EDGE} state;
state = IDLE;
value = 0;
while (1) {
// big loop starts here
// read sensor and update counter
// code omitted
// now process the sensorValue
if (sensorValue > 10) // pick your own values for high and low
value = 1;
else
if (sensorValue < 5)
value = 0;
switch state {
case IDLE : if (value == 1) {
counter = 0;
state = FOUND_LEADING_EDGE;
}
break;
case FOUND_LEADING_EDGE
if (value == 0) {
print counter;
state = IDLE;
}
break;
}
} // end of while(1)
I'm not guaranteeing the syntax but the idea is that the state machine approach allows you to run through the loop, reading the sensor, updating counter and all that stuff without getting hung up in loops.
There's a reason I don't reply to this stuff without my handy dandy breadboard so, here it comes!