I wrote some demo code to better explain post #5. It's only meant to show how the two states work, I cannot compile it because I don't have the libraries. It may not work correctly because I'm making some assumptions on how the MPU library works. Before writing to the file I added a seek to end, assuming you will want to append new data when you push the button. The button press may need to be debounced.
BTW, I wouldn't open the file in setup, since it may not get closed later.
// ================================================================
// === MAIN PROGRAM LOOP ===
// ================================================================
const int DATA_TO_SERIAL = 0; // ** Run the program as usual, giving the accelerations.
const int DATA_TO_FILE = 1; // ** Pause the program when pressing a button. Save the results.
int programState = DATA_TO_SERIAL; // ** initial program state
bool dataAvailable = false; // ** true if data is available to write
void loop() {
time = micros();
button_state = digitalRead(button_in);
if ( button_state == HIGH ) programSate = DATA_TO_FILE; // ** next read goes to file
// if programming failed, don't try to do anything
//if (!dmpReady) return;
if (!dmpReady) while (1); // ** can move this to setup
dataAvailable = chedkForData(); // set flag
// ** if data is available print it to serial or file
if ( dataAvailable )
{
// ** get data from buffer
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetAccel(&aa, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
// ** act on program state
switch ( programState )
{
// ** Run the program as usual, giving the accelerations.
case DATA_TO_SERIAL:
#ifdef OUTPUT_READABLE_REALACCEL
// display real acceleration, adjusted to remove gravity
Serial.print("areal\t");
Serial.print(time / 1000000.0, 6);
Serial.print("\t");
Serial.print(aaReal.x);
Serial.print("\t");
Serial.print(aaReal.y);
Serial.print("\t");
Serial.println(aaReal.z);
#endif
#ifdef OUTPUT_READABLE_WORLDACCEL
// display initial world-frame acceleration, adjusted to remove gravity
// and rotated based on known orientation from quaternion
Serial.print("aWorld\t");
Serial.print(time / 1000000.0, 6);
Serial.print("\t");
Serial.print(aaWorld.x);
Serial.print("\t");
Serial.print(aaWorld.y);
Serial.print("\t");
Serial.println(aaWorld.z);
#endif
break;
case DATA_TO_FILE: // ** Pause the program when pressing a button. Save the results.
// ** open file
myFile = SD.open("test1.txt", FILE_WRITE);
if (myFile)
{
// ** position at end of file to append new data
myFile.seek(myFile.size());
#ifdef OUTPUT_READABLE_REALACCEL
myFile.print("areal\t");
myFile.print(time / 1000000.0, 6);
myFile.print("\t");
myFile.print(aaReal.x);
myFile.print("\t");
myFile.print(aaReal.y);
myFile.print("\t");
myFile.println(aaReal.z);
#endif
#ifdef OUTPUT_READABLE_WORLDACCEL
myFile.print("aWorld\t");
myFile.print(time / 1000000.0, 6);
myFile.print("\t");
myFile.print(aaWorld.x);
myFile.print("\t");
myFile.print(aaWorld.y);
myFile.print("\t");
myFile.println(aaWorld.z);
#endif
// ** close file
myFile.close();
} //if
programState = DATA_TO_SERIAL; // ** Run the program as usual, giving the accelerations.
break;
default:
break;
} // switch
} // if
// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
}
// ** look for valid data
bool chedkForData()
{
bool retVal = false;
// ** if there is data waiting
if ( mpuInterrupt || (fifoCount >= packetSize) )
{
retVal = true; // ** signal data is available
// reset interrupt flag and get INT_STATUS byte
mpuInterrupt = false;
mpuIntStatus = mpu.getIntStatus();
// get current FIFO count
fifoCount = mpu.getFIFOCount();
// check for overflow (this should never happen unless our code is too inefficient)
if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
// reset so we can continue cleanly
mpu.resetFIFO();
Serial.println(F("FIFO overflow!"));
retVal = false; // ** no data after all?
// otherwise, check for DMP data ready interrupt (this should happen frequently)
} else if (mpuIntStatus & 0x02) {
// wait for correct available data length, should be a VERY short wait
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
// read a packet from FIFO
mpu.getFIFOBytes(fifoBuffer, packetSize);
// track FIFO count here in case there is > 1 packet available
// (this lets us immediately read more without waiting for an interrupt)
fifoCount -= packetSize;
} // else
} // if
return (retVal);
}