SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By happyboy
#120539
Hello

sorry for my poor English,

How to arduino import var values from text files to read from sd card!
My text file is:
var.txt
myvar1 = 120
myvar2 = 10
myvar3 = 60
....
....
By fll-freak
#120542
You will need to first open the SD card with a library like SdFat.

Next you will need to write a routine to read each line, parse it and perform whatever action you need based on the value.

Opening the file is not likely to be overly hard. Parsing the file with the limited memory of an Arduino might be a complex challenge. I found it was easier to write my data to the file in a binary (rather than ASCII) with a strict structure. Yes, it was hard to edit, yes the variables all had to be in a given order, but it was easy to read nay value I wanted with very little overhead.
User avatar
By shimniok
#121460
FWIW, parsing isn't all that tough, even without using fscanf().

It helps if you get to decide the file format. If so, then keep it really simple, with single character tokens. E.g.,

var1 = 13;
var2 = 128;

is harder to parse than:

# var1, var2
13, 128

or

13
128

You have atoi() that you can use for integers. Floats you have to parse with custom code. Or, if interested, I have a simple float parser I just wrote a few weeks ago that you can use. I also have code that splits the line up. I used simple comma separators and split on those tokens.

As for binary... it can be easy. If you put the data in a struct then you just write the struct out raw, using sizeof() to get the size of the struct. Then do the same in reverse to read.

But that's probably more work than it has to be if you just write a simple parser for text.

As for memory, no reason you can't read one line at a time and parse. Shouldn't take much memory.

For the SD card, stick to 2G and below and you can use lighter weight FAT16 libraries that take less memory. And use an AVR with a reasonable amount of mem to begin with. Should work out fine I think.