Read State from SD File

Hello Everyone,

I am trying to get the following coding working so I can adapt the following simple code into something much bigger. I am trying to get the Arduino to save data to a SD card then read it again to set the states if the Arduino should reboot (power loss). I am wanting to use this method instead of the Arduino’s EEPROM as I have been reading you can only read and write to it so many times.

Ideally I want one file to control a few states (LEDs) but I am just trying to get the one working first (examples would be greatly appreciated). The following code can currently save values to a txt file on the SD card when a new state is selected via a serial command but the problem is reading the text from the txt file on the SD card to then do an action if it contains something.

Also, if anyone knows of any better code to use instead of fully deleting the file each time, that would also be great!

Thanks in advanced,

James

#include <SPI.h>
#include <SD.h>

File myFile;

int LED = 13;
int LED_Switch = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  
  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  myFile = SD.open("led.txt");
  if (myFile) {
    Serial.println("led.txt:");
    while (myFile.available()) {
      Serial.write(myFile.read());
      if (myFile == '1') {
        LED_Switch = 1;
      }
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

}

void loop() {
  String input = "";
  while (Serial.available() > 0){
    input += (char) Serial.read();
    delay(5);
  }

  if (input == "on"){
    LED_Switch = 1;
    Serial.println("Removing LED.txt...");
    SD.remove("led.txt");
    myFile = SD.open("led.txt", FILE_WRITE);
    if (myFile) {
      Serial.print("Writing to led.txt...");
      myFile.println("1");
      // close the file:
      myFile.close();
      Serial.println("done.");
      } else {
        // if the file didn't open, print an error:
        Serial.println("error opening led.txt");
      }
  }
  
  if (input == "off"){
    LED_Switch = 0;
    Serial.println("Removing LED.txt...");
    SD.remove("led.txt");
    myFile = SD.open("led.txt", FILE_WRITE);
    if (myFile) {
      Serial.print("Writing to led.txt...");
      myFile.println("0");
      // close the file:
      myFile.close();
      Serial.println("done.");
      } else {
        // if the file didn't open, print an error:
        Serial.println("error opening led.txt");
      }
  }

  if (LED_Switch == 1) {
    digitalWrite(LED, HIGH);
  } else {
    digitalWrite(LED, LOW);
  }
}

How many times do you expect the Arduino to reboot? According to:

you can expect the Arduino to perform as follows:

The EEPROM memory has a specified life of 100,000 write/erase cycles

If you have a power loss once a day, it still should last you almost 275 years. Even if it dies every hour, it still will last over 11 years.

I plan to use this in a project where there may be up to 20 different states... Each one could be changed every time I am wanting to use the project. So it will be reading, writing and deleting files every time I set a setting on the main project but as a fail safe I want the arduino to set the setting back from the SD card if power should fail. SD Cards costs next to nothing to replace, rather replace that instead of the whole arduino board in a event of a read write failure.

Thanks in advanced,

James