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.