//Initiate an SPI communication instance.
//Create a serial connection to display the data on the terminal.
// use debugging LEDs
// set the SD CS pin to output
//Set up the Chip Select pin to be an output from the Arduino.
//Before communication starts, the Chip Select pin needs to be set high.
// must be set to high so the SD card won't talk at the same time
// initialize the sd card
if (!SD.begin(SD_CS) )
Serial.println("Couldn't Initialize SD Card");
Serial.println("Done Initializing SD Card");
// create a new file that starts with logger00 and the next one will be logger01 if logger00 exists
char filename = "LOGGER00.CSV";
for (int i = 0; i < 100; i++)
filename = i/10 + '0';
filename = i%10 + '0';
if (! SD.exists(filename))
// only open a new file if it doesn't exist
myFile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
// set up the file header
//Configure the SPI connection for the ADXL345.
// before communication starts the CS pin must be set to high
//Put the ADXL345 into +/- 16G range by writing the value 0x01 to the DATA_FORMAT register.
/// set the accelerometer into full resolution mode and +/- 16G mode
writeRegister( DATA_FORMAT, 0x08 | 0x0F);
// enable free fall interrupt
writeRegister(INT_MAP, 0x00); // pass the interrupt to the first interrupt pin
writeRegister(INT_ENABLE,0x04); // enable the free fall function
// Sets the THRESH_FF register which holds the threshold value, in an unsigned format, for
// free-fall detection. The root-sum-square (RSS) value of all axes is calculated and
// compared whith the value in THRESH_FF to determine if a free-fall event occured. The
// scale factor is 62.5mg/LSB. A value of 0 may result in undesirable behavior if the free-fall
// interrupt is enabled. The maximum value is 255
// Sets the TIME_FF register, which holds an unsigned time value representing the minimum
// time that the RSS value of all axes must be less than THRESH_FF to generate a free-fall
// interrupt. The scale factor is 5ms/LSB. A value of 0 may result in undesirable behavior if
// the free-fall interrupt is enabled. The maximum value is 255.
//Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
writeRegister(POWER_CTL, 0x08); //Measurement mode
writeRegister(BW_RATE,0x0F); // change sampling rate