Trying to make arduino heart beat counter but couldn't understand code

I am trying to make an arduino heart beat counter . I looked for some code online and found this code on arduino.cc forum but I couldn't understand working of the the given code .. specifically these few lines of code. I took the code from this forum and i am going to leave a link to the page from where I took this code down below.

The full code is given below:

int sensorValue = 0;
int sensorValue = 0;  // variable to store the value coming from the sensor
int count=0;
unsigned long time1=0;  // store the initial time
unsigned long time2;    // store the current time 
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(sensorPin, INPUT);  
Serial.begin(9600);
}

void loop() {
// read the value from the sensor:
if(count==0)
 {time1=millis();
  }
 time2=millis();
sensorValue = analogRead(sensorPin);    
if(sensorValue>156)
  { increment();
   }
if(time2>=time1+10000)
 { counter();
  } 

}


void increment()
{count++;
 while(sensorValue>156)
  {Serial.print("Sensor value is greater than .75V\n");
   }
} 
void counter()
{ count=count*6;
  Serial.print("Heart beat is ");
  Serial.print(count);
  Serial.print(" per min\n");
  time1=0;
  time2=0;
  count=0;
}

The part of the code I'm not able to understand (PS:it is in void loop in the very beginning of void loop):

if(count==0)
   {time1=millis();
    }
    time2=millis();
  sensorValue = analogRead(sensorPin);    
  if(sensorValue>156)
   { increment();
    }
  if(time2>=time1+10000)
   { counter();
    }

if u are not able to understand the code i'm leaving a link to the page:this is the link

i would be highly obliged if anyone could really help me soon. Thank u in advance :slight_smile:

i am going to leave a link to the page

You seem to have forgotten something.

The code you posted won't even compile.l

1
down vote
favorite
I am trying to make a heart rate analyzer device using an Arduino board that will calculate the BPM of a person by using raw sensor signals greater or equal to the threshold value to calculate pulses/heartbeat in 10 seconds and then multiply the number of pulses by 6.

The circuit is fairly simple: it has a led that is going to flash on the finger of the person. In-front of the finger there is going to be a photoresistor that is going to read changing values of light falling on it. The values will be varying because of the blood flowing through the finger. When there is going to be a pulse/heart-beat the amount of light falling on the photoresistor will increase, hence the reading of the photoresistor will also increase.

If the raw values read by the sensor are equal to or greater than the threshold value the code will increment the number of pulses by 1. To calculate the heartbeat rate we will have to count the number of pulses/heart-beats for 10 seconds and then multiply the count by 6.

In short the code that I will use will convert raw analog sensor data into BPM (Beats Per Minute) so that a user can get a standard measurement of his pulse rate.

I took the code that I am going to use from a website. Here is a link to page so that you can also have a look at the code.

I had some problem in the code and was wondering if anyone could help me with it. The full code is given below:

 int sensorValue = 0;
int sensorValue = 0;
// variable to store the value coming from the sensor
int count=0;
unsigned long time1=0; // store the initial time
unsigned long time2; // store the current time

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // read the value from the sensor:
  if(count==0) {
    time1=millis();
  }
  time2=millis();
  sensorValue = analogRead(sensorPin);
  if(sensorValue>156) {
    increment();
  }
  if(time2>=time1+10000) {
    counter();
  }
}

void increment() {
  count++;
  while(sensorValue>156) {
    Serial.print("Sensor value is greater than .75V\n");
  }
}

void counter() {
  count=count*6;
  Serial.print("Heartbeat rate is ");
  Serial.print(count);
  Serial.print(" per min\n");
  time1=0;
  time2=0;
  count=0;
}

Part of the code I'm not able to understand. This is located at the very top of void loop():

[note: i pretty much understand how increment and counter functions work but i am not able to understand how the program is able to take input for 10 seconds.basically how the counter function runs for 10 seconds]

  if(count==0) {
    time1=millis();
  }
  time2=millis();
  sensorValue = analogRead(sensorPin);
  if(sensorValue>156) {
    increment();
  }
  if(time2>=time1+10000) {
    counter();

Although I know that this part of the code that I am not able to understand counts the pulses for 10 seconds by taking raw sensor values and checking if these values are above or equal to the threshold value, I am not able to understand how it is working.

I would be highly obliged if anyone could help me with this problem of mine

time1 is set set to the current value of millis() if count is 0, then each tome through loop() time2 is set to the current value of millis(). If time2 is greater or equal to time1 (the start time) plus 10,000 milliseconds (ie 10 seconds) then the timing period is up

@Shantanu9daf, do not cross-post. Threads merged.

UKHeliBob hey I still cannot understand the code

if(count==0) {
    time1=millis();
  }
time2=millis();// r these lines part of else statement??
  sensorValue = analogRead(sensorPin);  //this one too??

Are these lines part of else statement??

time2=millis();// r these lines part of else statement??
  sensorValue = analogRead(sensorPin);

And this if statement:

 if(time2>=time1+10000) {
    counter();

UKHeliBob will time1 be equal to 1 milliseconds or if not can u tell me it's value . And suppose time2 will be equal to 10 milliseconds then the condition will not stand true as:
10 <10001
Please tell me the correct values if I can UKHeliBob.
Please help me this time I going crazy on this project.

Please do not start new threads asking the same question

I will ask the moderators to merge this and Trying to make arduino heart beat counter but couldn't understand code - #3 by Shantanu9daf - Programming Questions - Arduino Forum

Threads merged. Again.

Ok UKHeliBob ... I'm not going to merge threads anymore .. here is my problem again:
UKHeliBob hey I still cannot understand the code

if(count==0) {
    time1=millis();
  }
time2=millis();// r these lines part of else statement??
  sensorValue = analogRead(sensorPin);  //this one too??

Are these lines part of else statement??

time2=millis();// r these lines part of else statement??
  sensorValue = analogRead(sensorPin);

And this if statement:

 if(time2>=time1+10000) {
    counter();

UKHeliBob will time1 be equal to 1 milliseconds or if not can u tell me it's value . And suppose time2 will be equal to 10 milliseconds then the condition will not stand true as:
10 <10001
Please tell me the correct values if I can UKHeliBob.
Please help me this time I going crazy on this project.

Ok UKHeliBob ... I'm not going to merge threads anymore .. here is my problem again:
UKHeliBob hey I still cannot understand the code

if(count==0) {
    time1=millis();
  }
time2=millis();// r these lines part of else statement??
  sensorValue = analogRead(sensorPin);  //this one too??

Are these lines part of else statement??

time2=millis();// r these lines part of else statement??
  sensorValue = analogRead(sensorPin);

And this if statement:

if(time2>=time1+10000) {
    counter();

UKHeliBob will time1 be equal to 1 milliseconds or if not can u tell me it's value . And suppose time2 will be equal to 10 milliseconds then the condition will not stand true as:
10 <10001
Please tell me the correct values if I can UKHeliBob.
Please help me this time I going crazy on this project.

Are these lines part of else statement??

There is no else statement in the portion of code that you posted, so no.

will time1 be equal to 1 milliseconds or if not can u tell me it's value

time1 will be equal to the value of millis() when the value was assigned to it. The value will, therefore, vary. The timing in the code works by testing whether the required period has elapsed between time1 and time2. The actual values of time1 and time2 do not matter, just the difference between them.

Look at this simple example

unsigned long startTime = 0;
unsigned long currentTime = 0;
unsigned long period = 10000;  //10 seconds

void setup()
{
  Serial.begin(115200);
  Serial.println("waiting");
}

void loop()
{
  currentTime = millis();  //get the current millis() value, whatever it is
  if (currentTime - startTime >= period)  //is the difference greater than the required period ?
  {
    Serial.println("\tIt's time to do something");
    startTime = currentTime;    //save new start time for next period
    Serial.println("waiting");
  }
}

It prints a message every 10 seconds.

Ok UKHeliBob ... I'm not going to merge threads anymore .. here is my problem again:
UKHeliBob hey I still cannot understand the code

if(count==0) {
    time1=millis();
  }
time2=millis();// r these lines part of else statement??
  sensorValue = analogRead(sensorPin);  //this one too??

Are these lines part of else statement??

time2=millis();// r these lines part of else statement??
  sensorValue = analogRead(sensorPin);

And this if statement:

if(time2>=time1+10000) {
    counter();

UKHeliBob will time1 be equal to 1 milliseconds or if not can u tell me it's value . And suppose time2 will be equal to 10 milliseconds then the condition will not stand true as:
10 <10001
Please tell me the correct values if I can UKHeliBob.
Please help me this time I going crazy on this project.

I still cannot understand the code

That's YOUR problem. The code has been explained to you more than often enough.

Are these lines part of else statement??

How the f**k can they be when you don't HAVE an else statement?

will time1 be equal to 1 milliseconds

How can ANYONE know that? That statement does NOT do something is time1 is equal to any specific value. It is (incorrectly) comparing the difference between two values.

I going crazy on this project.

You misspelled gone.

Shantanu9daf:
ell me the correct values if I can UKHeliBob.
Please help me this time I going crazy on this project.

I'm not UKHeliBob, but I can tell you this:

The code you are trying to use is using a CRAZY ALGORITHM and will only work under certain conditions, when used with the SAME SENSOR, SAME PERSON, SAME FINGER AND SAME POSITION OF FINGER.

If you change anything (different sensor, different person, different finger, different position of finger, YOUR ALGORITHM WILL FAIL. It's much too simple.

Your algorighm seems to be based on a fixed value of analogRead():

if(sensorValue>156)
  { increment();
   }

It shall work like that' If, after a certain amount of time, the analogRead value increases above 156, then a heartbeat is detected.

This 156 value is a fixed value in the code.

Though being very simple, this is the ABSOLUTELY WORST "heartbeat detection" algorithm I ever saw.

You cannot do it that simple if you want the same code to work with:

  • different type of sensors (red LED diode or green LED diode or infrared IR LED diode)
  • different sensors (different calibration values)
  • different f persons
  • different fingers
  • different positions of finger in relation to the light detecting photodiode

So better forget about the code you posted when starting this topic!

Question: Do you want to discuss about suitable algorithms for heartbeat detection?

In case yes: Be prepared that it becomes much more complicated!

Do you want to discuss suitable algorithms for heartbeat detection? Yes or no?

I think that I asked a similar question to several dozens of topic starters within the last five year, but all of them were looking for ready-to-use Arduino code. None of them wanted to discuss anything about signal processing or heartbeat detection. ALL of them were looking just for "copy-and-paste" code, made by other persions, that they might be able to use instantly without any change and even without understanding anything.

What about you?
Are you interested in discussing algorithms and developing code?
Or are you just interestd in copying-and-pasting ready-to-use code?

thank u very very much UKHeliBob ... i finally figured out the code. i owe u big time.
thanks again :slight_smile: