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