Arduino Forum

Forum 2005-2010 (read only) => Software => Bugs & Suggestions => Topic started by: BMake on Jan 19, 2011, 06:05 pm

Title: Problem with SD.h ... Serial.print... doesn't work
Post by: BMake on Jan 19, 2011, 06:05 pm
I have been trying to use the examples that come with the SD.h library. the examples did not work but after some hours I found out that if I commented out all the "Serial.print("text") instructions then the program started to work and I could make a new file on the SD card and write some stuff to it (then I put the card in my  computer to see if there was anything on the card, so I have confirmed that it works)

but then I was going to add this function to an other project that I already had written. (add the function to be able to write to SD card) but as soon as I included SD.h (#include SD.h#) then the program went going "nuts" with all the Serial.print() instructions. (sorry for my lack of english).
So it looks like the instruction "Serial.print()"/Serial.println(), is also implemented in the SD.h class or something because if I include the SD.h then my program stops working normally (prints just some bullshit on the Serial Monitor or just keeps restarting on and on)

any solution folks? ::)
Title: Re: Problem with SD.h ... Serial.print... doesn't work
Post by: BMake on Jan 19, 2011, 06:05 pm
these are the products that I am using
http://www.sparkfun.com/products/204
http://www.sparkfun.com/products/666
128MB SD card
Title: Re: Problem with SD.h ... Serial.print... doesn't work
Post by: PaulS on Jan 19, 2011, 06:44 pm
The products that you are using are less important than the software you are using. Otherwise, you'd have posted in Hardware, no?
Title: Re: Problem with SD.h ... Serial.print... doesn't work
Post by: BMake on Jan 20, 2011, 10:15 am
now here is my software (using Arduino 0022)

this program works (writes increment number up to 5000 to the SD card) but if I uncomment all the Serial.print() instructions the program just writes the fyrst Serial.print("Initializing SD card...") to the Serial monitor and freezes, or keeeps just writing "Initializing SD card" forever.

Quote

/*
  SD card read/write
 
 This example shows how to read and write data to and from an SD card file       
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created   Nov 2010
 by David A. Mellis
 updated 2 Dec 2010
 by Tom Igoe
 
 This example code is in the public domain.
        
 */

#include <SD.h>

File file;

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  Serial.begin(9600);

  // Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    //   Serial.println("initialization failed!");
    //   return;
  }


  // Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
}

int j;
void loop()
{
  j++;
  file = SD.open("Test.txt", O_CREAT | O_WRITE | O_APPEND);

  while(millis() < 1000);  // delay so mills() is four digits

  for (int i = 0; i < 5000; i++) {
    file.println(i);
    //Serial.println(i);
  }
  // not needed in this example since close() will call flush.
  file.println(j);
  file.flush();  

  file.close();

}





Title: Re: Problem with SD.h ... Serial.print... doesn't work
Post by: fat16lib on Jan 20, 2011, 04:58 pm
Your sketch seems to work on a 328 Arduino with the comments removed from the Serial.print/Serial.println statements.

Are you using a 168 Arduino?  On a 168 it is marginal.  There is too little RAM with the print/println statements because the literal strings are in RAM.  It crashes and restarts as you describe.

This sketch will probably create a corrupt file since it is unlikely the file will be properly closed.
Title: Re: Problem with SD.h ... Serial.print... doesn't work
Post by: BMake on Jan 21, 2011, 10:17 am
Yes I am using arduino 168, I was just starting using arduino 2 weeks ago.. I thought the arduino software/compiler would give me some warning messages if I was out of ram, how can I see if I am out of ram/if the compiler generates a corrupt file? (just wondering so I can figure it out myself this problem will bug me later)

But thank you so much for finding my problem,  :D :D :D I have been 4 days or semething trying to figure it out  :o
Title: Re: Problem with SD.h ... Serial.print... doesn't work
Post by: PaulS on Jan 21, 2011, 04:52 pm
Quote
I thought the arduino software/compiler would give me some warning messages if I was out of ram

Since SRAM usage is partly static (all your arrays and strings) and partly dynamic (run-time dependent) (heap, stack, etc.), the compiler can not detect when you will, at run-time, run out of SRAM.