MPPU 6050 data logging to Adafruit SD

Hi

I’m very new to this so i tried meshing two different codes into one to see if it could work (clearly it hasnt, that’s why i’m here). I’ve connected an MPU 6050, Arduino uno and Adafruit data logger. They all work fine individually and the deault address of the MPU is 0x69. Tried using the code, which appears to work but it doesnt write anything to the SD card. Any help would be great. Thanks.

#include <SPI.h>
#include <SD.h>
#include "I2Cdev.h"
#include "MPU6050.h"

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
    #include "Wire.h"
#endif


// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)

// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to 
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()

#define ECHO_TO_SERIAL   0 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()

MPU6050 accelgyro(0x69);
//MPU6050 accelgyro(0x69); // <-- use for AD0 high

int16_t ax, ay, az;
int16_t gx, gy, gz;

#define OUTPUT_READABLE_ACCELGYRO


#define LED_PIN 13
bool blinkState = false;

// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;

// the logging file
File logfile;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
 
}

void setup(void)
{
  
      // join I2C bus (I2Cdev library doesn't do this automatically)
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif

  Serial.begin(38400);
  Serial.println("Initializing I2C devices...");
    accelgyro.initialize();
  
  Serial.println("Testing device connections...");
  Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  
#if WAIT_TO_START
  Serial.println("Type any character to start");
  while (!Serial.available());
#endif //WAIT_TO_START

  // initialize the SD card
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present");
  }
  Serial.println("card initialized.");
  
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // leave the loop!
    }
  }
  
  if (! logfile) {
    error("couldnt create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);

  logfile.println("&ax,&ay,&az,&gx,&gy,&gz");    
#if ECHO_TO_SERIAL
  Serial.println("&ax,&ay,&az,&gx,&gy,&gz");
#endif //ECHO_TO_SERIAL
 
}

void loop(void)
{


  // delay for the amount of time we want between readings
  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
   
  // log milliseconds since starting
  uint32_t m = millis();
  logfile.print(m);           // milliseconds since start
  logfile.print(", ");    
#if ECHO_TO_SERIAL
  Serial.print(m);         // milliseconds since start
  Serial.print(", ");  
#endif

    #ifdef OUTPUT_READABLE_ACCELGYRO
        // display tab-separated accel/gyro x/y/z values
        logfile.print("a/g:\t");
        logfile.print(ax); logfile.print("\t");
        logfile.print(ay); logfile.print("\t");
        logfile.print(az); logfile.print("\t");
        logfile.print(gx); logfile.print("\t");
        logfile.print(gy); logfile.print("\t");
        logfile.println(gz);
    #endif

    #ifdef OUTPUT_BINARY_ACCELGYRO
        logfile.write((uint8_t)(ax >> 8)); logfile.write((uint8_t)(ax & 0xFF));
        logfile.write((uint8_t)(ay >> 8)); logfile.write((uint8_t)(ay & 0xFF));
        logfile.write((uint8_t)(az >> 8)); logfile.write((uint8_t)(az & 0xFF));
        logfile.write((uint8_t)(gx >> 8)); logfile.write((uint8_t)(gx & 0xFF));
        logfile.write((uint8_t)(gy >> 8)); logfile.write((uint8_t)(gy & 0xFF));
        logfile.write((uint8_t)(gz >> 8)); logfile.write((uint8_t)(gz & 0xFF));
    #endif


  logfile.println();
#if ECHO_TO_SERIAL
  Serial.println();
#endif // ECHO_TO_SERIAL

blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
  

  // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  // which uses a bunch of power and takes time
  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  syncTime = millis();
  
}

Any help would be great.

Don't comment out code you don't need. DELETE IT!

I got tired trying to wade past all the commented out code, and gave up.

Fair point, have edited it down. :D

Littlechap:
Fair point, have edited it down. :smiley:

I see:

//MPU6050 accelgyro(0x69); // <-- use for AD0 high
  // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  // which uses a bunch of power and takes time

While the comments are correct (writing takes time and power), they have NOTHING to do with the code that follows.

How much available memory do you have?
http://playground.arduino.cc/Code/AvailableMemory

You are unnecessarily squandering memory:

  Serial.println("Initializing I2C devices...");

Make that:
Serial.println(F(“Initializing I2C devices…”));