Hi guys
I'm running the 3.3V Arduino Pro 328 with a H3LIS331DL 400g accelerometer. I need to record the impact acceleration of a payload in a crash frame falling 10m. To log the data I'm using an Arduino SD Shield from LC Studio. My code is below. The SD card will initialise when I run the script, but it will not open the "log.txt" file and thus will not record an data. I have chekced the voltage accross the chips with a multimeter and have tried using an external 5V to run the SD shield solely. I have also checked the memory amount before the FILE_WRITE function. There is around 700 bytes and so should be sufficient. I have tried two different SD cards, both formatted to FAT32. Neither have worked everytime. About 1 run out of 5 or so the data will be logged. Can anyone find any problems in the code or suggestion any possible solutions?
Cheers
#include "SparkFun_LIS331.h"
#include <SPI.h>
#include <SD.h>
LIS331 xl;
unsigned long time;
//[2] = Accelerometer
//[1] = SD Shield
const int chipSelect = 10;
void setup()
{
// put your setup code here, to run once:
pinMode(9,OUTPUT); // CS_2 for SPI
pinMode(10, OUTPUT); // CS_1 for SPI
digitalWrite(9, HIGH); // Make CS_1 high
pinMode(11, OUTPUT); // MOSI for SPI
pinMode(12, INPUT); // MISO for SPI
pinMode(13, OUTPUT); // SCK for SPI
SPI.begin();
xl.setSPICSPin(9); // This MUST be called BEFORE .begin() so
// .begin() can communicate with the chip
xl.begin(LIS331::USE_SPI); // Selects the bus to be used and sets
// the power up bit on the accelerometer.
// Also zeroes out all accelerometer
// registers that are user writable.
xl.setODR(LIS331::DR_1000HZ);//Options:1000,400,100,50
xl.setFullScale(LIS331::HIGH_RANGE);//HIGH=400,MED=200,LOW=100
Serial.begin(9600);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
}
void loop()
{
String dataString = " ";
digitalWrite(9,HIGH); //Make CS_2 HIGH
int16_t x, y, z;
xl.readAxes(x, y, z); // The readAxes() function transfers the
// current axis readings into the three
// parameter variables passed to it.
int xg = xl.convertToG(6,x);
//int xg = x;
int yg = xl.convertToG(6,y);
//int yg = y;
int zg = xl.convertToG(6,z);
//int zg = z;
dataString += String(xg);
dataString += ",";
dataString += String(yg);
dataString += ",";
dataString += String(zg);
digitalWrite(9,LOW);//Make CS_2 Low
digitalWrite(10,HIGH);//Make CS_1 High
File dataFile = SD.open("log", FILE_WRITE);
if (dataFile) {
} else {
// if the file didn't open, print an error:
Serial.println("error opening log.txt");
}
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}