Arduino將程式內的資料記錄到內建儲存資料區(EEPROM)[Arduino Write/Read string to eeprom][#include ]

Arduino將程式內的資料記錄到內建儲存資料區(EEPROM)[Arduino Write/Read string to eeprom][#include ]

Arduino將程式內的資料記錄到內建儲存資料區(EEPROM)[Arduino Write/Read string to eeprom][#include <EEPROM.h>]


資料來源: https://roboticsbackend.com/arduino-write-string-in-eeprom/


01.How we’re going to write a String into EEPROM


02.Write a String into EEPROM: The code


◆Write the String

void writeStringToEEPROM(int addrOffset, const String &strToWrite)
{
  byte len = strToWrite.length();
  EEPROM.write(addrOffset, len);

  for (int i = 0; i < len; i++)
  {
    EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
  }
}


◆Read the String

String readStringFromEEPROM(int addrOffset)
{
  int newStrLen = EEPROM.read(addrOffset);
  char data[newStrLen + 1];

  for (int i = 0; i < newStrLen; i++)
  {
    data[i] = EEPROM.read(addrOffset + 1 + i);
  }
  data[newStrLen] = '\ 0'; // !!! NOTE !!! Remove the space between the slash "/" and "0" (I've added a space because otherwise there is a display bug)

  return String(data);
}


◆Try the code

#include <EEPROM.h>

void writeStringToEEPROM(int addrOffset, const String &strToWrite)
{
  byte len = strToWrite.length();
  EEPROM.write(addrOffset, len);

  for (int i = 0; i < len; i++)
  {
    EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
  }
}

String readStringFromEEPROM(int addrOffset)
{
  int newStrLen = EEPROM.read(addrOffset);
  char data[newStrLen + 1];

  for (int i = 0; i < newStrLen; i++)
  {
    data[i] = EEPROM.read(addrOffset + 1 + i);
  }
  data[newStrLen] = '\ 0'; // !!! NOTE !!! Remove the space between the slash "/" and "0" (I've added a space because otherwise there is a display bug)

  return String(data);
}

void setup() {
  Serial.begin(9600);

  writeStringToEEPROM(0, "Hello Arduino");
  
  String retrievedString = readStringFromEEPROM(0);

  Serial.print("The String we read from EEPROM: ");
  Serial.println(retrievedString);
}

void loop() {}


03.Improvements to write multiple Strings to EEPROM


◆Improved code

#include <EEPROM.h>

int writeStringToEEPROM(int addrOffset, const String &strToWrite)
{
  byte len = strToWrite.length();
  EEPROM.write(addrOffset, len);

  for (int i = 0; i < len; i++)
  {
    EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
  }

  return addrOffset + 1 + len;
}

int readStringFromEEPROM(int addrOffset, String *strToRead)
{
  int newStrLen = EEPROM.read(addrOffset);
  char data[newStrLen + 1];

  for (int i = 0; i < newStrLen; i++)
  {
    data[i] = EEPROM.read(addrOffset + 1 + i);
  }
  data[newStrLen] = '\ 0'; // !!! NOTE !!! Remove the space between the slash "/" and "0" (I've added a space because otherwise there is a display bug)

  *strToRead = String(data);
  return addrOffset + 1 + newStrLen;
}

void setup() {
  Serial.begin(9600);

  int eepromOffset = 0;

  // Writing

  String str1 = "Today's tutorial:";
  String str2 = "Save String to EEPROM.";
  String str3 = "Thanks for reading!";

  int str1AddrOffset = writeStringToEEPROM(eepromOffset, str1);
  int str2AddrOffset = writeStringToEEPROM(str1AddrOffset, str2);
  writeStringToEEPROM(str2AddrOffset, str3);

  // Reading

  String newStr1;
  String newStr2;
  String newStr3;

  int newStr1AddrOffset = readStringFromEEPROM(eepromOffset, &newStr1);
  int newStr2AddrOffset = readStringFromEEPROM(newStr1AddrOffset, &newStr2);
  readStringFromEEPROM(newStr2AddrOffset, &newStr3);
  
  Serial.println(newStr1);
  Serial.println(newStr2);
  Serial.println(newStr3); 
}

void loop() {}


◆Explanations

    As you can see, both read and write functions now return an integer offset.

    The write function is the same, we simply return the current new offset, which is (the offset we got as a parameter + the length of the String + 1 because we also added one byte to store the length).

    So, when you use the writeStringToEEPROM() function, you get a new offset that you can directly use for the next writeStringToEEPROM() call. And you don’t need to compute the offset yourself.

    The read function is slightly different. Instead of returning the String object we got from EEPROM, we return the current new offset. The String is now a pointer you give as a parameter.

    The readStringFromEEPROM() function will simply write the new String value into the String object, and then return the offset (previous offset + length of String + 1 for the byte used to store the length).

    Using this technique has a few advantages, you can:

        Avoid doing any offset computation yourself, and thus you have less risk to mess up your code.
        
        Use the read/write functions without having to know the internal details anymore.
        
        Easily “chain” the functions. The returned offset you get is the offset you use as a parameter in your next function call.


04.Going further with storing Arduino String into EEPROM

    In this tutorial you have seen how to store a String into the EEPROM memory and get it back.

    Here are a few things for you to consider – about the previous code and EEPROM in general:

        String max size: here I have (implicitly) supposed that the String length will be less than 256. If you have a String containing 300 bytes, then you may need to store the length on 2 bytes (max value: 65535) instead of 1 byte (max value: 255). In this case you’d have to modify the write/read functions to handle 2 bytes for the length, and add 2 to the offset instead of 1.

        EEPROM max size: EEPROM is not an infinite storage! Usually it’s pretty small so you have to make sure you know the EEPROM size for your Arduino board. For example, the Arduino Uno EEPROM size is 1024 bytes. For the previous code, you could for example add a length test and return an error if the String length is too big.

        And finally, something worth repeating: there is a finite number of write operations possible for each address of the EEPROM memory. After about 100 000 write operations on a specific address (read operations do not count), you can consider that this EEPROM address is dead. So, make sure you don’t write without any blocking code in the loop() function of your program!

        With those remarks, you can work on the previous code example and try to solve the String max size and EEPROM max size problems. That’s a good way to practice and progress with Arduino!


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *