Need help with my counter

Hi guys i am working on my heart rate monitor project and i am having some difficultly with my counter. I am printing the bpm to the serial monitor and want to print the amount of seconds that have elapsed but this code just keeps printing 1’s for the counter… i have tried putting the code for the counter in the loop but when i do that it messes up the BPM’s… any ideas??

#include <SD.h>
#include <SPI.h>

float getBPM();
int getHeartbeat();
int getCount();

File myFile;

unsigned long startmillis=0; // declare startmillis in unsigned long data type, initial at zero
unsigned long lastmillis=0; // declare lastmililis in unsigned long data type, initial at zero
unsigned long calmillis=0; // declare calmillis in unsigned long data type, initial at zero

const int pin =2;
int state=0;

float bpms;
float heartdecect;
int time;

void setup()
Serial.begin(115200); // initial serial monitor at baud rate of 115200.For effectiveserial communication
time= getCount();

void loop() {

delay_x(5); // delay 5 milisecond


float getBPM()
float BPM=0; //

/* As sensor output >> particular threshold let say 512, the digital pin will count as 1
else will count as 0 */

/*millis() library function–> a counter that count how many milisecond has passed once serial monitor start */

while(digitalRead(2)==1) //as output equal to 1, last millis = startmillis. at intial =0. If dtect another logic high lastmililis wil store previous logic low millis() value
{ state=1;

while(digitalRead(2)==0) // startmilis() store value of millis() once it detect logic low(heart beat)
{ state=0;

calmillis=startmillis-lastmillis; // different of these 2 variable correpond to heartbeat interval
BPM=60000/calmillis; // relation between BPM and heartbeat interval

return BPM;

return counter;

void delay_x(uint32_t millis_delay)
uint16_t micros_now = (uint16_t)micros();

while (millis_delay > 0) {
if (((uint16_t)micros() - micros_now) >= 1000) {
micros_now += 1000;

Please go read the forum rules and then edit your post above so that you are not breaking them.

Hi, Welcome to the Forum

Please read the first post in any forum entitled how to use this forum. How to use this forum then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :)

Can you post code that compiles? I do applaud you though for using function prototypes.