Advice for properly reading a SD memory card

Hi. I'm working in an automatic control project for cultives. The idea now is to save three bytes that correspond to the states of three configuration flags into the SD card memory, so in the case that system suffers a reset it could load this important values at the setup(). So far so long I have this code:

void save_to_SD() {
  SD.remove("example.txt");
  myFile = SD.open("example.txt", FILE_WRITE);

  if (myFile) {
    myFile.println(c_auto_lamp_ON);   // parametros de configuración
    myFile.println(c_auto_plump_ON);
    myFile.println(foto_p_veg);
    myFile.close();
  } else {
    Serial.println("error saving test.txt");
  }
  myFile.close();
}

void read_from_SD()   {
  int data;
  myFile = SD.open("example.txt");
  if (myFile) {
    Serial.println("Content of the file:");
    while (myFile.available()) {
      data = myFile.read();
      Serial.write(data);    
    }
    myFile.close();
  } else {
    Serial.println("error reading example.txt");
  }
}

Now what I'd like to do is to be able to read these values from the file AND to specificaly know which value is if the first, the second or the third, and to copy this value to the system flags. I'm not sure how to accomplish this due to my lack of experience reading memory cards and I don't know if I will need to use a string (to save the incoming data in it). I tried using a counter within the while (myFile.available()) { } routine but it didn't work. I'm pretty sure there must be a simple way I just don't know how and I tthought I could get some useful guidance here. Thanks a lot!

For such a tiny amount of data, EEPROM is faster, easier to use, much more reliable than SD cards, and is intended for just that sort of application.

Couple of thoughts...

  1. It seems like your equipment states are bits and can be combined into one byte (8 bits).

  2. Do you have an RTC? If not your data will not have much of a time reference. The last time I used an SD card with an arduino the file date was NOT set.

EEPROM was my first thought. I remember having readed a little bit about it and found some comments about not being a good idea to use Arduino's EEPROM (can't remember why tho). I guess I could read about it further.

  1. Could you please give me some guidance/ clues of how doing it?
  2. The system does has RTC. I don't understand why the date is important, I'm sorry.

Perhaps the date is not important to your project. I only suggested it because many data is time related. I just wanted to point out the SD file date does not work like on a PC.

If you find that eeprom is not suitable due to memory limits, and you need to examine/manipulate data later on, you could try parsing it from the SD card, treating it like a CSV file. This link could get you started. GitHub - michalmonday/CSV-Parser-for-Arduino: It turns CSV string into an associative array (like dict in python)

I don't know, I second think it after reading this.

EEPROM has a total lifetime of ~100,000 write cycles. Be careful when writing code so that you don’t write to EEPROM too often! Remember that erasing memory also is a writing operation.

I even worry a little bit when I program the Arduino with few changes in the code. But if I program something that will write this memory each certain time even after the coding was done then wouldn't this short the device's life? I don't know much about this. But the advantages that you mention about using EEPROM sound really nice, maybe by not overwriting the memory too often then it could be worth.

Yes I quite understand now, I do have thought something about like storing sensors measured data into a memory or data base and then being able to see/track it or even to graph it. Thanks a lot for the observation. I'm guessing this issue could be solved by storing the data and "manually" including the date/time associated to the data ¿? Like what marc-alexander shared about CSV files.

EEPROM has a total lifetime of ~100,000 write cycles.

That is the more or less guaranteed minimum. How much luck have you had in frequently writing to SD cards, which are a type of EEPROM (flash) and have about the same lifetime?

save three bytes that correspond to the states of three configuration flags into the SD card memory,

How often do you expect the configuration to change?

Unlike EEPROM, a new file can be added or the same file can be opened and additional data included. Either way, the key would be either finding the latest file or identifying the last data written.
It seems EEPROM could be searched similarly (read back till first unwritten location found and go from there).

Yes, I understand they both have flash memory. I researched a little bit and found this:

Almost all modern memory cards can withstand at least 100,000 Program/Erase Cycles , and some cards can withstand as many as 10 times more cycles than standard cards. What this all means is that, according to the card manufacturers, you could fill a memory card every day for a couple decades without having any problems.

During my research I found a site/blog that had a sketch for using an Arduino to measure each two seconds and then to write it into a sd card. wouldn't that memory be bad in a matter of a few days?

What I think one could pointo to is to the cost of the memory. Like I would like to preserv as long as I can the Arduino board memory and to spend into an external simple and cheap device. I saw there were external EEPROMs too. I'd like to see if with sd card flows as it comes integrated with ethernet shield which is already in the hardware.

I expect this configuration to change each time that the user changes it. These parameters are: to set ON or OFF the automatic control of the lights, of the plump, and the fotoperiod either 18/16 hours or 12/12 hours of light. So they all three are basically flags of "0" or "1".

The frecuency in which the user changes this configuration depends on the light needs of the cultive and the preferences of the cultiver if he wants it manual or automatic. The light needs for the plant I'm designing this change once during its life span which is about 6 months.

I like what JohnRob said about using only one byte and splitting it into bits because I only need 3 for now. I'm not sure of how to do it tho.

No. Arduino EEPROM lasts 100,000+ writes before it is at risk. No reason to suppose that SD is the same. If your config changes every six months, don't worry about it unless you do something silly - you could kill an EEPROM cell very quickly if you write on it continuously due to a bug.

Another alternative is a FRAM. Far more cycles before it dies.

Is this for marijuana?

The original idea came out from a cannabis forum but I think it can be used in any cultive that requires a variable setting for its lights, like a flowers cultive.