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?