need to find the difference between the BPM before and after

I am doing a project that detect a change between the heartbeat before and after that display at serial monitor and trigger the output if a rapid change happen...but my program only give out the difference between heartbeat before a second and after a second...can someone give some advice for me??

pulse_sensor_3.ino (8.47 KB)

I am doing a project that detect a change between the heartbeat before and after that display at serial monitor

Before and after what?

can someone give some advice for me??

Yes. Sentences start with a capital letter and end with ONE punctuation symbol.

PaulS:
Before and after what?
Yes. Sentences start with a capital letter and end with ONE punctuation symbol.

Sorry for the bad sentences. This mistake has became my habit. The word "before and after" means the initial heartbeat and heartbeat after 2ms. My LCD displays the BPM within the duration of 1s, so I need to change the program in which part so that my program can find out the difference between the heartbeat in 1s. I found this program in the Internet and changed a little bit.

The word "before and after" means the initial heartbeat and heartbeat after 2ms.

A heartbeat is a discrete event.

The heart rate is the number of beats in some unit of time, typically one minute, although one does not count the number of heartbeats for one minute to determine the rate. One counts the number of beats for a shorter interval and extrapolates.

Now, given that the typical resting heart rate for healthy people is 60 to 100 beats per minute, or one or two per second, it is completely ludicrous to be talking about measuring the heart rate in 2 millisecond intervals.

PaulS:
Now, given that the typical resting heart rate for healthy people is 60 to 100 beats per minute, or one or two per second, it is completely ludicrous to be talking about measuring the heart rate in 2 millisecond intervals.

I might misunderstand the code. Is the code written to display the heartbeat repeatedly in the range of 2 millisecond?

YAWLOL:
I might misunderstand the code. Is the code written to display the heartbeat repeatedly in the range of 2 millisecond?

No. The code is looking at the sensor every 2 milliseconds, to see where in the waveform it is, so that it can identify the peak, to know WHEN a heartbeat occurred.

PaulS:
No. The code is looking at the sensor every 2 milliseconds, to see where in the waveform it is, so that it can identify the peak, to know WHEN a heartbeat occurred.

So, which part of the code that I need to change or add so that I can find the difference between them?

Besides, thank you for replying me.

So, which part of the code that I need to change or add so that I can find the difference between them?

The difference between what? It is not at all clear what you are asking about.

PaulS:
The difference between what? It is not at all clear what you are asking about.

It is the difference between the heartbeat which sensed by the pulse sensor. I delay the display of heartbeat to 1 second, but arduino find the difference between the heartbeat in 2ms. How I can change the code to sense the difference between the heartbeat in 1 second?

Right now your code is displaying the change in BPM which it holds in difbeat. The only reason it doesn't display as frequently as you'd like is the delay(5000) in loop.

However, unless your heart is beating rapidly, you may need to capture data for several seconds to get meaningful data.

It is the difference between the heartbeat which sensed by the pulse sensor.

What is?

You are completely, apparently, confusing a heart beat, a discrete event, and a heart rate - the number of discrete events in some period of time.

I delay the display of heartbeat to 1 second

You do not delay anything. You periodically display the RATE (NOT an event) once per second.

but arduino find the difference between the heartbeat in 2ms.

Do you have ANY idea what a heartbeat looks like? Earlier, I referred to it as a discrete event, but it is really a complex series of contractions of the muscles in the heart. What you would see on a scope, on an EKG machine, is something that looks vaguely like a sine wave. The curve is continuous, with peaks and valleys. Since the Arduino needs to be doing other things, it can't look at the signal continuously. The best it can do is look at the signal periodically (with 2 milliseconds being the period).

It looks at the signal that often so that it can detect when a peak occurs. When the value of the signal, at some point in time, is more that the value last time, the value has NOT reached a peak. So, the Arduino goes off and does other stuff.

Two milliseconds later, it looks again. This time, maybe the signal is the same as last time, so the signal is either at a peak or at a trough. So, the Arduino goes off and does other stuff.

Two milliseconds later, it looks again. This time, maybe the signal is less than last time, so last time's value WAS a peak. The Arduino then notes that a peak has occurred (ONE heartbeat has happened).

Part of what the Arduino does when it goes away is to determine if it is time to report the number of beats (the number of discrete events) that have happened in some period of time, extrapolated to a more normal unit of time. That is, it calculates that 19 beats have occurred in the last 15 seconds, to that corresponds to 76 beats in 60 seconds.

The Arduino is NOT "find the difference between the heartbeat in 2ms.". That doesn't even make sense.

PaulS:
What is?

You are completely, apparently, confusing a heart beat, a discrete event, and a heart rate - the number of discrete events in some period of time.
You do not delay anything. You periodically display the RATE (NOT an event) once per second.
Do you have ANY idea what a heartbeat looks like? Earlier, I referred to it as a discrete event, but it is really a complex series of contractions of the muscles in the heart. What you would see on a scope, on an EKG machine, is something that looks vaguely like a sine wave. The curve is continuous, with peaks and valleys. Since the Arduino needs to be doing other things, it can't look at the signal continuously. The best it can do is look at the signal periodically (with 2 milliseconds being the period).

It looks at the signal that often so that it can detect when a peak occurs. When the value of the signal, at some point in time, is more that the value last time, the value has NOT reached a peak. So, the Arduino goes off and does other stuff.

Two milliseconds later, it looks again. This time, maybe the signal is the same as last time, so the signal is either at a peak or at a trough. So, the Arduino goes off and does other stuff.

Two milliseconds later, it looks again. This time, maybe the signal is less than last time, so last time's value WAS a peak. The Arduino then notes that a peak has occurred (ONE heartbeat has happened).

Part of what the Arduino does when it goes away is to determine if it is time to report the number of beats (the number of discrete events) that have happened in some period of time, extrapolated to a more normal unit of time. That is, it calculates that 19 beats have occurred in the last 15 seconds, to that corresponds to 76 beats in 60 seconds.

The Arduino is NOT "find the difference between the heartbeat in 2ms.". That doesn't even make sense.

PaulS:
What is?

You are completely, apparently, confusing a heart beat, a discrete event, and a heart rate - the number of discrete events in some period of time.
You do not delay anything. You periodically display the RATE (NOT an event) once per second.
Do you have ANY idea what a heartbeat looks like? Earlier, I referred to it as a discrete event, but it is really a complex series of contractions of the muscles in the heart. What you would see on a scope, on an EKG machine, is something that looks vaguely like a sine wave. The curve is continuous, with peaks and valleys. Since the Arduino needs to be doing other things, it can't look at the signal continuously. The best it can do is look at the signal periodically (with 2 milliseconds being the period).

It looks at the signal that often so that it can detect when a peak occurs. When the value of the signal, at some point in time, is more that the value last time, the value has NOT reached a peak. So, the Arduino goes off and does other stuff.

Two milliseconds later, it looks again. This time, maybe the signal is the same as last time, so the signal is either at a peak or at a trough. So, the Arduino goes off and does other stuff.

Two milliseconds later, it looks again. This time, maybe the signal is less than last time, so last time's value WAS a peak. The Arduino then notes that a peak has occurred (ONE heartbeat has happened).

Part of what the Arduino does when it goes away is to determine if it is time to report the number of beats (the number of discrete events) that have happened in some period of time, extrapolated to a more normal unit of time. That is, it calculates that 19 beats have occurred in the last 15 seconds, to that corresponds to 76 beats in 60 seconds.

The Arduino is NOT "find the difference between the heartbeat in 2ms.". That doesn't even make sense.

Sorry for my bad english. I finally get your meaning after this comment. Thank you. So what I need to find is the heart rate but not heartbeat. the difference that I want to find is also the heart rate. Is that right?

So what I need to find is the heart rate but not heartbeat.

You don't find the heart rate. You calculate the heart rate, either by counting the number of heart beats in some period of time (typically 15 seconds) and scaling to the desired interval (usually one minute), or by recording when each occurs, and when you have the times for two heart beats, you calculate the average number that would occur in the desired period of time (usually one minute) assuming that the interval between beats remains constant.

I think, since you seem to be looking for changes in rate, that you need to use the second approach, so that you can detect decreasing intervals (increasing heart rate).

PaulS:
You don't find the heart rate. You calculate the heart rate, either by counting the number of heart beats in some period of time (typically 15 seconds) and scaling to the desired interval (usually one minute), or by recording when each occurs, and when you have the times for two heart beats, you calculate the average number that would occur in the desired period of time (usually one minute) assuming that the interval between beats remains constant.

I think, since you seem to be looking for changes in rate, that you need to use the second approach, so that you can detect decreasing intervals (increasing heart rate).

Can I change the time intervals for the arduino to detect the signal of the sensor from 2 ms to 1 s? If it is okay, I need to change which part of the code?

Can I change the time intervals for the arduino to detect the signal of the sensor from 2 ms to 1 s?

Yes. but why? At 60 beats per minute (a typical resting rate), a single heartbeat takes about 1000 milliseconds. Checking more frequently for a peak might get you a point closer to the peak, but a single heartbeat does not have an all-that-well defined peak.

Being a tiny bit off the peak usually doesn't matter.

If it is okay, I need to change which part of the code?

The part that sets up the timer to trigger the interrupt.

  OCR2A = 0X7C;      // SET THE TOP OF THE COUNT TO 124 FOR 500Hz SAMPLE RATE

0x7C is 124 in base 10. To trigger twice as often you'd count up half as much.

PaulS:
Yes. but why? At 60 beats per minute (a typical resting rate), a single heartbeat takes about 1000 milliseconds. Checking more frequently for a peak might get you a point closer to the peak, but a single heartbeat does not have an all-that-well defined peak.

Being a tiny bit off the peak usually doesn't matter.
The part that sets up the timer to trigger the interrupt.

  OCR2A = 0X7C;      // SET THE TOP OF THE COUNT TO 124 FOR 500Hz SAMPLE RATE

0x7C is 124 in base 10. To trigger twice as often you'd count up half as much.

If I set the code that put the BPM into 5 array and compare it to find the highest BPM and put that highest BPM into another array, is that ok? For example, I got 5 readings of BPM in 10 ms, like 76, 65, 78, 87 and 90 and find the highest BPM which is 90 bpm and put this into highest BPM array. Next, I got another 5 readings, which are 87, 95, 87, 98 and 103 and find out the highest BPM (13 bpm) and put it into highest BPM array. Next, I subtract the first highest reading with the second highest reading to find the difference.

If I set the code that put the BPM into 5 array and compare it to find the highest BPM and put that highest BPM into another array, is that ok?

You don't need our permission to do that.

YAWLOL:
If I set the code that put the BPM into 5 array and compare it to find the highest BPM and put that highest BPM into another array, is that ok? For example, I got 5 readings of BPM in 10 ms, like 76, 65, 78, 87 and 90 and find the highest BPM which is 90 bpm and put this into highest BPM array. Next, I got another 5 readings, which are 87, 95, 87, 98 and 103 and find out the highest BPM (13 bpm) and put it into highest BPM array. Next, I subtract the first highest reading with the second highest reading to find the difference.

Your code is already doing most of this. Attach yourself to the sensor & run the sketch with the serial monitor operating. Jump up & down a bit and observe what happens to BPM and difbeat.

I got 5 readings of BPM in 10 ms,

Do you really think your heart rate changes that much in 10 milliseconds? That's not even long enough to measure one beat. You'd have to measure at least two beats to calculate a rate. So the minimum time between measurements will be on the order of seconds, not milliseconds.

So the minimum time between measurements will be on the order of seconds, not milliseconds.

At less than 60 beats per minute, that will be true. But, very few people have resting heart rates that low.

So, the time will be on the order of hundreds of milliseconds between beats, for most people, most of the time.