Hi guys,
First of all I should say that I'm very new to this whole area; this is the first project I'm doing on my own, and I don't have any kind of electronics background, so please forgive my ignorance, and if I've posted this in the wrong place please let me know...
I'm currently working on on a project to build a datalogger for a go-kart to aid in driver performance analysis, saving all the info to an onboard SD card to analyse later. The aim is that eventually it'll be something fairly extensive, but for the first stage I'm just looking at implementing accelerometer and gyro data.
To that end, I've purchased an ethernet shield and the 9 axes motion shield. I've done some playing around, and I've figured out how to get the data off the motion shield (despite the difficulty finding much documentation for it...) and, separately, how to stream data straight to the SD card.
I'm having problems occur when I try to combine the two, however. Namely, when the sketch uses the SD card, the motion sensor no longer updates, so I end up with an output file which is hundreds of identical lines no matter how I move the sensor while it's logging.
Could the problem be that the two shields use one of the pins for different tasks and they're conflicting? I haven't made any modifications or done any soldering to the boards; might there have been something I've missed in the documentation?
The code I'm using is below:
#include <SPI.h>
#include <SD.h>
#include "NAxisMotion.h"
#include <Wire.h>
const int chipSelect = 4;
const int interval = 100; // Possible to get this from something in the SD card?
// Might have to do it inside setup()?
NAxisMotion mySensor; //Object that for the sensor
unsigned long lastStreamTime = 0; //To store the last streamed time stamp
bool updateSensorData = true; //Flag to update the sensor data. Default is true to perform the first read before the first stream
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
I2C.begin(); //Initialize I2C communication to the let the library communicate with the 9DoF sensor.
//Sensor Initialization
mySensor.initSensor(); //The I2C Address can be changed here inside this function in the library
mySensor.setOperationMode(OPERATION_MODE_NDOF); //Can be configured to other operation modes as desired
mySensor.setUpdateMode(MANUAL); //The default is AUTO. Changing to manual requires calling the relevant update functions prior to calling the read functions
mySensor.writeAccelConfig(ACCEL_RANGE_8G,ACCEL_BW_7_81HZ,ACCEL_NORMAL);
//Setting to MANUAL requires lesser reads to the sensor
mySensor.updateAccelConfig();
updateSensorData = true;
Serial.println();
Serial.println("Default accelerometer configuration settings...");
Serial.print("Range: ");
Serial.println(mySensor.readAccelRange());
Serial.print("Bandwidth: ");
Serial.println(mySensor.readAccelBandwidth());
Serial.print("Power Mode: ");
Serial.println(mySensor.readAccelPowerMode());
Serial.println("Streaming in ..."); //Countdown
Serial.print("3...");
delay(200); //Wait for a bit
Serial.print("2...");
delay(200); //Wait for a bit
Serial.println("1...");
delay(200); //Wait for a bit
}
void loop() {
if (updateSensorData) //Keep the updating of data as a separate task
{
mySensor.updateAccel(); //Update the Accelerometer data
mySensor.updateLinearAccel(); //Update the Linear Acceleration data
mySensor.updateGravAccel(); //Update the Gravity Acceleration data
mySensor.updateCalibStatus(); //Update the Calibration Status
updateSensorData = false;
}
if ((millis() - lastStreamTime) >= interval) {
// make a string for assembling the data to log:
String dataString = "";
dataString += mySensor.readAccelX(); //Accelerometer X-Axis data
dataString += ",";
dataString += mySensor.readAccelY(); //Accelerometer Y-Axis data
dataString += ",";
dataString += mySensor.readAccelZ(); //Accelerometer Z-Axis data
dataString += ",";
dataString += mySensor.readGravAccelX(); //Accelerometer X-Axis data
dataString += ",";
dataString += mySensor.readGravAccelY(); //Accelerometer Y-Axis data
dataString += ",";
dataString += mySensor.readGravAccelZ(); //Accelerometer Z-Axis data
dataString += ",";
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
updateSensorData = true;
}
}
Any guidance you can give me would be greatly appreciated!!!