Heartrate monitor project

H i guys currently writing code for my heartrate monitor. The code for finding the BPM is just above where i have the code for writing and saving the code to the SD card in .txt file. It is able to get the BPM fine when i try to write just to serial monitor but once i try to send the values to the SD card it starts giving funny readings. Any ideas?
Here is the code:

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

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
float BPM=0; // delcare BPM in float data type , intial at 0
const int pin =2;
int state=0;

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

if(SD.begin())
{
Serial.print("SD card is card is ready to use\n");
Serial.print("Your heartrate is");
}
else
{
Serial.print("SD Initilization failed...");
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.

}

void loop() {

/* 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;
lastmillis=startmillis;
}

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

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

//serial monitor
Serial.print(state);
Serial.print(",");
Serial.print(startmillis);
Serial.print(",");
Serial.print(lastmillis);
Serial.print(",");
Serial.print(calmillis);
Serial.print(",");
Serial.println(BPM);

delay_x(5); // delay 5 milisecond

myFile = SD.open("test.txt", FILE_WRITE);

// if the file opened okay, write to it:
if (myFile) {
myFile.print(BPM);
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}

// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");

// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}

}

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) {
millis_delay--;
micros_now += 1000;
}
}
}