Go Down

Topic: SD writer code ultra light (Read 1 time) previous topic - next topic

nokasho

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 :(

there's someone can help with this?


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

\{end}

nokasho

Two examples:

Code: [Select]

#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 :(

Code: [Select]

#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 :(

Thxs y gracias again! :D
Me

\{end}

fat16lib

I wrote a small library for Standard SD cards, 2 GB or less, formatted FAT16. The library is here http://code.google.com/p/fat16lib/downloads/list.

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

Code: [Select]

#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.
Code: [Select]

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

billroy

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

nokasho

Mr fat16lib:

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


Thank you so much!
Me

\{end}

nokasho

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:

Code: [Select]
...
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 :( ***

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

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

Thanks for you help with this! :D
Me

\{end}

fat16lib

You need to change this line:
Code: [Select]

  if (!card.init() ||

to:
Code: [Select]

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

Where
Quote

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:
Code: [Select]
  pinMode(10, OUTPUT);

Go Up