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