Hey comm,
I got an acceleration sensor. My only goal is to log the acc data in a file on my SD card.
It works, but as soon as i use SD.remove in my Project, the data file wont open anymore.
Here's the code: (only initiations in the setup func)
#include "quaternionFilters.h"
#include "MPU9250.h"
#include <SPI.h>
#include <SD.h>
File myFile, tmpFile;
MPU9250 myIMU;
String input;
void setup()
{
Wire.begin();
Serial.begin(9600);
byte c = myIMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
if (c == 0x73) // WHO_AM_I should always be 0x68
{
Serial.println("MPU9250 is online...");
myIMU.MPU9250SelfTest(myIMU.SelfTest);
myIMU.calibrateMPU9250(myIMU.gyroBias, myIMU.accelBias);
myIMU.initMPU9250();
myIMU.initAK8963(myIMU.magCalibration);
} // if (c == 0x73)
else
{
Serial.print("Could not connect to MPU9250");
while(1);
}
// SD INITIALISIERUNG-------
if (!SD.begin(4)) {
Serial.println("SD Reader error!");
while(1);
}else{
Serial.println("SD is ready.");
myFile = SD.open("data.dat", FILE_WRITE);
if (myFile) {
myFile.println("-----");
myFile.close();
} else {
Serial.println("error opening data.dat");
}
}
// ----------------------------
}
void loop(){
if (Serial.available() > 0) {
input = Serial.readString();
}
myIMU.updateTime();
MahonyQuaternionUpdate(myIMU.ax, myIMU.ay, myIMU.az, myIMU.gx*DEG_TO_RAD,
myIMU.gy*DEG_TO_RAD, myIMU.gz*DEG_TO_RAD, myIMU.my,
myIMU.mx, myIMU.mz, myIMU.deltat);
myFile = SD.open("data.dat", FILE_WRITE);
if (myFile) {
if(input == "readdata"){
input = "";
Serial.println("data.dat:");
while (myFile.available()) {
Serial.write(myFile.read());
}
}else if(input == "deletedata"){
myFile.close();
SD.remove("data.dat");
Serial.println("abort");
while(1);
}else{
String data;
// Print acceleration values in milligs!
if(myIMU.ax >= 0)
data += String("+");
data += String(1000*myIMU.ax) + String(" ");
if(myIMU.ay >= 0)
data += String("+");
data += String(1000*myIMU.ay) + String(" ");
if(myIMU.az >= 0)
data += String("+");
data += String(1000*myIMU.az) + String(" ");
// Print gyro values in degree/sec
if(myIMU.gx >= 0)
data += String("+");
data += String(myIMU.gx, 3) + String(" ");
if(myIMU.gy >= 0)
data += String("+");
data += String(myIMU.gy, 3) + String(" ");
if(myIMU.gz >= 0)
data += String("+");
data += String(myIMU.gz, 3) + String(" ");
// Print mag values in milliGauss
if(myIMU.mx >= 0)
data += String("+");
data += String(myIMU.mx) + String(" ");
if(myIMU.my >= 0)
data += String("+");
data += String(myIMU.my) + String(" ");
if(myIMU.mz >= 0)
data += String("+");
data += String(myIMU.mz); // in mG
myFile.println(data);
Serial.println(".");
}
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("ERROR OPENING FILE data.dat!");
}
delay(200);
}
"readdata" -> Reading the File data.dat
works fine.
"logging" -> The Acceleration data into the file works fine.
But all without the SD.remove function. As soon as it is in the Code ill get "ERROR OPENING FILE data.dat!" (even if not called thru deletedata)
Why is this so? Is there any solution to my Problem?
Happy programming, Luca