Revisited – Fixed some bugs, improved stability, changed the file structure for mistake-proof reading, added Long integer support.

For my AquaTank project I needed the ability to change settings of the system and I needed them to stay after power loss.
There is an option to use Arduinos built-in EEPROM to pemanently store data, but they have read/write cycle limit and its messy.
So for my Project I decided to use an external SD card and store the values inside a file – in my case its SETTINGS.TXT

SETTINGS.TXT

The structure of the file goes like this:
[parameter_name1=value1]
[parameter_name2=value2]
[parameter_name3=value3]

In my example its:
[exINT=15]
[exFloat=1.12345]
[exBoolean=1]
[exLong=2123456789]

The brackets “[” and “]” were added in the revisit, according toΒ Gregor van Egdom, some text editors have different formatting and it was messing with the new line detection,
now the code reads only within the brackets, so its more solid and won’t get confused by anything.

The code was even tested with the following file and worked flawlessly (whatever is outside of the brackets is not read by the code):
[exINT=15] THIS IS NOT READ
[exFloat=1.12345] BLAH BLAH
[exBoolean=1] This is a comment
[exLong=2123456789] LOL

This sums all my needs storing integers,long integers, floats, and boolean variables to specify if some feature is on or off

The Setup

Using any available Arduino SD Card Shield even a DIY one (will be soon),
You have 2 general functions that you use :

readSDSettings – opens the file, and going line by line separating the parameter name and the value into 2 strings, using another function, applySetting that gets both strings with a simple if functions converts it to the right type (int,float,boolean,long) and applies to the right parameter.

writeSDSettings – first thing deletes the SETTINGS.TXT file and creates a new one, using the current parameters in the arduinos memory

The Code:

The code last tested on Arduino IDE 1.6.3 of Arduino.cc

Hope it helps you with your project πŸ™‚