Creating Presets By Saving current values of variables to the SD card with the push of a button

Greeting everyone. I have been trying different methods for my project for a while and still can't find a solution that works.

I am looking for guidance on how to save the current state of multiple varibles (values represented by numbers) in my program to an SD card.

For example, while my program is running, the values for my variables can be adjusted.

int example = 0; //default for setup

While the program is running I use a button to change the value to my liking

the variable example is now = to 5. // I changed it

Before turning off my controller, I want to be able to hit a button and save the current state of all of my variables to a file on the SD card

Once I power on my controller, it will know to check the SD card and read my file to set all the variables to the previously saved preset.

I understand that I could use EEPROM, but I'd rather use an SD card so that I can simply transfer my presets to any controller running my code and it just works. Also it would give the benifit of others adjusting the varibles on the SD card if wanted, then inserting it into the controller and it reads the file and works.

I even thought of creating a GUI for pc to save presets, but this will limit my project from being about to use those preset on mobile devices. The SD would just handle that in house and not complicate things for other uses.

I even tried using BLYNK. However, when using that app, once you disconnect, then reconnect, all of your settings reset. I think there's a way to prevent it, but again, more apps and downloads and extra things involved that I want to avoid. The SD would incorporate my only needed features, to save and share.

I am using a wio terminal if that helps but I'm assuming it would be a similar process for most boards.

I have tried using the SD library. In the serial monitor, it saves my text file and reads it at the very beginning with the default values, yet this does not save my values later on because it only does it at the beginning. I have tried including the read and write code of the SD library in my loop function, which does update it in the serial monitor, but, once powered off, i am back at square one which defeats the purpose of saving it.

Sorry this all sounds like a lot but I have seen that is possible, but other post that question this topic, when finding the solution, they simply post that they figured it out in the end but their working code isn't included. Understandable, as this give me hope but I am still struggling to find the answer.

Again, my goal is the make a simple program that saves to the SD and is able to recall those settings automatically after being saved (preset).

Program loads sd card and reads,. if it doesn't find anything, it doesn't effect the program. If it finds a previously saved preset file, it loads and updates variables.

Int x = 0; ///// Do some stuff hit buttons that change values...etc


Int x = 25; ///// Hit a button mapped to save my variables to the SD card.

Turn it off, power back on now

Int x = 25;

Thanks in advance for anyone who is willing to help!

The first thing to do is to specify a format how you want to store it. You can opt to e.g. store every variable on a new line or to use a single line where all variable values are stored in a comma separated list. It's your responsibility to know the format when implementing reading and writing in your program.





Next you can have a look at the examples that come with the SD library. For the first one you can use

  yourFile.print(setting1); // save setting
  yourFile.print("\n");     // add end-of-line
  yourFile.print(setting2); // save setting
  yourFile.print("\n");     // add end-of-line

For the second one

  yourFile.print(setting1); // save setting
  yourFile.print(",");     // add comma

When reading, you can Serial.readBytesUntil() - Arduino Reference and read till the "\n" (don't worry about the fact that it is Serial, the function also works for files).

For the first example with every variable on a line, you will have to read a few times (without closing the file) into a character buffer of sufficient size to hold the longest variable.
For the second example, you will only read once into a character buffer of sufficient size to hold the full line and next parse the line using e.g. strtok(3) - Linux manual page.

Once you got a value for a variable, you can convert to integer, float or whatever using functions like atoi, strtol, strtoul, atof etc. and assign it to our variables. You don't have to convert text, just copy it.

You can basically follow the read/write example that comes with the SD library.

For further question, please list the declarations of the variables, e.g.

int x;
float f;
1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.