Micro SD Card not working

Hi all,

WARNING

Till you comment look the video bellow.

So I got he micro SD Card module followed the video and everything its working :smiley: you may say now then why you made a Topic about it if everything its working :smiley:

This is the code from the video and the code I’m using too

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

int CS_PIN = 10;

File file;

void setup()
{

  Serial.begin(9600);

  initializeSD();
  createFile("test.txt");
  writeToFile("This is sample text!");
  closeFile();

  openFile("prefs.txt");
  Serial.println(readLine());
  Serial.println(readLine());
  closeFile();
}
void loop()
{
}

void initializeSD()
{
  Serial.println("Initializing SD card...");
  pinMode(CS_PIN, OUTPUT);

  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
}

int createFile(char filename[])
{
  file = SD.open(filename, FILE_WRITE);

  if (file)
  {
    Serial.println("File created successfully.");
    return 1;
  } else
  {
    Serial.println("Error while creating file.");
    return 0;
  }
}

int writeToFile(char text[])
{
  if (file)
  {
    file.println(text);
    Serial.println("Writing to file: ");
    Serial.println(text);
    return 1;
  } else
  {
    Serial.println("Couldn't write to file");
    return 0;
  }
}

void closeFile()
{
  if (file)
  {
    file.close();
    Serial.println("File closed");
  }
}

int openFile(char filename[])
{
  file = SD.open(filename);
  if (file)
  {
    Serial.println("File opened with success!");
    return 1;
  } else
  {
    Serial.println("Error opening file...");
    return 0;
  }
}

String readLine()
{
  String received = "";
  char ch;
  while (file.available())
  {
    ch = file.read();
    if (ch == '\n')
    {
      return String(received);
    }
    else
    {
      received += ch;
    }
  }
  return "";
}

As your looking the video there is only a demonstration how to write in the SD CARD a char “text” so
i done a modification to write integer.

I added :

int writeToFileI(int)
{
  if (file)
  {
    file.println( );
    Serial.println("Writing to file: ");
    Serial.println( );
    return 1;
  } else
  {
    Serial.println("Couldn't write to file");
    return 0;
  }
}

So what i tried to do and done more than 20+ tested and modification is to write on the sc card integer and char while looping, by looking the video you will see there is no demonstration in the loop so i thought that will not be very hard to do.

My loop code 1:

  writeToFileI(i);
  writeToFile(",");
    writeToFile("Loop");
  digitalWrite(led, HIGH);
  delay(500);
  digitalWrite(led, LOW);
 
  i++;

     delay(500);

My loop code 2:

  openFile("GPS.txt");
  writeToFileI(i);
  writeToFile(",");
    writeToFile("Loop");
  digitalWrite(led, HIGH);
  delay(500);
  digitalWrite(led, LOW);
 
  i++;
    closeFile();
     delay(500);

and other several tested that all failed. I got the 1st “writetoFile” text from the setup but in the loop
there is nothing on my sd card what ever i tried I got no write/saves from the loop code.

So can someone explain to me what can be the problem??

int writeToFileI(int)

So, the function takes are argument of type int, but has no name. How on earth do you plan to write that int to a file?

So, the function takes are argument of type int, but has no name. How on earth do you plan to write that int to a file?

Yea ok :D but my main point is that i tried to do same thing in loop and didn't worked.

So, the function takes are argument of type int, but has no name. How on earth do you plan to write that int to a file?

I put the name and it's ok right now but look the picture bellow, can someone people tell me why there is no data in my *.txt file?

As you see from the serial monitor it's writing really well and nice but when i open the cd card on my PC there is nothing, only the setup write:

http://pix.my/YW08kfYD |500x282 Pic. Nr.1 without close.

After that i thought because you need to open the file to write inside and close to be saved i tried that too but nothing. http://pix.my/mkvL7Rfz |500x282 Pic. Nr.2 with close.

So what can be wrong? why the data is not saving in the loop?

Test number who the hack knows …

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

int CS_PIN = 10;

File file;
int led = 6;
void setup()
{

  Serial.begin(9600);
pinMode(led, OUTPUT);

  initializeSD();
 // createFile("GPS.txt");
// writeToFile("setup");
//  closeFile();
  
}
int i;

void loop()
{
  
 String stringOne = String(i);
 Serial.println(stringOne);
 writeEntryToFile(stringOne);
  digitalWrite(led, HIGH);
  delay(500);
  digitalWrite(led, LOW); 
  i++;
     delay(500);

}


void writeEntryToFile(String entry)
{
  openFile("GPS.txt");
  Serial.println(entry);
  writeToFile(entry);
  closeFile();
}

void initializeSD()
{
  Serial.println("Initializing SD card...");
  pinMode(CS_PIN, OUTPUT);

  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
}

int createFile(char filename[])
{
  file = SD.open(filename, FILE_WRITE);

  if (file)
  {
    Serial.println("File created successfully.");
    return 1;
  } else
  {
    Serial.println("Error while creating file.");
    return 0;
  }
}

int writeToFile(String text)
{
  if (file)
  {
    file.println(text);
    Serial.println("Writing to file: ");
    Serial.println(text);
    return 1;
  } else
  {
    Serial.println("Couldn't write to file");
    return 0;
  }
}

void closeFile()
{
  if (file)
  {
    file.close();
    Serial.println("File closed");
  }
}

int openFile(char filename[])
{
  file = SD.open(filename);
  if (file)
  {
    Serial.println("File opened with success!");
    return 1;
  } else
  {
    Serial.println("Error opening file...");
    return 0;
  }
}

String readLine()
{
  String received = "";
  char ch;
  while (file.available())
  {
    ch = file.read();
    if (ch == '\n')
    {
      return String(received);
    }
    else
    {
      received += ch;
    }
  }
  return "";
}

I even tried to convert the numbers (int) intro strings so to be able to write them on the sd card
but still nothing, NOTHING ON THE FREAKING TXT :confused: I can’t understand why it’s not writing on the sd card/txt file.

The file is created i can create it from the code too if i set a write text from the setup then it works but when i try to do something in loop nothing, only in the serial monitor i see the data i see that file is oppened write the test and closed when i oped the sd card on my PC there is nothing inside…

WHAT im doing wrong?? :smiley:
I’m getting crazy… time to sleep 5am

int openFile(char filename[])
{
 //file = SD.open(filename);
 file = SD.open(filename,FILE_WRITE);
  if (file)
  {
    Serial.println("File opened with success!");
    return 1;
  } else
  {
    Serial.println("Error opening file...");
    return 0;
  }
}

You need to open the file for writing.

WHAT im doing wrong?

You are pissing resources away uselessly misusing/abusing/using the String class. Knock that shit off now. Maybe, someday, when you have a clue, you can resume using the String class.

Paul's right: The String class is a crutch for those who don't want to use char arrays instead. Change stringOne to char stringOne[MAXSIZE] etc. Next, do the calls to initializeSD() and createFile() create an entry on the SD card? What does readFile() do when called?

You need to open the file for writing.

I'm opening the file, just look careful in the code:

http://pix.my/urZ9vRQu |500x296

Problem Solved

I knew that i need to make the int as string but i make a mistake on the string yes my fault
it was 5am so i needed sleep, when you need sleep your brain is not working very good.

Code:

#include <SD.h>


const int chipSelect = 10;
int led = 6;
void setup()
{
 
  Serial.begin(9600);
   while (!Serial) {
    ; 
  }

  pinMode(led, OUTPUT);
  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);
  
  
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
}
int i;
void loop()
{

  String dataString2 = "";
  dataString2 += String(i);
  
  File dataFile = SD.open("GPS.txt", FILE_WRITE);

  
  if (dataFile) {
    dataFile.println(dataString2);
    dataFile.close();

    Serial.println(dataString2);
  }  
  else {
    Serial.println("error opening datalog.txt");
  } 
  digitalWrite(led,HIGH);
  delay(500);
  digitalWrite(led,LOW);
  delay(500);
  i++;
}

I knew that i need to make the int as string

Why? The SD class is perfectly capable of converting the value to a string as it writes it to the file.