Pages: [1]   Go Down
Author Topic: SD writer code ultra light  (Read 1743 times)
0 Members and 1 Guest are viewing this topic.
Santiago
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Hello Arduino World!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-sad

there's someone can help with this?


Really really thanks for your help! smiley-grin i've tried all day and i cant do it (SD or SdFat)   smiley-cry
Logged

Me

\{end}

Santiago
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Hello Arduino World!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Two examples:

Code:
#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 smiley-sad

Code:
#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 smiley-sad

Thxs y gracias again! smiley-grin
Logged

Me

\{end}

0
Offline Offline
Edison Member
*
Karma: 64
Posts: 1639
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
void setup() {
  Serial.begin(9600);
  Serial.println("Done");
}
void loop() {}
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Santiago
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Hello Arduino World!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mr fat16lib:

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


Thank you so much!
Logged

Me

\{end}

Santiago
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Hello Arduino World!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
...
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 smiley-sad ***

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

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


Thanks for you help with this! smiley-grin
Logged

Me

\{end}

0
Offline Offline
Edison Member
*
Karma: 64
Posts: 1639
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to change this line:
Code:
  if (!card.init() ||
to:
Code:
  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:
  pinMode(10, OUTPUT);
Logged

Pages: [1]   Go Up
Jump to: