I am using Arduino Nano, ATmega328p (Old bootloader) to program my 3-pin photodiode Pulse Sensor. I have fine-tuned the threshold to the value of 403 in my case by using the example code from pulsesensor.com. The code works fine, where it blinks once a pulse is detected. However, I have started facing problems when I want to get the BPM. I have used the code below to count the pulse for 10 seconds and will multiply by 6 at the end to get BPM. The problem I faced is there will be a two to three extra count for the pulse every time it exceeds the threshold, but what I want is just to take a single count every time it exceeds the threshold.
//Pulse sensor model = SEN-11574 brought from shopee (3pin - Vcc, Gnd, Signal)
// Variables
int PulseSensorSignalPin = 0; // Pulse Sensor SIGNAL WIRE (white colour wire in my case) connected to ANALOG PIN 0
int LED13 = 13; // The on-board Arduion LED (Arduino Nano Built-in Led in my case)
int currSignal; // holds the incoming raw data. Signal value can range from 0-1023 (1024 values in total)
int prevSignal;
int Threshold = 403; // Determine which Signal to "count as a beat", and which to ingore. (in my case is 402)
int BPM = 0;
int Pulse = 0;
bool countedPulse = false; // Initially save as false as no pulse have been counted.
const unsigned long eventInterval = 10000;
unsigned long startTime = 0; // unsigned - store variables neglecting the +- sign, long - larger bytes for storing variable
void setup() {
Serial.begin(115200); // Set's up Serial Communication at certain speed.
pinMode(LED13, OUTPUT); // pin that will blink to your heartbeat!
digitalWrite(LED13, LOW); // Set initial condition for LED - initially not blinking.
}
void loop() {
startTime = millis(); // let startTime = millis() that will keep update (not sure work or not)
while (millis() < startTime + 10000) {
currSignal = analogRead (PulseSensorSignalPin);
if (currSignal > Threshold && prevSignal < Threshold) {
if (countedPulse == false) {
Pulse++;
Serial.print("Pulse = ");
Serial.println(Pulse);
digitalWrite(LED13, HIGH);
delay(20);
digitalWrite(LED13, LOW);
countedPulse = true;
}
}
else if (currSignal < Threshold && countedPulse == true) {
countedPulse = false;
digitalWrite(LED13, LOW);
}
prevSignal = currSignal;
}
BPM = Pulse * 6; // measure number of pulse in 10s, multiply by 6 to get pulses per min
Serial.println ();
Serial.print ("BPM = ");
Serial.println (BPM);
Serial.println ();
Pulse = 0; // return the pulse count to 0 after print BPM
}
There is a stupid way I have done, which I divided 2 for my BPM calculation to make the BPM looks legit since there is an extra 2 to 3 counts every time. But I hope there is a better way to solve the problem.