Accelerologger (Accelerometer Datalogger): Help needed with code!

Hi Everyone, For the scientific investigation awards I have been designing an accelerologger device that reads the speed of Its movement. So far I have constructed the hardware.

It consists of an Arduino Nano, an ADXL 3 axis gyro/accelerometer  and a Micro SD Card Reader . So far I have got each device (accelerometer/gyro + datalogger module) working succesfully and now have been trying to combine the code. Here is what I have done so far:

 

#include <SparkFun_ADXL345.h>
#include <SD.h>
#include <SPI.h>

//ADXL345 adxl = ADXL345(10); For SPI Cumunication
ADXL345 adxl = ADXL345();
File myFile;
const int threshold = 1499;
const int switchPin = 7;
int switchState;
int ctr = 0;
int pinCS = 10;


void setup() {
Serial.begin(9600);
Serial.println();
pinMode(pinCS, OUTPUT);
pinMode(switchPin, OUTPUT);

adxl.powerOn();

adxl.setRangeSetting(16);

adxl.setSpiBit(0);

if (SD.begin())
{
Serial.println(“SD card is ready…”);
} else
{
Serial.println(“SD card initialization failed”);
return;
}

// create and open a file
myFile = SD.open(“testRun.txt”, FILE_WRITE);
}

void loop() {
int x, y, z;
adxl.readAccel(&x, &y, &z);
switchState = digitalRead(switchPin);

if (switchState == HIGH) {
ctr++;
if (ctr > threshold) {
if (myFile) {
myFile.println(y);
Serial.println(y);
}
// if the file didn’t open, print an error:
else {
Serial.println(“error opening test.txt”);
}
}
ctr = 0;
} else
{
myFile.close();
Serial.println(“closed”);
}
}

That is it. It all most works perfectly. The only issues I am facing is that none of the accelerometer details are serially printed or written on the SD card in a text file. If anyone can help please take a look at the code and do.

 

EJBuilder

3 Replies to “Accelerologger (Accelerometer Datalogger): Help needed with code!”

  1. Hi EJBuilder,

    Looks like a good project. I had a quick read of the code, but did not spot the error. Do you have a link to the example code for the SD card? I would run that one, and once you have the SD card working, add in one thing at a time from the accelerometer. Make sure you test the write to SD card after each line of code you add. Keep versions backups as you go along, so that if you stuff it up it is easy to go back.

    Lets ask one of the parents if they can spot the error..

    Let us know how you go.

    Bob

  2. Hey Bob

    I went back through and ran the example code for both the data logger and gyro and it worked. I really appreciate what Mike mentioned about the CTR bug! Would you or Mike have any suggestions of a fix?

    thanks

    EJBuilder

Leave a Reply