Categories
Mastering Development

Storing the last value of a file from SD card using arduino

I am making a device that moves back and fourth and needs to store its last position so that upon power up, the last stored value can be grabbed from the last line of the file on an SD card, and it can resume operation. This file will then be destroyed and re-written. For this particular application homing and other methods can not be used because it must start in the spot it last was. Due to position tracking via encoder, there is no positional memory otherwise.The file is setup to be a single data column seperated by commas.

Currently I am successfully writing to the SD card as position changes, and reading the entire file to be printed on the Serial monitor. However, I really only need the last value. The length of the file will always be different do to system operation.

I have read a lot of different solutions but none of them seem to work for my application.

I can read the entire file using:

void read_file() {
  // open the file for reading:
  myFile = SD.open("test8.txt");
  if (myFile) {
    Serial.println("test8.txt:");
    // read from the file until there's nothing else in it:
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      String a = "";
      for (int i = 0; i < 9; ++i)
      {
        int j;
        char temp = myFile.read();
        if (temp != ',' && temp != '\r')
        { //a=temp;
          a += temp;
        }
        else if (temp == ',' || temp == '\r') {
          j = a.toInt();
          // Serial.println(a);
          Serial.println(j);
          break;
        }
      }
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test8.txt");
  }
}

This gives me a stream of the values separated by 0 like this:

20050
0
20071
0
20092
0
20113
0
20133
0

Ideally I just need 20133 to be grabbed and stored as an int.

I have also tried:

void read_file_3() {
  // open the file for reading:
  myFile = SD.open("test8.txt");
  if (myFile) {
    Serial.println("test8.txt:");
    // read from the file until there's nothing else in it:
    Serial.println(myFile.seek(myFile.size()));
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

This only returns "1", which does not make any sense to me.

Update:
I have found a sketch that does what I want, however it is very slow due to the use of string class. Per post #6 here: https://forum.arduino.cc/index.php?topic=379209.0

This does grab the last stored value, however it takes quite awhile as the file gets bigger, and may blow up memory.

How could this be done without the string class?

void read_file() {
  // open the file for reading:
  myFile = SD.open("test8.txt");
  if (myFile) {
    while (myFile.available())
    {
      String  line_str = myFile.readStringUntil(',');  // string lavue reading from the stream - from , to , (coma to comma)
      int line = line_str.toInt();
      if (line != 0) // checking for the last NON-Zero value
      {
        line2 = line; // this really does the trick
      }
//      Serial.print(line2);
//      delay(100);
    }
    Serial.print("Last line = ");
    Serial.print(line2);
    // close the file:
    myFile.close();
    //    SD.remove("test3.txt");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

Any help would be greatly appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *