Read/Write in SD card

hye..

i am new in arduino and i have a problem in using it. i hope you can help me.

my project is make a program to the rain gauge which is every count of 'tick' in the rain gauge will be write in the SD card.

this is my coding that i have try using SDuFat..

#include <SdFat.h>
#include <SdFatUtil.h>

Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

int powerSD = 8;
int ledPin =  13;           // For monitoring purpose only if action occur
int pushButton = 9;         // Set this to RainGauge Sensor Pin
int pushButtonState = 0;    // monitor the State of RainGaugeState,
int val = 0;
int lastButtonState = 0;    // To compare with current state
                            // Eliminate repeating status on same state

// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
void error_P(const char *str)
{
  PgmPrint("error: ");
  SerialPrintln_P(str);
  if (card.errorCode())
  {
    PgmPrint("SD error: ");
    Serial.print(card.errorCode(), HEX);
    Serial.print(',');
    Serial.println(card.errorData(), HEX);
  }
  while(1);
}

// Write Carriage Return/Line Feed to a file
void writeCRLF(SdFile &f)
{
  f.write((uint8_t *)"\r\n", 2);
}

// Write an unsigned number to a file
void writeNumber(SdFile &f, uint32_t n)
{
  uint8_t buf[10];
  uint8_t i = 0;
  do {
    i++;
    buf[sizeof(buf) - i] = n%10 + '0';
    n /= 10;
  } while (n);
  f.write(&buf[sizeof(buf) - i], i);
}

// Write a string to a file
void writeString(SdFile &f, char *str)
{
  uint8_t n;
  for (n = 0; str[n]; n++);
  f.write((uint8_t *)str, n);
}

void setup()
{
  // initialize the digital pin as an output:
  pinMode(powerSD, OUTPUT);
  //pinMode(pushButton, INPUT);
  Serial.begin(115200);

  Serial.println("Type any character to start");
  while (!Serial.available());

  if (!card.init()) error("card.init");                // initialize the SD card
  if (!volume.init(card)) error("volume.init");        // initialize a FAT volume
  if (!root.openRoot(volume)) error("openRoot");       // open the root directory

  // create a new file
  char name[] = "WRITE00.TXT";
  for (uint8_t i = 0; i < 100; i++) {
    name[5] = i/10 + '0';
    name[6] = i%10 + '0';
    if (file.open(root, name, O_CREAT | O_EXCL | O_WRITE)) break;
  }
  if (!file.isOpen()) error ("file.create");
  Serial.print("Writing to: ");
  Serial.println(name);

  pushButtonState = digitalRead(pushButton);
   if(pushButtonState != lastButtonState)
  {
    if (pushButtonState == HIGH )
    {
      digitalWrite(ledPin, pushButtonState);
      delay(250);
      writeString(file, " millis = ");
      writeNumber(file, millis());
      writeCRLF(file);
    }
    lastButtonState = pushButtonState;
  }
  else
  {
    digitalWrite(ledPin, LOW);    // set the LED off
  }
  delay(200);

  // close file and force write of all data to the SD card
  file.close();
  Serial.println("Done");
}

void loop(void) {}

this code have no error but it give me a result of SD card not initialized. like this...

Type any character to start
error: card.init
SD error: 1,FF

for your knowledge, i am using arduino duemilanove atMega168 and Libelium MicroSD module.

Did you format the SD card using the PC?

What format?

i did not format it using PC..

did i need to format the SD card first? if so, how can i format it?

Yes, you need to format it first.

Do you have some hardware that lets you connect the SD card to the PC? If so, the SD card should appear as a hard drive on the PC. Use the middle mouse button to post the contextual menu. Select Format.... Choose the appropriate format and click OK.

thanks..

now my SD card is in FAT32 format..

so, what should i do now because it still give me the same problem..
did my coding above is right? or it is any problem with that coding?

Aisya,

I haven't checked the code, but I think the card needs to be formatted as FAT, also known as FAT16. That will limit how much of the card you can use. There may also be a limit on the card size to work with the SDuFAT library.

Regards,

-Mike