Last active
September 21, 2023 14:32
-
-
Save mindeon/20a88f1d80d91c1eb1a60e2d1da1ded1 to your computer and use it in GitHub Desktop.
arduino eeprom read/write string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <EEPROM.h> | |
void writeString(int address, String data) | |
{ | |
int stringSize = data.length(); | |
for(int i=0;i<_size;i++) | |
{ | |
EEPROM.write(address+i, data[i]); | |
} | |
EEPROM.write(address + stringSize,'\0'); //Add termination null character | |
} | |
String readString(int address) | |
{ | |
char data[100]; //Max 100 Bytes | |
int len=0; | |
unsigned char k; | |
k = EEPROM.read(address); | |
while(k != '\0' && len < 100) //Read until null character | |
{ | |
k = EEPROM.read(address + len); | |
data[len] = k; | |
len++; | |
} | |
data[len]='\0'; | |
return String(data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In line 4 _size change to stringSize