SD Card Writing not continuously writing

Im using a ESP32 30P and I want to write MPU6050 data to a SD card. I have confirmed all of it works but I'm stumped on how to continuously write numbers.



float version = 1.00;
//Imports
#include <SPI.h>
#include <SD.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
//Constants 

#define BUTTON_PIN 26
#define PIN_SPI_CS 5
float Seconds = 0;

//SDCARD
File myFile;
//MPUG6050
Adafruit_MPU6050 mpu;

boolean Normal = true;

void setup() {
  delay(2000);
  Serial.begin(115200);
  Serial.println("SD card test...");
  delay(10);

  pinMode(BUTTON_PIN, INPUT_PULLUP); 

  if (!SD.begin(PIN_SPI_CS)) {
    while (1) {
      Serial.println("SD CARD FAILED, OR NOT PRESENT!");
      delay(1000);
      Normal = false;
    }
  }

  SD.remove("/Flight.txt");

  Serial.println("SD CARD INITIALIZED.");

  if (!SD.exists("/Flight.txt")) {
    Serial.println("Flight.txt doesn't exist");
    delay(1);
    Serial.println("Flight.txt created....");
    // create a new file by opening a new file and immediately close it
    myFile = SD.open("/Flight.txt", FILE_WRITE);
    myFile.close();
  }

  if (!SD.exists("/Flight.txt")) {
    Normal = false;
    Serial.println("Flight.txt doesn't exist on SD Card.");
  } else {
    Serial.println("Flight.txt exists on SD Card.");
  }


  delay(10);

  Serial.println("Sd card Complete! Starting..");

  Serial.println("Adafruit MPU6050 test!");

  delay(10);

  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");
  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  Serial.print("Accelerometer range set to: ");
  switch (mpu.getAccelerometerRange()) {
  case MPU6050_RANGE_2_G:
    Serial.println("+-2G");
    break;
  case MPU6050_RANGE_4_G:
    Serial.println("+-4G");
    break;
  case MPU6050_RANGE_8_G:
    Serial.println("+-8G");
    break;
  case MPU6050_RANGE_16_G:
    Serial.println("+-16G");
    break;
  }
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  Serial.print("Gyro range set to: ");
  switch (mpu.getGyroRange()) {
  case MPU6050_RANGE_250_DEG:
    Serial.println("+- 250 deg/s");
    break;
  case MPU6050_RANGE_500_DEG:
    Serial.println("+- 500 deg/s");
    break;
  case MPU6050_RANGE_1000_DEG:
    Serial.println("+- 1000 deg/s");
    break;
  case MPU6050_RANGE_2000_DEG:
    Serial.println("+- 2000 deg/s");
    break;
  }

  mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
  Serial.print("Filter bandwidth set to: ");
  switch (mpu.getFilterBandwidth()) {
  case MPU6050_BAND_260_HZ:
    Serial.println("260 Hz");
    break;
  case MPU6050_BAND_184_HZ:
    Serial.println("184 Hz");
    break;
  case MPU6050_BAND_94_HZ:
    Serial.println("94 Hz");
    break;
  case MPU6050_BAND_44_HZ:
    Serial.println("44 Hz");
    break;
  case MPU6050_BAND_21_HZ:
    Serial.println("21 Hz");
    break;
  case MPU6050_BAND_10_HZ:
    Serial.println("10 Hz");
    break;
  case MPU6050_BAND_5_HZ:
    Serial.println("5 Hz");
    break;
  }

  if (Normal) {
    Serial.println("All Systems Nomnial....");
  } else {
    Serial.println("All Systems NOT!!!!! Nomnial....");
  }
  
  delay(100);

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

  delay(1000);

  if (myFile) {
    myFile.println("Flight Controller and Data Collector aboard Pathway."); 
    myFile.println("Today Is..");
    myFile.println("June 11 2024");
    myFile.println("Data below will be rockets Velocity,Speed,Accelaration,and X and Y roll. All recorded by onboard MPU-6050");
    myFile.println("Also data will include BPM180 Height.");
    myFile.println("If you found this rocket and would like to return it please contact me here.");
    myFile.println("Onboard GPS transmitter has started");
    myFile.println("Marking Time for Launch...");
    myFile.println(" How To Read data....");
    myFile.println("Acceleration:");
    myFile.println("Rotation X:");
    myFile.println("Rotation Y:");
    myFile.println("Rotation Z:");
    myFile.println("Temperature:");
    myFile.close();
    Serial.print("TITLE SUCCSES");
  } else {
    Serial.print(F("SD Card: Issue encountered while attempting to open the file Flight.txt"));
  }

  delay(1000);
  myFile = SD.open("/Flight.txt", FILE_READ);

  if (myFile) {
    while (myFile.available()) {
      char ch = myFile.read(); // read characters one by one from Micro SD Card
      Serial.print(ch); // print the character to Serial Monitor
    }
    myFile.close();
  } else {
    Serial.print(F("SD Card: Issue encountered while attempting to open the file Flight.txt"));
  }


}

void loop() {
  // put your main code here, to run repeatedly:

  sensors_event_t a, g, temp;

  mpu.getEvent(&a, &g, &temp);

  float TempF = 0;

  float TempC = temp.temperature;

  TempF = (TempC*1.8)+32;
  
  savePosition();
  
  int buttonState = digitalRead(BUTTON_PIN);
  
  delay(1000);

  

}

void savePosition() {

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

  sensors_event_t a, g, temp;

  mpu.getEvent(&a, &g, &temp);

  float TempF = 0;

  float TempC = temp.temperature;

  TempF = (TempC*1.8)+32;

  myFile.println(a.acceleration.z);

  myFile.println(g.gyro.x);

  myFile.println(g.gyro.y);

  myFile.println(g.gyro.z);

  myFile.print(TempF);

  myFile.close();
  
}

Now I expect the Sd card to have written the Data and Title heading i gave it. Instead it just looks like this:
9.24
-0.78
-0.44
0.85
79.08
which is good! but there is nothing else.... What am i doing wrong?

Is this solved, too?

Open the file with FILE_APPEND instead of FILE_WRITE. On an ESP32 FILE_WRITE will overwrite the contents of the file.

void savePosition() {
  myFile = SD.open("/Flight.txt", FILE_APPEND);
2 Likes

No but i thought i could better describe the issue. with changing the code

Thank you sir this fixed my issue.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.