SD.open Returning 0

Hello,

I have a datalogging + LCD stacking shields and I am providing signals to store in a SD card (SDHC 16gb SanDisk Extreme Pro). My code checks and returns a success when SD.begin() is sent.

But when I create an object and try to open open it.

File dataFile = SD.open("LOG.csv", FILE_WRITE);

I keep getting a 0 and I cannot open the SD card.

Any suggestions?

Any suggestions?

The usual one. Post your code.

PaulS, sorry about that - I have a poor tendancy leaving it out.

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

int CS_PIN = 10;

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

const byte MY_ADDRESS = 42;

float REFRESH_RATE = 0.0;
long ID = 1;

void setup()
{
  Serial.begin(9600);
  Wire.begin(MY_ADDRESS);  
  Wire.onReceive(receiveEvent);
  
  lcd.begin(16,2);
  pinMode(CS_PIN, OUTPUT);
  
  lcd.setCursor(0,0);
  lcd.print("ME: ");
  lcd.setCursor(8,0);
  lcd.print("SE: ");
  lcd.setCursor(0,1);
  lcd.print("Datalog: ");
  
  if (!SD.begin(CS_PIN))
  {
    Serial.println("Card Failure");
    return;
  }
  Serial.println("Card Ready");
}

volatile boolean haveData = false;
volatile int X;
volatile int Y;
int LOOP_COUNTER = 0;

void loop()
{  
  if (haveData)
  {
    lcd.setCursor(3,0);
    lcd.print(X);
    lcd.setCursor(11,0);
    lcd.print(Y);
    haveData = false;   
  }
  
  File dataFile = SD.open("LOG.csv", FILE_WRITE);
  
  if (LOOP_COUNTER == 500)
  {
    if (dataFile)  <--- always prints 0
    {
      dataFile.print(millis());
      dataFile.print(", ");
      dataFile.print(ID);
      dataFile.print(", ");
      dataFile.print(X);
      dataFile.print(", ");
      dataFile.println(Y);
      dataFile.close();
      lcd.setCursor(9,1);
      lcd.print("Success");
      ID++;
    }
    else
    {
      lcd.setCursor(9,1);
      lcd.print("Failure");
    }
    
  LOOP_COUNTER = 0;  
  
  }   
  LOOP_COUNTER++;
}



void receiveEvent (int howMany)
{
  if (howMany >= (sizeof X) + (sizeof Y))
  {
    I2C_readAnything (X);
    I2C_readAnything (Y);
    haveData = true;
  }
}

int CS_PIN = 10;

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

const byte MY_ADDRESS = 42;

float REFRESH_RATE = 0.0;

By convention, all capital letter names are reserved for constants. So, these are all good.

int LOOP_COUNTER = 0;

Now, a counter can hardly be considered constant, can it?

I'd try stripping stuff out of that program, to see whether the problem is with the card, or a library you are using, or some other issue. For instance, the LCD has nothing to do with reading from/writing to the SD card. So, out it goes.

Similarly, the I2C and Wire classes have nothing to do with reading from/writing to the SD card. So, out they go.

That leaves you with a much smaller program to test JUST the write to the card.

What SD shield are you using? Some of them do not support such large cards.

PaulS,

Thank you for your help, I will do the changed and test the SD card alone - I contacted the manufacturer and the shield should be okay for the HCSD I am using. The shield I am using is below:

http://arduino-direct.com/sunshop/index.php?l=product_detail&p=290

Sorry for such a late reply - I have been really sick with the flu.

Daniel

The SD Card works just fine, but I realized that I was using the wrong library. I need to be using SdFat, it is very robust so I hope to understand this quickly, thank you for your help.

Daniel