Ciao, in questo periodo sto lavorando su un datalogger, ti posto lo sketch, funziona:
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
analog sensors on analog ins 0, 1, and 2
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
/*
I2C comunication
read acceleration value
CS pin 3v3
SDA pin SDA +10k to 3v3
SDO pin GND
SCL pin SCL +10k to 3v3
3v3 VCC
GND GND
Accelerazione di gravità sulla superficie terrestre
9,80665 m/s².
*/
#include <SPI.h>
#include <SD.h>
File dataFile;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD device address to 0x27 for a 16 chars and 2 line display
/*
#define _2g (4)
#define _4g (8)
#define _8g (16)
#define _16g (32)
*/
#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 samples (100)
const int chipSelect = 4;
float x1, y1, z1;
long sum_x, sum_y, sum_z;
byte i, j;
float Axoff, Ayoff, Azoff;
float ERR_gx, ERR_gy, ERR_gz;
float gx, gy, gz;
float pitch, roll, yaw;
float factor;
byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device
int regAddress;
int x, y, z;
void setup()
{
// join i2c bus (address optional for master)
// Open serial communications and wait for port to open:
Serial.begin(9600);
Wire.begin();
// while (!Serial) {
// ; // wait for serial port to connect. Needed for native USB port only
// }
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect))// see if the card is present and can be initialized
{
Serial.println("Card failed, or not present");
// don't do anything more:
// return;
}
else
{
Serial.println("card initialized.");
}
/*
Serial.println("Creating datalog.txt...");
dataFile = SD.open("datalog.txt", FILE_WRITE);// open a new file and immediately
dataFile.close();//close it
*/
// Check to see if the file exists:
if (SD.exists("datalog.txt"))
{
Serial.println("datalog.txt");
Serial.println("Removing datalog.txt...");
SD.remove("datalog.txt");//delete the file
}
else
{
Serial.println("datalog.txt doesn't exist.");
}
lcd.init();// initialize the lcd
lcd.backlight();
//Turning on the ADXL345
writeTo(DEVICE, 0x2D, 0x00);//enter in standby mode to configure
//writeTo(DEVICE, 0x31, 0b00000011);//self mode off with 10 bit resolution +/-16g
writeTo(DEVICE, 0x31, 0b00000000);//self mode off with 10 bit resolution +/-2g
writeTo(DEVICE, 0x2C, 0x0F);//set 100 Hz data rate
writeTo(DEVICE, 0x38, 0x80);//FIFO stream mode
writeTo(DEVICE, 0x2D, 0x08);//enter in measurement mode
regAddress = 0x32;//first axis-acceleration-data register on the ADXL345
sum_x = 0;
sum_y = 0;
sum_z = 0;
//values from calibration procedure
Axoff = -170.5;// 2g 10 bit
Ayoff = -32.5;// 2g 10 bit
Azoff = 275;// 2g 10 bit
ERR_gx = .00322;// 2g 10 bit
ERR_gy = .00331;// 2g 10 bit
ERR_gz = .00396;// 2g 10 bit
}
void loop()
{
for (j = 0; j < samples; j++)
{
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];
sum_x += x;
sum_y += y;
sum_z += z;
}
x1 = sum_x / samples;
y1 = sum_y / samples;
z1 = sum_z / samples;
gx = (x1 - Axoff) * ERR_gx;
gy = (y1 - Ayoff) * ERR_gy;
gz = (z1 - Azoff) * ERR_gz;
float G[3] = {gx, gy, gz};
sum_x = 0;
sum_y = 0;
sum_z = 0;
/*
pitch = pow(tan(gy / pow(pow(gx, 2) + pow(gz, 2), 0.5)), -1);
Serial.print('\t');
Serial.print(pitch, 2);
Serial.print('\t');
Serial.println("pitch");
Serial.println();
*/
pitch = atan(gy / gz) * 180.0 / PI;
Serial.print('\t');
Serial.print(pitch, 2);
Serial.print('\t');
Serial.println("pitch");
Serial.println();
lcd.setCursor(0, 0);
lcd.print("gx");
lcd.setCursor(3, 0);
lcd.print(gx);
lcd.setCursor(10, 0);
lcd.print("g");
lcd.setCursor(0, 1);
lcd.print("gy");
lcd.setCursor(3, 1);
lcd.print(gy);
lcd.setCursor(10, 1);
lcd.print("g");
lcd.setCursor(0, 2);
lcd.print("gz");
lcd.setCursor(3, 2);
lcd.print(gz);
lcd.setCursor(10, 2);
lcd.print("g");
delay(50);//It appears that delay is needed in order not to clog the port
// log section
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile)// if the file is available, write to it:
{
for (i = 0; i < 3; i++)// read three sensor values and append to the string:
{
dataFile.print(G[i], 3);
Serial.print(G[i], 3);
if (i < 2)
{
dataFile.print(",");
Serial.print(",");
}
}
dataFile.print(",");
dataFile.print(pitch, 3);
dataFile.println();
Serial.println();
dataFile.close();
}
else
{
Serial.println("error opening datalog.txt");// if the file isn't open, pop up an error:
}
}
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
}
//readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
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++;
// Serial.println(buff[i]);
}
Wire.endTransmission(); //end transmission
}