Programming problem with delay.

Hello! I had a problem while I was programming and I can´t solve it. I am taking an analog value from my arduino and I am graphing it. The problem is that this code works perfectly when the delay is aproximately of 1,000 but it starts to do weird stuff when I put the delay on 10. The problem is that when I put a delay of 10 it counts the value interval several times when it suppose to count it only once in each interval (For example when you use a delay of 100), because in the first loop that it enters the “if” we change the variable or Flag or however it’s said, you can see this better in the code below.

I need to leave the delay on 10 so the graph of the value can be more exact. Anyone knows what is the problem?

Here is the code:

const int treshold = 450; //Aproximated value of the treshold
const int FotoplesPin = A0; //Pin of the Analogic Signal.
const int TRUE = 1;
const int FALSE = 0;

int BPM = 0; // BPM counter.
int timer = 0; //Variable for counting.
int heartRate; //Actual Heart Rate Signal
int peak = TRUE; //Previous Heart Rate Signal

void setup(){
Serial.begin(9600); //for the processing sketch
}
void loop(){
heartRate = analogRead(FotoplesPin); // Analog value of the Heart Rate.
heartRate = map(heartRate, 0, 1023, 200, 650);

if ((heartRate > treshold) && (peak == TRUE)){ // If there is a peak
BPM++; // We have 1 beat.
peak = FALSE; // We make it TRUE until the analog value it gets another peak.
}

else if ((heartRate < treshold)){ // If there is a peak
peak = TRUE;
}

Serial.print(heartRate);
Serial.print(",");
Serial.print(BPM);
Serial.print(",");
Serial.println();

delay(10); // Delay between each loop.
}

Weird stuff. You want me to guess at what that weird stuff is?

Do you go to the doctor and just say, "I feel weird, what's wrong with me?" Come on. Surely you know you gotta give us some symptoms to work with.

And really man, how many times have you been asked to put code tags around your code to make it easy to read?

What are you doing, trying to make it as hard as possible for anyone to help you? How does that make any sense?

Don´t be mean. Not all people are experts, I´m new in this.

The problem is that when I put a delay of 10 it counts the value interval several times when it suppose to count it only once in each interval (For example when you use a delay of 100), because in the first loop that it enters the “if” we change the variable or Flag or however it’s said.

The problem is that when I put a delay of 10 it counts the value interval several times when it suppose to count it only once in each interval

How do you know that it is counting several times in each interval ?

luis_ag: Don´t be mean. Not all people are experts, I´m new in this.

It has nothing to do with how new you are to programming. When you want help with something you should tell the people you are asking what's wrong. That's just common sense.

I don't see anything in your code called interval. What is this interval you speak of?

Have you tried looking at the sensor output with a scope? There may be some noise in the signal such that it is going above and below the threshold more than once on each peak.

How many charactares are you outputting via the Serial ? How long does it take to output one character at 9600 baud - a little more than 1 ms.

Weird.