SD writer code ultra light

Hello friends!

I need your help. I have 25,142 byte used (of 32,256 byte available) for my code.

So, I need to write on SD a datastream [name, time, value x8] in a .csv file, but just with 7 kb :frowning:

there's someone can help with this?

Really really thanks for your help! :smiley: i've tried all day and i cant do it (SD or SdFat) =(

Two examples:

#include <SdFat.h>
SdFat sd;
SdFile myFile;

void setup() {

  if (!sd.begin(4, SPI_HALF_SPEED)) sd.initErrorHalt();

  if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening test.txt for write failed");
  }
  myFile.println("testing 1, 2, 3.");

  myFile.close();
}

void loop() {
}

10,842 bytes :frowning:

#include <SD.h>
File myFile;

void setup(){

   pinMode(10, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
  }

  myFile = SD.open("test.txt", FILE_WRITE);

  if (myFile) {
    myFile.println("testing 1, 2, 3.");
    myFile.close();
  } 
}

void loop(){
}

12,856 bytes :frowning:

Thxs y gracias again! :smiley:

I wrote a small library for Standard SD cards, 2 GB or less, formatted FAT16. The library is here Google Code Archive - Long-term storage for Google Code Project Hosting..

The total size of this sketch is 7126 bytes on an Uno with Arduino 1.02.

#include <Fat16.h>
SdCard card;
Fat16 file;

void setup() {
  Serial.begin(9600); 
  if (!card.init() || 
    !Fat16::init(&card) ||
    !file.open("MYFILE.TXT", O_APPEND | O_CREAT | O_WRITE)) {
    Serial.println("init failed");
    while(1);
  }
  file.println("append 1, 2, 3");
  file.close();
  Serial.println("Done");
}
void loop() {}

It should add less than 7 KB to your sketch since the following sketch take 1928 bytes.

void setup() {
  Serial.begin(9600);
  Serial.println("Done");
}
void loop() {}

That is great. I will try it with Bitlash and celebrate the extra free RAM. Thank you.

Are there any differences in the API compared with its bigger cousin that one should be aware of?

Best,

-br

Mr fat16lib:

Check this! "Binary sketch size: 29,932 bytes (of a 32,256 byte maximum)"

Thank you so much!

Well well now i'm working with your code, but...

I'm using the LCD shield in this proyect, so the first code i used have this:

...
pinMode(10, OUTPUT);
if (!SD.begin(4)) 
...{

***pi 4 and 10 are no connected to LCD shield, and before use Fat16 my code was working :frowning: ***

Do i need this part? because i read on SDCARD this must to be declared:

SdCard::init(uint8_t speed, uint8_t [b]chipSelectPin[/b])

Thanks for you help with this! :smiley:

You need to change this line:

  if (!card.init() ||

to:

  if (!card.init(speed, chipSelectPin ) ||

Where

uint8_t SdCard::init ( uint8_t speed,
uint8_t chipSelectPin
)

Initialize a SD flash memory card.

Parameters:
[in] speed Set SPI Frequency to F_CPU/2 if speed = 0 or F_CPU/4 if speed = 1.
[in] chipSelectPin SD chip select pin number.

Returns:
The value one, true, is returned for success and the value zero, false, is returned for failure.

In your case chipSelectPin is 4 and first try speed = 0.

You don't need this but it won't hurt:

  pinMode(10, OUTPUT);