Basically, the signal that comes from the sensor varies from 0-1024 and it's very hard to sense a beat since the signal varies a lot. So, I reduced the scale from 0-1024 to a 0-1 scale by dividing by 512. I noticed that everytime a pulse was sensed it was higher than 512 on the first scale, which will give me 1 on the second scale.
Because there was a lot of false reads, or noises, the sensor would hit the number 1(2nd scale) once in a while. Then created a counter that reduce that noice when counting beats. Everytime this counter is above or equals 4, it'd be probably a real beat.
Sorry, english is not my first language but I hope it helped you.
If you have any questions, feel free to ask me leaving a comment bellow.
// Variables int PulseSensor = 0; //Pulse sensor is connected to the '0' analog pin int Signal; //Signal Received from the sensor int base = 0; //Base to the measurement int beat = 0; //Number of beats unsigned long time = 0; //Current time in milliseconds int bpm = 0; //BPM int SignalPrev = 0; //Armazena o sinal coletado anterior ao atual int counter = 4; //Number of variations that will be despised void setup() { Serial.begin(9600); // Set's up Serial Communication at certain speed. pinMode(13, OUTPUT); } void loop() { Signal = analogRead(PulseSensor); // Reads the sensor Signal = Signal/512; // Reduce the 1024 value scale to 0-1 scale if(Signal > base && SignalPrev ==0 && counter>=4){ //If There's a pulse and the previous signal is 0 digitalWrite(13, HIGH); counter = 0; // and the number of the signs despised is 4, then beat++; SignalPrev = Signal; if (checkTime(time)){ //True if 1 minute has passed bpm = beat; } Serial.println(bpm); //Prints the BPM value beat = 0; // Resets the values bpm = 0; } }else { //Senão, despreze o valor lido e adicione 1 ao SignalPrev = Signal; //número de valores desprezados counter++; digitalWrite(13, LOW); } delay(100); //Atrase a leitura do sinal em 100ms } //check if 1 minute has passed boolean checkTime (unsigned long temp){ unsigned long temp2= millis(); if((temp2-time)>=(60000)){ time = millis(); return true; } return false; }