Hi All,
I try to use following code to control the "read" and "write" of EEPROM.
Basically, I want to be able to type "write 3 10" in the serial monitor and save number 10 into the location 3 of EEPROM and when I type "read 3" it should print out 10 (the number that store in location 3).
My code compiled well but it runs funny...
I am able to store the "read" "location" and "number" into different variables and convert them into string, int, int, respectively.
However, the part with read & writing into EEPROM doesn't go well!!
Somehow the program cannot store the number into correct location and that after I type in "write 10 12" (for example), the next time I type "read 10", the Serial print gives me "reade 10" instead of "read 10". Also, it cannot get the correct number back.
I just cannot see what has gone wrong with my code. I imaging its some problem with the start/end of the serial read or some problem with the leftover serial input.
Thanks in advance for any suggestions!
#include <EEPROM.h>
char commend[20];
char rw[20];
char location[20];
char number[20];
int l;
int n;
String RW;
int value;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0)
{
Serial.readBytesUntil(' ',rw, 20);
Serial.readBytesUntil(' ',location, 20);
Serial.readBytesUntil(' ',number, 20);
Serial.println(rw);
Serial.println(location);
Serial.println(number);
int l = atoi(location);
int n = atoi(number);
RW = String(rw);
Serial.println(l);
Serial.println(n);
}
if (RW == "read"){
value = EEPROM.read(l);
Serial.println(value);
}
if (RW == "write"){
EEPROM.write(l,n);
}