The first problem I encountered was that the ADXL345 is a SPI bus hog, If I read the data sheet on it correctly the only way to get it off the bus is to cut power to it. This means I have to initialize the XL345 get all of my readings then turn it off to clear the SPI bus for writing to the SD card file.
This sketch is not complete yet, I've been working on small sections to get a little less to chew on at one time.
Currently working on getting the data formatted in a file to transfer to a spread sheet.
I haven't done any programming in about 20 years but seem to be able to get most things working on my own for the most part. BTW the Arduino can not be connected to a computer as it will be spinning at around 1500 rpm. If it could I could have copied the data from the terminal and pasted it into a text file.
There is a lot of junk still in the sketch to be cleaned out later. Here is a snippet of the code.
void loop()
{ // Begin main loop
if (testmode == 1){ // 5
Serial.print(filename);
Serial.println(G_range, DEC);
}
if(G_range == 0){ // begin range check
// ADXL345 initialized in 2G mode in setup G range is stepped upward here through all of it's ranges.
// Program terminates after reading 16G range logging data and turns on blue LED.
G_range++; //increment range for next sample set
samplecount = 0; //reset data sample count to 0
filename = ("G_range2.txt");
goto sample;
}
// switch (G_range){ // begin range select
if (G_range == 1) {
// case 1:
//Put the ADXL345 into +/- 4 G range by writing the value 0x01 to the DATA_FORMAT register.
writeRegister(DATA_FORMAT, 0x01);
G_range++; //increment range for next sample set
samplecount = 0; //reset data sample count to 0
filename = ("G_range4.txt");
Serial.print(filename);
Serial.println(G_range, DEC);
goto sample;
}
if (G_range == 2){
// case 2:
//Put the ADXL345 into +/- 8 G range by writing the value 0x01 to the DATA_FORMAT register.
writeRegister(DATA_FORMAT, 0x10);
G_range++; //increment range for next sample set
samplecount = 0; //reset data sample count to 0
filename = ("G_range8.txt");
Serial.print(filename);
Serial.println(G_range, DEC);
goto sample;
}
if (G_range == 3) {
// case 3:
//Put the ADXL345 into +/- 16 G range by writing the value 0x01 to the DATA_FORMAT register.
writeRegister(DATA_FORMAT, 0x11);
G_range++; //increment range for next sample set
samplecount = 0; //reset data sample count to 0
filename = ("Grange16.txt");
Serial.print(filename);
Serial.println(G_range, DEC);
goto sample;
}
if (G_range == 4){
// case 4:
Serial.print(filename);
Serial.println(G_range, DEC);
digitalWrite(redledPin,HIGH);
digitalWrite(grnledPin,HIGH);
digitalWrite(bluledPin,LOW);
Serial.println("Program Terminated at line 154");
while (pend < 1){ // wait here till reset.
wait= millis();
}
G_range++; //increment range for next sample set
// return; // program terminates normaly with blue LED on.
// default:
// } // End range elect
//Reading 6 bytes of data starting at register DATAX0 will retrieve the x,y and z acceleration values from the ADXL345.
// G_range++; //increment range for next sample set
sample:
dataString =(""); // Clear for next sample set
while (samplecount <= 100){ // Begin sample read loop
//The results of the read operation will get stored to the values[] buffer.
readRegister(DATAX0, 6, values);
//The ADXL345 gives 10-bit acceleration values, but they are stored as bytes (8-bits). To get the full value, two bytes must be combined for each axis.
//The X value is stored in values[0] and values[1].
x = ((int)values[1]<<8)|(int)values[0];
//The Y value is stored in values[2] and values[3].
y = ((int)values[3]<<8)|(int)values[2];
//The Z value is stored in values[4] and values[5].
z = ((int)values[5]<<8)|(int)values[4];
// clear string for next samples of data to log:
dataString += String(x);
dataString += String(",");
dataString += String(y,'/rl');
// dataString += String('/r');
// If test mode = true Print the results to the terminal.
if (testmode == 1){ // 5
Serial.print(samplecount, DEC);
Serial.print(" ");
Serial.print(x, DEC);
Serial.print(',');
Serial.println(y, DEC);
} // 5
samplecount = samplecount +1;
delay(50); // wait 50ms before reading next sample
} // end of sample read
// open the file.
File dataFile = SD.open(filename, FILE_WRITE);
// if the file is available, write to it:
if (dataFile) { // write to file routine
dataFile.println(dataString); //dataFile.println(dataString);
dataFile.close();
}
// if testmode is true print to the serial port too:
if (testmode == 1){
Serial.println(dataString);
}
// if the file isn't open, turn on red LED on error:
else { // error trap
// turn on red led on fail
digitalWrite(redledPin, LOW);
digitalWrite(grnledPin, HIGH);
digitalWrite(bluledPin,HIGH);
Serial.println("Program Terminated");
while (pend < 1){ // wait here till reset.
wait= millis();
}
return; // terminate program
} // End error trap
}
// } // End of sample read/write loop
/*
while (pend < 1){ // wait here till reset.
wait= millis();
*/
// }
}// End of main loop