9 DOF sensor stick + Open Log trouble

I’m having a bit of trouble with my rocket sensor I have a code and it works somewhat
but I cannot get the sd card “open log” to read when power is connected which is tough because I only have a day to complete and test the code any other time I’ll just give up but a grade is involved and I really don’t want to let the team down in short this is an Arduino Forum Emergency! Please help

here’s the code

#include <Wire.h>
#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;

//i2c write
void i2c_write(int address, byte reg, byte data) {
  // Send output register address
  Wire.beginTransmission(address);
  Wire.send(reg);
  // Connect to device and send byte
  Wire.send(data); // low byte
  Wire.endTransmission();
}
//i2c read
void i2c_read(int address, byte reg, int count, byte* data) {
  int i = 0;

  // Send input register address
  Wire.beginTransmission(address);
  Wire.send(reg);
  Wire.endTransmission();
  // Connect to device and request bytes
  Wire.beginTransmission(address);
  Wire.requestFrom(address,count);
  while(Wire.available()) // slave may send less than requested 
  {
    char c = Wire.receive(); // receive a byte as character
    data[i] = c;
    i++;
  }
  Wire.endTransmission();
}

//Accelerometer

#define  ADXL345_ADDRESS (0xA6 >> 1)

//There are 6 data registers, they are sequential starting 
//with the LSB of X.  We'll read all 6 in a burst and won't
//address them individually
#define ADXL345_REGISTER_XLSB (0x32)

//Need to set power control bit to wake up the adxl345
#define ADXL_REGISTER_PWRCTL (0x2D)
#define ADXL_PWRCTL_MEASURE (1 << 3)

void init_adxl345() {
  byte data = 0;

  i2c_write(ADXL345_ADDRESS, ADXL_REGISTER_PWRCTL, ADXL_PWRCTL_MEASURE);

  //Check to see if it worked!
  i2c_read(ADXL345_ADDRESS, ADXL_REGISTER_PWRCTL, 1, &data);
  Serial.println((unsigned int)data);
}

int accelerometer_data[3];

void read_adxl345() {
  byte bytes[6];
  memset(bytes,0,6);

  //read 6 bytes from the ADXL345
  i2c_read(ADXL345_ADDRESS, ADXL345_REGISTER_XLSB, 6, bytes);

  //now unpack the bytes
  for (int i=0;i<3;++i) {
    accelerometer_data[i] = (int)bytes[2*i] + (((int)bytes[2*i + 1]) << 8);
  }
}


//Gyroscope

#define ITG3200_ADDRESS (0xD0 >> 1)
//request burst of 6 bytes from this address
#define ITG3200_REGISTER_XMSB (0x1D)
#define ITG3200_REGISTER_DLPF_FS (0x16)
#define ITG3200_FULLSCALE (0x03 << 3)
#define ITG3200_42HZ (0x03)

void init_itg3200() {
  byte data = 0;

  //Set DLPF to 42 Hz (change it if you want) and
  //set the scale to "Full Scale"
  i2c_write(ITG3200_ADDRESS, ITG3200_REGISTER_DLPF_FS, ITG3200_FULLSCALE | ITG3200_42HZ);

  //Sanity check! Make sure the register value is correct.
  i2c_read(ITG3200_ADDRESS, ITG3200_REGISTER_DLPF_FS, 1, &data);

  Serial.println((unsigned int)data);
}

int gyro_data[3];

void read_itg3200() {
  byte bytes[6];
  memset(bytes,0,6);

  //read 6 bytes from the ITG3200
  i2c_read(ITG3200_ADDRESS, ITG3200_REGISTER_XMSB, 6, bytes);  //now unpack the bytes
  for (int i=0;i<3;++i) {
    gyro_data[i] = (int)bytes[2*i + 1] + (((int)bytes[2*i]) << 8);
  }
}

//Magenetometer

#define HMC5843_ADDRESS (0x3C >> 1)
//First data address of 6 is XMSB.  Also need to set a configuration register for
//continuous measurement
#define HMC5843_REGISTER_XMSB (0x03)
#define HMC5843_REGISTER_MEASMODE (0x02)
#define HMC5843_MEASMODE_CONT (0x00)

void init_hmc5843() {
  byte data = 0;
  //set up continuous measurement
  i2c_write(HMC5843_ADDRESS, HMC5843_REGISTER_MEASMODE, HMC5843_MEASMODE_CONT);

  //Sanity check, make sure the register value is correct.
  i2c_read(HMC5843_ADDRESS, HMC5843_REGISTER_MEASMODE, 1, &data);
  Serial.println((unsigned int)data);
}

int magnetometer_data[3];
void read_hmc5843() {
  byte bytes[6];
  memset(bytes,0,6);

  //read 6 bytes from the HMC5843
  i2c_read(HMC5843_ADDRESS, HMC5843_REGISTER_XMSB, 6, bytes);

  //now unpack the bytes
  for (int i=0;i<3;++i) {
    magnetometer_data[i] = (int)bytes[2*i + 1] + (((int)bytes[2*i]) << 8);
  }
}


// Code Begins

void setup()
//SDcard
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);

  // 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.");
  {
    Wire.begin();
    Serial.begin(9600);

    for(int i = 0; i < 3; ++i) {
      accelerometer_data[i] = gyro_data[i] = magnetometer_data[i] = 0;
    }

    init_adxl345();
    init_hmc5843();
    init_itg3200();
  }  
}

//Sensor stick


void loop() 
//SD Card  
{
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 4; analogPin < 5; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      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");
    {
      read_adxl345();

      Serial.print("ACCEL: ");
      Serial.print(accelerometer_data[0]);
      Serial.print("\t");
      Serial.print(accelerometer_data[1]);
      Serial.print("\t");
      Serial.print(accelerometer_data[2]);
      Serial.print("\n");

      read_hmc5843();

      Serial.print("MAG: ");
      Serial.print(magnetometer_data[0]);
      Serial.print(",");
      Serial.print(magnetometer_data[1]);
      Serial.print(",");
      Serial.print(magnetometer_data[2]);
      Serial.print("\n");


      read_itg3200();

      Serial.print("GYRO: ");
      Serial.print(gyro_data[0]);
      Serial.print("\t");
      Serial.print(gyro_data[1]);
      Serial.print("\t");
      Serial.print(gyro_data[2]);
      Serial.print("\n");

      //Sample at 10Hz
      delay(100); 
    } 
  } 
}

any and all help is needed and

  for (int analogPin = 4; analogPin < 5; analogPin++) {

It’s amazing the creative ways people come up with to do something once.

but I cannot get the sd card “open log” to read when power is connected

It will probably be even harder for it to read when there is no power connected.

which is tough because I only have a day to complete and test the code any other time I’ll just give up but a grade is involved and I really don’t want to let the team down in short this is an Arduino Forum Emergency!

I suggest that you start two weeks ago, then.

What exactly is the problem?

thanks for the tips luckly I did get an extension on the project for about the time the problem is that simply when I add the power to the arduino and check through the serial monitor the sd card sometimes fail to record data from the sensors but sometimes it works ok in short I would like the open log to record from the sensor stick

Thanks for your help everyone but it looks like this project is a bit too difficult for anyone I'll try to understand it more to see if I can get some help

sometimes fail to record data from the sensors but sometimes it works ok

That's a bit vague. What is your debugging output telling you.

The SD library requires quite a lot of SRAM.

You're using the String class too, so it's quite possible that you are getting memory fragmentation, and running out of SRAM.

that's the thing i'm not getting any errors from the serial monitor also it work sometimes but a better way of asking how so you get open log to get data from analog ports 4 and 5 because the sparkfun 9dof sensorstick works ok but the datalogger isn't my sd card sometimes fail to startup and sometimes it works fine maybe what you say is correct and if so what can be done about it?

#include <SD.h>


const int chipSelect = 4;

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // 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.");
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      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");
  } 
}

I thought by changing the pins to Analog 4and5 it will work
in short this

Current:
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += “,”;

Mine:
// read three sensors and append to the string:
for (int analogPin = 5; analogPin < 4; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += “,”;

I thought this would read the sensor stick in analog pins 4 and 5
but who knows what wrong

for (int analogPin = 5; analogPin < 4; analogPin++) {

Starting with analogPin 5, execute this block of code while analogPin is less than 4. At the end of each execution of the block of code, increment analogPin by 1.

So, how many times is that block of code going to be executed? None, zip, nada, zilch…

so then it should be

for (int analogPin = 4; analogPin < 5; analogPin++) {

just a thought what if i use

#include <NewSoftSerial.h>
NewSoftSerial logger(4,5);

void setup() {
pinMode(4, INPUT);
pinMode(5, OUTPUT);
 delay(1000); //just to let everything settle down after power up
 Serial.begin(9600);
 logger.begin(9600);

 logger.println("I should be written to the file!");
}

void loop() { }

to read data from the sensor stick and record my data

Sorry correction what if I used this code to read from the sensor stick

#include <SD.h>

void setup() {
pinMode(4, INPUT);
pinMode(5, OUTPUT);
 delay(1000); //just to let everything settle down after power up
 Serial.begin(9600);
 SD.begin(9600);

}

I don’t know what you’re trying to do, and I don’t think you do either.

As far as I can see you’re opening the file repeatedly (or trying to), using dynamic memory to output a simple number, and doing nothing to control or record how frequently this happens. They’re your grades at the end of the day, but I wouldn’t want to submit code like that if it was my grades on the line.

umm just have open log to read from A4 and A5 of the arduino please read the upper post

Are you using one of these - http://www.sparkfun.com/products/9530 ?

so then it should be

The middle condition is a while clause. The body of the for loop will execute as long as the middle clause is true. So, 4 < 5 is true. Then, 4 is incremented to 5, and 5 is not less than 5, so the loop is only executed once.

Why you need a String object and a for loop to read from two pins and write the data to a file, though, is a mystery.

Sigma-x: umm just have open log to read from A4 and A5 of the arduino please read the upper post

Who is this reply aimed at?

sorry I thought someone else asked the same question

I figured out some of my problem the sd logger is not an openlog device that's why the code sometimes work but I still have no clue what the openlogger code is but this is a breakthrough

still no luck

AHHH HAAA!!

I'm not using the open log code i'm using the arduino example sd code also I didn't format my open log to logging mode!

but it doesn't explains how it worked sometimes? and logged data from the serial monitor??

my guess it that perhaps the sd code from the example file works like the open log code if that's the case then I had the code all along I just didn't format the darn thing from the serial monitor any expert opinion is welcome here