Go Down

Topic: Serial Printing problems (Read 3142 times) previous topic - next topic

tombruty

Hi All, 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!

Code: [Select]
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  

Digger450

At a glance I don't see you setting "timer0_overflow_count" to any thing besides 0.

tombruty

Unfortunately that doesn't make any difference. Even if I say;

"else if (timer0_overflow_count == 20000){"

is still doesn't work! Thanks anyway. Any other suggestions?

jims

I think what your code is doing is counting how many times through the loop that the heart pin was high. This will undoubtedly overflow your variable and look like a random number. You probably mean to count how many times it switches from 0 to 1.

You might also wish to initialize time to timer0_overflow_count+20000. It doesn't have to start at zero, it will be ticking away during program initialization.

tombruty

where would I put "timer0_overflow_count+20000"? Would it go in the if statement? I want to read off the value every 20 seconds. Thanks so much for getting back to me. I've been working all day on this!

mellis

You should really be using millis() instead of timer0_overflow_count (which is an internal variable).

In any case, what is it you're trying to count?  That is, what's hooked up to pin 12?

tombruty

#6
Mar 26, 2007, 05:07 pm Last Edit: Mar 26, 2007, 05:08 pm by tombruty Reason: 1
I'm trying to read the values sent by an LED that blinks on and off whenever a pulse is detected. I can see this value as long as I print it out in the void loop(). If I just tell it to print out to serial after a particular time it gives me a random number, sometimes with a - before it which doesn't make sense at all!

I've tried all of the above suggestions and no matter what I do it just doesn't work! Could it be a bug? Thanks to everyone who's helped so far. Are there any other suggestions? :'(

mellis

I believe jims is right.  You want to count the number of times the pin changes from low to high (or vice-versa).  Instead, you're counting the number of times through the loop() that the pin is high.  You need to remember the previous state of the pin and only increment heartDetect if the most recent reading is different than the previous one.  

tombruty

Thanks for getting back to me - again! How would I go about doing this roughly? I've only just started coding in Arduino!

tombruty

IT WORKS!!! ;D ;D ;D ;D ;D

Thanks so much to everyone who helped me out! It was what Jims suggested. I thought I was doing what was suggested in the first place and I wasn't at all. After Mellis re-suggested it I had a look at the code again and it turned out I wasn't doing it at all! I also scrapped the timer0_overflow_count system I was using and went back to Millis(). Thanks again,

Tom

Go Up