Writing string on EEPROM without using function

Hey Everyone,
In my project, I ask a user to provide address and value he wants to write on the eeprom.

I take that string and convert it into character to separate the address and value. But the problem is if the value is two or more digits, say 15, then I get two characters. I can’t put two characters/bytes in one address. So what should I do with my data so that I concatinate the value and store it in one address.

Moreover, if I store only one digit say ‘1’ then on read I get its ASCII value i.e 49 but not 1. how to solve this problem as well. Thanks.

#include <EEPROM.h>
String str;
char buf[50];
char t;
char add;
char value1; char value2;
byte MSB; byte LSB;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
while (Serial.available() > 0) {
str = Serial.readStringUntil(‘x’);
Serial.println(str); //prints what the user inputs
str.toCharArray(buf,20); //converting String into Char and putting in “buf” variable

add = buf[6];
value1 = buf[8];
value2 = buf[9];
EEPROM.write(add,value1); // here only one digit is store i.e 1 and ACII value 49. not 15

}
}

2 bytes needs 2 addresses needs 2 writes.

Make sure you write things to EEPROM carefully, you only have 100,000 write cycles, then poof! :wink:
An uncontrolled write to EEPROM can happen in seconds!

.

When you use ASCII encoding of the digits '0' to '9', every digit occupies a byte. When you convert the value to binary, a byte can hold numbers up to 255. This applies to both add and value.

To convert value1 and value2 to binary, use something like this:

value = (value2-'0')*10 + (value1-'0');

K_Chohan:
I take that string

It's a String, not a string(see String() - Arduino Reference and string - Arduino Reference).

If the input is always going to be numbers then you should convert it to numbers instead of storing Strings. You can use toInt() if you insist on using String but I'd use a char array(string) and atoi() instead.

If the value will fit in one byte(0 to 255 or -128 to 127) then you can store it in a single address, otherwise it will take multiple addresses, there's no way around that.

If you want to store multiple bytes then EEPROM.put() is useful. It will work with pretty much any type or object and you can use EEPROM.get() to read the data.