ADXL 345 & Adafruit SD data logger_ how to merge the two working codes together?

Hello

I require some assistance to merge two working codes i have together in order to read the accelerometer values of a sparkfun ADXL345 Accelerometer and Data Log this using the Adafruit Data logger.

I have the accelerometer values streaming in the serial monitor and the SD logger is working also.

The code for the trashcan project does have everything, although it uses a different accelerometer which has analog outs and this is where my problem lies. i want to output the x, y, z values from the digital sparkfun breakout board. i am using the I2C connection to keep my digital pin 10 as output which the datalogger requires.

I have added the main bit fo code for the ADXL 345 into the data logger code before and removed the analog inputs and modified the “void loop()” section by adding the ADXL345 code in but it does not work.

Is it possible for someone to aid me in implementing the two codes together? I am new at C programming, as used Matlab before.

Here is the ADXL 345 Code i am using which gives me x, y, z values in serial monitor correctly.

#include <Wire.h>

#define DEVICE (0x53)    //ADXL345 device address
#define TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)

byte buff[TO_READ] ;    //6 bytes buffer for saving data read from the device
char str[512];                      //string buffer to transform data before sending it to the serial port

int sampleSize = 50; // used in moving average  


void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  
  //Turning on the ADXL345
  writeTo(DEVICE, 0x2D, 0);      
  writeTo(DEVICE, 0x2D, 16);
  writeTo(DEVICE, 0x2D, 8);
}

void loop()
{
  int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
  int x, y, z;
  int xx, yy, zz;
  
  readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
  
  
   // moving average for noise compensation  
   for( int i = 1; i<=50; i++)
   {
     
   //each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
   //thus we are converting both bytes in to one int
  x += (((int)buff[1]) << 8) | buff[0];   
  y += (((int)buff[3])<< 8) | buff[2];
  z += (((int)buff[5]) << 8) | buff[4];  
  
   }  
   xx = x / sampleSize;  
   yy = y / sampleSize;   
   zz = z / sampleSize;  
   
  //we send the x y z values as a string to the serial port
  sprintf(str, "%d %d %d", xx, yy, zz);  
  Serial.write(str);
  Serial.write(byte(10));
  
  //It appears that delay is needed in order not to clog the port
  delay(15);
}

//---------------- Functions
//Writes val to address register on device
void writeTo(int device, byte address, byte val) {
   Wire.beginTransmission(device); //start transmission to device 
   Wire.write(address);        // send register address
   Wire.write(val);        // send value to write
   Wire.endTransmission(); //end transmission
}

//reads num bytes starting from address register on device in to buff array
void readFrom(int device, byte address, int num, byte buff[]) {
  Wire.beginTransmission(device); //start transmission to device 
  Wire.write(address);        //sends address to read from
  Wire.endTransmission(); //end transmission
  
  Wire.beginTransmission(device); //start transmission to device
  Wire.requestFrom(device, num);    // request 6 bytes from device
  
  int i = 0;
  while(Wire.available())    //device may send less than requested (abnormal)
  { 
    buff[i] = Wire.read(); // receive a byte
    i++;
  }
  Wire.endTransmission(); //end transmission
}

Here is the code for the adafruit SD data logger, from the trashcan accelerometer project:

 /* --------------------------------------  
  MMA7260Q 3-axis accelerometer  
   
  Accelerometer and data collection.  
   
  code released as Open Source  
  feel free to use as you see fit.  
   
 */ --------------------------------------  
   
 // libraries from   
 // http://www.ladyada.net/make/logshield/download.html  
   
 #include <SdFat.h>  
 #include <Wire.h>  
 #include "RTClib.h"  
   
 #define ECHO_TO_SERIAL 0  // 1=echo is on, 0=echo is off  
   
 #define redLEDpin 9  
 #define greenLEDpin 8  
   
 #define x_axis 0  
 #define y_axis 1  
 #define z_axis 2  
 #define startStopSwitch 7  
   
   
 RTC_DS1307 RTC;   
 Sd2Card card;  
 SdVolume volume;  
 SdFile root;  
 SdFile file;  
   
 int sampleSize = 50; // used in moving average   
   
 //---------------------------------------------------------------  
 void error(char *str)  
 {  
  Serial.print("error: ");  
  Serial.println(str);  
  digitalWrite(greenLEDpin, LOW);   
  while(1)  
  {  
    digitalWrite(redLEDpin, HIGH);   
    delay(250);  
    digitalWrite(redLEDpin, LOW);   
    delay(250);     
  };  
 }  
   
 //---------------------------------------------------------------  
 void setup(void)  
 {  
  pinMode(redLEDpin, OUTPUT);  
  pinMode(greenLEDpin, OUTPUT);  
  pinMode(startStopSwitch, INPUT);  
    
  digitalWrite(redLEDpin, HIGH);  
  digitalWrite(greenLEDpin, HIGH);  
    
  Serial.begin(9600);  
   
  // initialize the SD card  
  if (!card.init()) error("card.init");  
    
  // initialize a FAT volume  
  if (!volume.init(card)) error("volume.init");  
    
  // open root directory  
  if (!root.openRoot(volume)) error("openRoot");  
    
  // create a new file  
  // starts with LOGGER00, and next one would be LOGGER01 if  
  // LOGGER00 already exists. THis preserves existing files and  
  // increments the new filename  
  char name[] = "XYZLOG00.CSV";  
  for (uint8_t i = 0; i < 100; i++)   
  {  
   name[6] = i/10 + '0';  
   name[7] = i%10 + '0';  
   //O_CREAT = create file, O_EXCL = only if file doesn't already exist  
   //O_WRITE = open for writing  
   if (file.open(root, name, O_CREAT | O_EXCL | O_WRITE)) break;  
  }  
    
  if (!file.isOpen()) error ("file.create");  
  //Serial.print("Logging to: ");  
  //Serial.println(name);  
   
  // write header  
  file.writeError = 0;  
   
  Wire.begin();   
  if (!RTC.begin())   
  {  
   file.println("RTC failed");  
   #if ECHO_TO_SERIAL  
    Serial.println("RTC failed");  
   #endif //ECHO_TO_SERIAL  
  }  
    
   
  file.println("sec,x-axis,y-axis,z-axis");    
  #if ECHO_TO_SERIAL  
   Serial.println("sec,x-axis,y-axis,z-axis");  
  #endif //ECHO_TO_SERIAL  
   
  // attempt to write out the header to the file  
  if (file.writeError || !file.sync()) {  
   error("write header");  
  }  
    
  digitalWrite(redLEDpin, LOW);  
  digitalWrite(greenLEDpin, LOW);  
  delay(1000);  
   
 }  
   
 //----------------------------------------------------------------------  
 void loop(void)  
 {  
    
  if (digitalRead(startStopSwitch) == LOW)  
  {  
   // user feedback for errors and status  
   digitalWrite(redLEDpin, HIGH);  
   digitalWrite(greenLEDpin, LOW);  
  }  
  else  
  {  
   digitalWrite(redLEDpin, LOW);  
   digitalWrite(greenLEDpin, HIGH);  
   DateTime now;  
    
   // clear print error  
   file.writeError = 0;  
    
   // delay for the amount of time we want between readings  
   delay(100);  
    
   digitalWrite(greenLEDpin, LOW);  
   now = RTC.now();   
   float x = 0;   
   float y = 0;   
   float z = 0;  
     
   // moving average for noise compensation  
   for( int i = 1; i<=sampleSize; i++)  
   {  
    x+= analogRead(0);  
    y+= analogRead(1);   
    z+= analogRead(2);  
   }  
   x = x / sampleSize;  
   y = y / sampleSize;   
   z = z / sampleSize;  
   
   // output format for CSV data on SD card    
   file.print(now.second(), DEC);  
   file.print(", ");  
   file.print(x);  
   file.print(", ");  
   file.print(y);  
   file.print(", ");  
   file.println(z);   
   #if ECHO_TO_SERIAL   
    Serial.print(now.second(), DEC);  
    Serial.print(", ");  
    Serial.print(x);  
    Serial.print(", ");  
    Serial.print(y);  
    Serial.print(", ");  
    Serial.println(z);    
   #endif //ECHO_TO_SERIAL   
    
   if (file.writeError) error("write data");   
   if (!file.sync()) error("sync");  
  }  
   
 }

any assistance will be greatly appreciated. thanks in advance

Tom

Please edit your post, and get rid of the glow, it hurts my eyes. Then put the the code in ... tags, by selecting it and clicking the ‘#’ button. That will stop smileys appearing.

Then fix your caps lock key, it seems to be stuck on.

Ok sorry new to posting too. This is better for sure

Certainly is.

Here is my attempt at the modified code…

 /* --------------------------------------  
  MMA7260Q 3-axis accelerometer  
   
  Accelerometer and data collection.  
   
  code released as Open Source  
  feel free to use as you see fit.  
   
 */
   
 // libraries from   
 // http://www.ladyada.net/make/logshield/download.html  
   
 #include "SD.h" 
 #include <Wire.h>  
 #include "RTClib.h"
 

 #define DEVICE (0x53)    //ADXL345 device address
 #define TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)
 #define ECHO_TO_SERIAL 0  // 1=echo is on, 0=echo is off  
   
 #define redLEDpin 9  
 #define greenLEDpin 8  
  
 #define startStopSwitch 7   

 RTC_DS1307 RTC;
 
 //______ADDED CODE FOR ADXL345_____________
 byte buff[TO_READ];    //6 bytes buffer for saving data read from the device
 char str[512];                      //string buffer to transform data before sending it to the serial port 
 //_________________________________________
 Sd2Card card;  
 SdVolume volume;  
 SdFile root;  
 SdFile file;  
   
 
void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  
  //Turning on the ADXL345
  writeTo(DEVICE, 0x2D, 0);      
  writeTo(DEVICE, 0x2D, 16);
  writeTo(DEVICE, 0x2D, 8);
}
 
   
 //---------------------------------------------------------------  
 void error(char *str)  
 {  
  Serial.print("error: ");  
  Serial.println(str);  
  digitalWrite(greenLEDpin, LOW);   
  while(1)  
  {  
    digitalWrite(redLEDpin, HIGH);   
    delay(250);  
    digitalWrite(redLEDpin, LOW);   
    delay(250);     
  };  
 }  
   
 //---------------------------------------------------------------  
 void setup(void)  
 {
   
  digitalWrite(redLEDpin, LOW);    
  pinMode(redLEDpin, OUTPUT);  
  pinMode(greenLEDpin, OUTPUT);  
  pinMode(startStopSwitch, INPUT);  
    
  digitalWrite(redLEDpin, HIGH);  
  digitalWrite(greenLEDpin, HIGH);  
    
  Serial.begin(9600);  
   
  // initialize the SD card  
  if (!card.init()) error("card.init");  
    
  // initialize a FAT volume  
  if (!volume.init(card)) error("volume.init");  
    
  // open root directory  
  if (!root.openRoot(volume)) error("openRoot");  
    
  // create a new file  
  // starts with LOGGER00, and next one would be LOGGER01 if  
  // LOGGER00 already exists. THis preserves existing files and  
  // increments the new filename  
  char name[] = "XYZLOG00.CSV";  
  for (uint8_t i = 0; i < 100; i++)   
  {  
   name[6] = i/10 + '0';  
   name[7] = i%10 + '0';  
   //O_CREAT = create file, O_EXCL = only if file doesn't already exist  
   //O_WRITE = open for writing  
   if (file.open(root, name, O_CREAT | O_EXCL | O_WRITE)) break;  
  }  
    
  if (!file.isOpen()) error ("file.create");  
  //Serial.print("Logging to: ");  
  //Serial.println(name);  
   
  // write header  
  file.writeError = 0;  
   
  Wire.begin();   
  if (!RTC.begin())   
  {  
   file.println("RTC failed");  
   #if ECHO_TO_SERIAL  
    Serial.println("RTC failed");  
   #endif //ECHO_TO_SERIAL  
  }  
    
   
  file.println("sec,x-axis,y-axis,z-axis");    
  #if ECHO_TO_SERIAL  
   Serial.println("sec,x-axis,y-axis,z-axis");  
  #endif //ECHO_TO_SERIAL  
   
  // attempt to write out the header to the file  
  if (file.writeError || !file.sync()) {  
   error("write header");  
  }  
   
  digitalWrite(redLEDpin, LOW);  
  digitalWrite(greenLEDpin, LOW);  
  delay(1000);  
   
 }
   
 //----------------------------------------------------------------------  
 void loop(void)  
 {  
    
  if (digitalRead(startStopSwitch) == LOW)  
  {  
   // user feedback for errors and status  
   digitalWrite(redLEDpin, HIGH);  
   digitalWrite(greenLEDpin, LOW);  
  }  
  else  
  {  
   digitalWrite(redLEDpin, LOW);  
   digitalWrite(greenLEDpin, HIGH);  
   DateTime now;  
    
   // clear print error  
   file.writeError = 0;  
    
   // delay for the amount of time we want between readings  
   delay(100);  
    
   digitalWrite(greenLEDpin, LOW);  
   now = RTC.now();    
     
     // FROM THIS PART I NEED TO ADD MY CODE FOR THE ADXL345 SO I 
     // CAN OUTPUT X, Y, Z VALUES, PREFERABLY USING THE AVERAGE OF SAMPLE SIZE = 50.  
     // HOW DO I ADD MY ADXL 345 WORKING CODE INTO THIS SECTION????

  int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
  int x, y, z;
  
  readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
     
    //each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
   //thus we are converting both bytes in to one  
    x= (((int)buff[1]) << 8) | buff[0];  
    y= (((int)buff[3])<< 8) | buff[2];  
    z= (((int)buff[5]) << 8) | buff[4]; 
   
    //we send the x y z values as a string to the serial port
  sprintf(str, "%d %d %d", x, y, z);  
  Serial.write(str);
  Serial.write(byte(10));
    //It appears that delay is needed in order not to clog the port
  delay(15);
   
   //______ SHOULD THIS PART BE AT THIS STAGE IN THE CODE OR ELSEWHERE??_______ 
  }  
   //---------------- Functions
//Writes val to address register on device
void writeTo(int device, byte address, byte val) {
   Wire.beginTransmission(device); //start transmission to device 
   Wire.write(address);        // send register address
   Wire.write(val);        // send value to write
   Wire.endTransmission(); //end transmission
}

//reads num bytes starting from address register on device in to buff array
void readFrom(int device, byte address, int num, byte buff[]) {
  Wire.beginTransmission(device); //start transmission to device 
  Wire.write(address);        //sends address to read from
  Wire.endTransmission(); //end transmission
  
  Wire.beginTransmission(device); //start transmission to device
  Wire.requestFrom(device, num);    // request 6 bytes from device
  
  int i = 0;
  while(Wire.available())    //device may send less than requested (abnormal)
  { 
    buff[i] = Wire.read(); // receive a byte
    i++;
  }
  Wire.endTransmission(); //end transmission
  
  
  
   // output format for CSV data on SD card    
   file.print(now.second(), DEC);  
   file.print(", ");  
   file.print(x);  
   file.print(", ");  
   file.print(y);  
   file.print(", ");  
   file.println(z);   
   #if ECHO_TO_SERIAL   
    Serial.print(now.second(), DEC);  
    Serial.print(", ");  
    Serial.print(x);  
    Serial.print(", ");  
    Serial.print(y);  
    Serial.print(", ");  
    Serial.println(z);    
   #endif //ECHO_TO_SERIAL   
    
   if (file.writeError) error("write data");   
   if (!file.sync()) error("sync");  
  }  
   
 }

i keep getting the error:: ‘writeTo’ was not declared in this scope.

Why is this?

Why is this?

Could be because the writeTo() function is nested inside loop(). Though that is very hard to tell with the poor formatting. Use Tools + Auto format to straighten out the indenting, and see where writeTo() is actually implemented. It can not be inside loop().

The last post of your code has 2 setup areas and 2 loop areas. You also do Serial.begin 2 times. You can only have 1 setup and one loop. Combine all things that need setup in one area and all loop things in one area. After that we can continue to work out the issues.

Here is a Bare minimum example:

//put definitions in this area

void setup() {
  // put your setup code here, to run once:

}

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

dxw00d:
Please edit your post, and get rid of the glow, it hurts my eyes.

The SHOUTING is hurting my eyes.

The whole post, apart from the glowing code, was originally all caps.

Hello

apologies for being a newbie but i just require a little assistance, i am not familiar with C or C++ i have only ever used matlab before so my coding skills are basic.

I have amended the code and kept everything in its appropriate function sets.

now its autoformatted so hopefully easier to scan over.

it compiles now, but it still does not provide me with a sensible output in the serial monitor. I feel this post will be helpful to many other who want to datalog using adxl345 as there is no current open source code for it.

/* --------------------------------------  
 ADXL 345  3-axis accelerometer  
 
 Accelerometer and data collection.  
 
 code released as Open Source  
 feel free to use as you see fit.  
 
 */

// libraries from   
// http://www.ladyada.net/make/logshield/download.html  

#include <SD.h>  
#include <Wire.h>  
#include "RTClib.h"  

#define ECHO_TO_SERIAL 0  // 1=echo is on, 0=echo is off  
#define DEVICE (0x53)    //ADXL345 device address
#define TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)

#define redLEDpin 9  
#define greenLEDpin 8   


RTC_DS1307 RTC; 
byte buff[TO_READ] ;    //6 bytes buffer for saving data read from the device
char str[512];          //string buffer to transform data before sending it to the serial port

Sd2Card card;  
SdVolume volume;  
SdFile root;  
SdFile file;     

// not sure where this part of the code below should be presented? in adxl code it appears below the loop function?//

//Writes val to address register on device
void writeTo(int device, byte address, byte val) {
  Wire.beginTransmission(device); //start transmission to device 
  Wire.write(address);        // send register address
  Wire.write(val);        // send value to write
  Wire.endTransmission(); //end transmission
}

//---------------------------------------------------------------// 
//reads num bytes starting from address register on device in to buff array
void readFrom(int device, byte address, int num, byte buff[]) {
  Wire.beginTransmission(device); //start transmission to device 
  Wire.write(address);        //sends address to read from
  Wire.endTransmission(); //end transmission

    Wire.beginTransmission(device); //start transmission to device
  Wire.requestFrom(device, num);    // request 6 bytes from device

  int i = 0;
  while(Wire.available())    //device may send less than requested (abnormal)
  { 
    buff[i] = Wire.read(); // receive a byte
    i++;
  }
  Wire.endTransmission(); //end transmission
}
// _______________________________________________________________//



void error(char *str)  
{  
  Serial.print("error: ");  
  Serial.println(str);  
  digitalWrite(greenLEDpin, LOW);   
  while(1)  
  {  
    digitalWrite(redLEDpin, HIGH);   
    delay(250);  
    digitalWrite(redLEDpin, LOW);   
    delay(250);     
  };  
}  




//---------------------------------------------------------------  
void setup(void)  
{  
  pinMode(redLEDpin, OUTPUT);  
  pinMode(greenLEDpin, OUTPUT);  
  //pinMode(startStopSwitch, INPUT);  

  digitalWrite(redLEDpin, HIGH);  
  digitalWrite(greenLEDpin, HIGH);  



  //--------------------------------------------------------------- 
  // Added the void setup function in the void setup part of the code for the accelerometer//
  Serial.begin(9600);  
  Wire.begin();        // join i2c bus (address optional for master)

  //Turning on the ADXL345
  writeTo(DEVICE, 0x2D, 0);      
  writeTo(DEVICE, 0x2D, 16);
  writeTo(DEVICE, 0x2D, 8);   

  //--------------------------------------------------------------- 



  // initialize the SD card  
  if (!card.init()) error("card.init");  

  // initialize a FAT volume  
  if (!volume.init(card)) error("volume.init");  

  // open root directory  
  if (!root.openRoot(volume)) error("openRoot");  

  // create a new file  
  // starts with LOGGER00, and next one would be LOGGER01 if  
  // LOGGER00 already exists. THis preserves existing files and  
  // increments the new filename  
  char name[] = "XYZLOG00.CSV";  
  for (uint8_t i = 0; i < 100; i++)   
  {  
    name[6] = i/10 + '0';  
    name[7] = i%10 + '0';  
    //O_CREAT = create file, O_EXCL = only if file doesn't already exist  
    //O_WRITE = open for writing  
    if (file.open(root, name, O_CREAT | O_EXCL | O_WRITE)) break;  
  }  

  if (!file.isOpen()) error ("file.create");  
  //Serial.print("Logging to: ");  
  //Serial.println(name);  

  // write header  
  file.writeError = 0;  

  Wire.begin();   
  if (!RTC.begin())   
  {  
    file.println("RTC failed");  
#if ECHO_TO_SERIAL  
    Serial.println("RTC failed");  
#endif //ECHO_TO_SERIAL  
  }  


  file.println("sec,x,y,z");    
#if ECHO_TO_SERIAL  
  Serial.println("sec,x,y,z");  
#endif //ECHO_TO_SERIAL  

  // attempt to write out the header to the file  
  if (file.writeError || !file.sync()) {  
    error("write header");  
  }  
  digitalWrite(redLEDpin, LOW);  
  digitalWrite(greenLEDpin, LOW);  
  delay(1000);  
}  



//---------------------------------------------------------// 
void loop(void)  
{  

  // commented out star/stop switch code part
  /* 
   if (digitalRead(startStopSwitch) == LOW)  
   {  
   // user feedback for errors and status  
   digitalWrite(redLEDpin, HIGH);  
   digitalWrite(greenLEDpin, LOW);  
   }  
   else  
   {  
   */

  digitalWrite(redLEDpin, LOW);  
  digitalWrite(greenLEDpin, HIGH);  
  DateTime now;  

  // clear print error  
  file.writeError = 0;  

  // delay for the amount of time we want between readings  
  delay(100);  

  digitalWrite(greenLEDpin, LOW);  
  now = RTC.now();

  //---------------------------------------------------------------
  // added code for loop part of adxl345 accelerometer


  int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
  int x, y, z;

  readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345

  //each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
  //thus we are converting both bytes in to one int
  x = (((int)buff[1]) << 8) | buff[0];   
  y = (((int)buff[3])<< 8) | buff[2];
  z = (((int)buff[5]) << 8) | buff[4];

  //we send the x y z values as a string to the serial port
  sprintf(str, "%d %d %d", x, y, z);  
  Serial.write(str);
  Serial.write( byte(200) );
  //It appears that delay is needed in order not to clog the port
  delay(15);

  // output format for CSV data on SD card    
  file.print(now.second(), DEC);  
  file.print(", ");  
  file.print(x);  
  file.print(", ");  
  file.print(y);  
  file.print(", ");  
  file.println(z);   
#if ECHO_TO_SERIAL   
  Serial.print(now.second(), DEC);  
  Serial.print(", ");  
  Serial.print(x);  
  Serial.print(", ");  
  Serial.print(y);  
  Serial.print(", ");  
  Serial.println(z);    
#endif //ECHO_TO_SERIAL   

  if (file.writeError) error("write data");   
  if (!file.sync()) error("sync");  
}

thanks

, but it still does not provide me with a sensible output in the serial monitor. I

Well you just give your code a good talking-to and tell it to shape-up and start making sense.
(That’s a hint for you to tell us what you consider sensible and how that differs from what you observe)

Hi folks

I’m attempting pretty much exactly what this thread started out as. Alot of the same code even. My issue is that though the unit is working and outputing everything to the serial monitor (the accelerometer is outputting correctly on all three axis) including headers, as well as creating the file on the SD card (as well as properly changing the name of the file each time I activate the process) it is not actually saving any of the accelerometers data to the excel files it is creating. So I have a bunch of 0KB excel files at the end of my tests. I’ve been working on this for quite a while now, and figure it’s time to ask for help. If anyone has the time to look at my sketch (and hopefully find one or more rookie errors…) I’d very much appreciate the help!! Judging by previous posts I’ll post the entire code, lots of it will look familiar, it’s a combo of the code from Adafruit and a page called Live Fast Code Young. I’ve gone cross-eyed at this point, thank you in advance! (and sorry about the massive code dump…)

#include "SD.h"
#include <Wire.h>
#include "RTClib.h"

#define LOG_INTERVAL  100 //milliseconds between entries
#define ECHO_TO_SERIAL  1 //send data to serial port? 1=yes 0=no
#define WAIT_TO_START  1 //require a character to be sent to the serial monitor to activite the monitor? 1=yes 0=no 
#define DEVICE  (0x1D) //wiring the adxl345 will dtermine wether this is 0x53 or 0x1D
#define TO_READ  (6) //num of bytes we are going to read each time (two bytes for each axis)


byte buff[TO_READ];
char str[512];

RTC_DS1307 RTC; // define the Real Time Clock object  
// 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 setup()
{
  Serial.begin(9600);
  Serial.println();

#if WAIT_TO_START
  Serial.println("Type any character to start");
  while (!Serial.available());
#endif //WAIT_TO_START

  // intialize 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)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:    
    return;  
  }  
  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!    
    }  
  } 

  Serial.print("Logging to: ");
  Serial.println(filename);



  Wire.begin();

  writeTo(DEVICE, 0x2D, 8);  //turn on adxl345

  if (!RTC.begin()) {
    logfile.println("RTC failed");
#if ECHO_TO_SERIAL
    Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
  }

  logfile.println(" Time , X , Y , Z ");
#if ECHO_TO_SERIAL  
  Serial.println(" Time , X , Y , Z ");
#endif
}    



void loop()
{
  DateTime now;

  //delay for the amount of time we want between readings
  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));

  // fetch the time
  now = RTC.now();
  //log time
  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(" ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
#if ECHO_TO_SERIAL
  Serial.print(", ");
  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
#endif //ECHO_TO_SERIAL

  int regAddress = 0x32;  //first axis-acceleration-data register on the ADXL345

  int x, y, z;

  readFrom(DEVICE, regAddress, TO_READ, buff);  //read the acceleration data from the ADXL345
  
  //each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
  //thus we are converting both bytes in to one int
  x = (((int)buff[1]) << 8) | buff[0];
  y = (((int)buff[3]) << 8) | buff[2];
  z = (((int)buff[5]) << 8) | buff[4];
  
  //we send the x y z values as a string to the serial port
  sprintf(str, " %d %d %d", x, y, z);
  logfile.print(str);
  logfile.write(10);
#if ECHO_TO_SERIAL 
  Serial.print(str);
  Serial.write(10);
#endif                  //It appears that a delay is needed in order not to clog the port

  delay(15);
}


//Functions


void writeTo(int device, byte address, byte val)  {
  Wire.beginTransmission(device);        //start transmission to device 
  Wire.write(address);                    // send register address
  Wire.write(val);                       // send value to write
  Wire.endTransmission();              //end transmission
}

void readFrom(int device, byte address, int num, byte buff[])  {
  Wire.beginTransmission(device);      //start transmission to device 
  Wire.write(address);                  //sends address to read from
  Wire.endTransmission();             //end transmission

    Wire.beginTransmission(device);      //start transmission to device (initiate again)
  Wire.requestFrom(device, num);         // request 6 bytes from device

  int i = 0;
  while(Wire.available())            //device may send less than requested (abnormal)

  {
    buff[i] = Wire.read();           // receive a byte
    i++;
  }
  Wire.endTransmission();            //end transmission
}

Bump

    Wire.beginTransmission(device);      //start transmission to device (initiate again)
  Wire.requestFrom(device, num);         // request 6 bytes from device

  int i = 0;
  while(Wire.available())            //device may send less than requested (abnormal)

  {
    buff[i] = Wire.read();           // receive a byte
    i++;
  }
  Wire.endTransmission();            //end transmission

You don't do a Wire.beginTransmission before doing a Wire.requestFrom (nor a Wire.endTransmission afterwards).

Don't you need to close logfile at some stage?

What do your serial prints tell you?

http://www.gammon.com.au/i2c

Referring to the begin and end transmission, from Live Fast Code Young-

“Lets look at that communication diagram on page 10 of the datasheet again. The steps we are to follow, in order to perform a multi-byte reading, are:
1.Initiate transmission to device (using write address*)
2.Write the address of the register we want to start** reading from
3.Initiate transmission to device, again! (using read address*)
4.Read bytes one after another
5.Finish transmission”

So, according to that resource, this is the way to communicate with the ADXL345. It works quite well, as I said, I get good numbers from the unit on all three axis in the serial monitor. The intent is to set this unit up and have it run for months at a time, so I’m hoping to have it dump information into the excel file and only save once the file has reached a certain size (probably 4 gigs due to the maximum size FAT32 will support), however I haven’t gotten that far yet…