Serial Problems

Hi All, sorry for the cross posting but I'm desperate!can someone please try and point out what I'm doing wrong! I'm trying to print out the heart rate that I'm detecting and passing into Arduino. I can't get it to print properly and I've tried everything!

int heart = 12; //digital pin being used
int heartDetect=0; // detects the value of digital pin 12 
int heartRate = 0; // Assigned the value of heartDetect added to itself over a period of time.
int time = 20000; // The time value that I'm using.
extern volatile unsigned long timer0_overflow_count; //Sets the timer

void setup()
{
  Serial.begin(300);//need to use a slow serial    
  pinMode(heart, INPUT);//declare pin 12 as an INPUT
}

void loop()
{
  heartDetect = digitalRead(heart); //heartDetect is assigned the value of pin 12

  if (timer0_overflow_count < time){ //If it's less than 20 seconds
    heartRate = heartRate + heartDetect; //Add the value of heartRate to itself plus the value of heartDetect
    //Serial.print("  ");
    //Serial.print(heartRate, DEC);
    //Serial.print(0, BYTE);
  }
  else if (timer0_overflow_count > time){ //else If it's over 20 seconds
    Serial.print(" ");
    Serial.print(heartRate, DEC); //print out the value of heartRate
    Serial.print(0, BYTE);
    timer0_overflow_count = 0; //reset the timer
    heartRate = 0; //reset heartRate
  }                 
}

For some reason, the only way I can get the value of 'heartRate' to print to serial in the 'else if' statement is to un-comment the serial.print() lines within the 'if' statement. If I don't do this i get a random value assigned to heartRate.

I only want the value of 'heartRate' to be printed to the serial every 20 seconds. I would really appreciate any comments if you have an idea of how I could get around this problem. Thanks,

Tom

You probably don't need to use timer0_overflow_count. Can you use millis() instead? Instead of resetting the time to 0 after 20 seconds, you should remember the time when you print out the heartbeat value. Then you can check if 20 seconds have elapsed:

if (millis() - previousPrintTime > 20000)

Make sure that previousPrintTime is declared as an unsigned long.

Also, what are you using to read the serial data on the computer (have you checked it in the Arduino serial monitor)? Have you tried sending a newline (with Serial.println()), instead of the byte 0? Why such a slow baud rate?