#include <SD.h> //Load SD card library
#include<SPI.h> //Load SPI Library
#include "Wire.h" // imports the wire library for talking over I2C
int ADXL345 = 0x53; // The ADXL345 sensor I2C address
float X_out, Y_out, Z_out; // Outputs
int chipSelect = 4; //chipSelect pin for the SD card Reader
File AngleData; //Data object you will write your sesnor data to
void setup(){
Serial.begin(9600); //turn on serial monitor
//mySensor.begin(); //initialize pressure sensor mySensor
Wire.begin(); // Initiate the Wire library
// Set ADXL345 in measuring mode
Wire.beginTransmission(ADXL345); // Start communicating with the device
Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
// Enable measurement
Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable
Wire.endTransmission();
delay(10);
pinMode(10, OUTPUT); //Must declare 10 an output and reserve it
SD.begin(4); //Initialize the SD card reader
}
void loop() {
Wire.beginTransmission(ADXL345);
Wire.write(0x32);
Wire.endTransmission(false);
Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
X_out = ( Wire.read()| Wire.read() << 8); // X-axis value
X_out = X_out; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value
Y_out = Y_out;
Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
Z_out = Z_out/256;
AngleData = SD.open("PTData.txt", FILE_WRITE);
if (AngleData) {
Serial.print("X= ");
Serial.print(X_out);
Serial.print(" Y= ");
Serial.print(Y_out);
Serial.print(" Z= ");
Serial.println(Z_out);
delay(50); //Pause between readings.
AngleData.print(X_out);
AngleData.print(",");
AngleData.println(Y_out);
AngleData.print(",");
AngleData.println(Z_out);
AngleData.close(); //close the file
}
}