Writing to an SD card, how to make sure that data isn't corrupt when powered off

I am currently trying to make a program that reads a BMP280 pressure sensor, writes the data to the serial monitor and saves the data to an SD card, and then remove the SD card and simply view the text file on my computer, nothing to difficult but I seem to be having a problem.

What I want to do is have a continuous loop, that if I inout “e” into the serial monitor, the program halts so nothing is being written to the SD card so that it should be safe when I remove the power
</
#include <SD.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp;

File myFile;

const int chipselect = 10;
float QNH = 1022.67;
const int BMP_address = 0x76;

float pressure;
float temperature;
float altitude;

byte i=0;
char dataStr[100] = “”;
char buffer {7};
char charRead;

void setup() {
Serial.begin(9600);
bmp.begin(BMP_address);
if (SD.begin(chipselect))
{
Serial.println(“SD card is present”);
}
else
{
Serial.println(“SD card missing or failure”);
while(1);
}
if (SD.exists(“csv.txt”))
{
Serial.println(“Clearing file”);
SD.remove(“csv.txt”);
Serial.print(“Done”);
Serial.print("");
}
myFile = SD.open(“csv.txt”, FILE_WRITE);
if (myFile)
{
myFile.println(“Time, Pressure, Temperature, Altitude”);
myFile.close();
}
else
{
Serial.println(“Error opening file”);
}
}

void loop(void) {
dataStr[0] = 0;
pressure = bmp.readPressure()/100;
temperature = bmp.readTemperature();
altitude = bmp.readAltitude(QNH);

ltoa(millis(),buffer,10);
strcat(dataStr, buffer);
strcat(dataStr, ", ");

dtostrf(pressure, 5, 2, buffer);
strcat( dataStr, buffer);
strcat( dataStr, ", ");

dtostrf(temperature, 5, 2, buffer);
strcat( dataStr, buffer);
strcat( dataStr, ", ");

dtostrf(altitude, 5, 2, buffer);
strcat( dataStr, buffer);
strcat( dataStr, 0);

Serial.print(pressure); Serial.print("hPa ");
Serial.print(temperature);
Serial.write(0xC2); //send degree symbol
Serial.write(0xB0); //send degree symbol
Serial.print("C ");
Serial.print(altitude); Serial.println(“m”);

myFile = SD.open(“csv.txt”, FILE_WRITE);
// if the file opened okay, write to it:
if (myFile)
{
Serial.println(“Writing to csv.txt”);
myFile.println(dataStr);
myFile.println(dataStr);
myFile.close();
}
else
{
Serial.println(“error opening csv.txt”);
}
delay(500);

if (Serial.available()) //get command from keyboard:
{
charRead = tolower(Serial.read()); //force ucase
Serial.write(charRead); //write it back to Serial window
Serial.println();
}

if (charRead = “e”)
{
noInterrupts();
while(1){}
}
else

I have tried to implement the “e” input at the end bur have no idea what to do next/ go to
Mant thanks

if (charRead = "e") Two things here:

  1. == is the way to compare, = is for assignment
  2. a single character would be ‘e’

Please remember to use code tags when posting code

And
3) this is not a bootloader issue.

@harveyn4444

Your topic was Moved to it's current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

You MUST flush and close the SD file if you want all the data you have written to be available when you turn off the Arduino. The directory entry has to be updated when you close the file or it will be left with the same file size information as when the file was first opened.

Paul