Hello everyone i would like to write numbers from 0 to 255 and read to EEPROM with serial monitor. I tried this code but it didn't work properly. I will be glad if you help me to fix it.
#include <EEPROM.h>
int address;
int dataToWrite;
void setup() {
Serial.begin(9600);
}
void loop() {
String buffer=" ";
buffer=Serial.readString();
if(buffer.startsWith("read")){
address=buffer.substring(buffer.indexOf(' ')+1).toInt();
Serial.print("Data from the Address: ");
Serial.print(address);
Serial.println();
Serial.println(EEPROM.read(address));
}
else if(buffer.startsWith("write")){
address=buffer.substring(6,7).toInt();
dataToWrite=buffer.substring(8).toInt();
Serial.print("Written: ");
Serial.print(dataToWrite);
Serial.println();
Serial.print("to the Address: ");
Serial.print(address);
Serial.println();
Serial.println(EEPROM.read(address));
EEPROM.write(address,dataToWrite);
}
}
Please explain what you expect to happen, and what happens instead.
For receiving data from the serial port, the "Serial Input Basics" tutorial on the forum is highly recommended.
Use of Strings is not recommended, as they tend to cause memory problems and program crashes.
hammy
February 5, 2022, 4:56pm
3
Be aware that the EEPROM has a limited number of read write cycles so you can easily exceed this number if not careful .
There are example for the EEPROM library within the IDE and serial too . Plus lots of the tutorials on here .
I’d suggest you tackle the serial bit first .
I would print to the serial monitor asking for your number , read it into a variable then save to EEPROMas a start .to avoid all that string manipulation
But what are trying to do would be helpful
gcjr
February 5, 2022, 10:03pm
4
here's function i used to handle commands with values thru the serial interface. you can extend it to add commands to read/write from the EEPROM
// pcRead - debugging using serial monitor
const char version [] = "PcRead 201114a";
int debug = 0;
// ---------------------------------------------------------
// toggle output bit
int
readString (
char *s,
int maxChar )
{
int n = 0;
Serial.print ("> ");
do {
if (Serial.available()) {
int c = Serial.read ();
if ('\n' == c)
break;
s [n++] = c;
if (maxChar == n)
break;
}
} while (true);
return n;
}
// -----------------------------------------------------------------------------
// process single character commands from the PC
#define MAX_CHAR 10
char s [MAX_CHAR] = {};
int analogPin = 0;
void
pcRead (void)
{
static int val = 0;
if (Serial.available()) {
int c = Serial.read ();
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
val = c - '0' + (10 * val);
break;
case 'A':
analogPin = val;
Serial.print ("analogPin = ");
Serial.println (val);
val = 0;
break;
case 'D':
debug ^= 1;
break;
case 'I':
pinMode (val, INPUT);
Serial.print ("pinMode ");
Serial.print (val);
Serial.println (" INPUT");
val = 0;
break;
case 'O':
pinMode (val, OUTPUT);
Serial.print ("pinMode ");
Serial.print (val);
Serial.println (" OUTPUT");
val = 0;
break;
case 'P':
pinMode (val, INPUT_PULLUP);
Serial.print ("pinMode ");
Serial.print (val);
Serial.println (" INPUT_PULLUP");
val = 0;
break;
case 'a':
Serial.print ("analogRead: ");
Serial.println (analogRead (val));
val = 0;
break;
case 'c':
digitalWrite (val, LOW);
Serial.print ("digitalWrite: LOW ");
Serial.println (val);
val = 0;
break;
case 'p':
#if !defined(ARDUINO_ARCH_ESP32)
analogWrite (analogPin, val);
Serial.print ("analogWrite: pin ");
Serial.print (analogPin);
Serial.print (", ");
Serial.println (val);
val = 0;
#endif
break;
case 'r':
Serial.print ("digitalRead: pin ");
Serial.print (val);
Serial.print (", ");
Serial.println (digitalRead (val));
val = 0;
break;
case 's':
digitalWrite (val, HIGH);
Serial.print ("digitalWrite: HIGH ");
Serial.println (val);
val = 0;
break;
case 't':
Serial.print ("pinToggle ");
Serial.println (val);
digitalWrite (val, ! digitalRead (val));
val = 0;
break;
case 'v':
Serial.print ("\nversion: ");
Serial.println (version);
break;
case '\n': // ignore
break;
case '"':
while ('\n' != Serial.read ()) // discard linefeed
;
readString (s, MAX_CHAR-1);
Serial.println (s);
break;
case '?':
Serial.println ("\npcRead:\n");
Serial.println (" [0-9] append to #");
Serial.println (" A # - set analog pin #");
Serial.println (" D # - set debug to #");
Serial.println (" I # - set pin # to INPUT");
Serial.println (" O # - set pin # to OUTPUT");
Serial.println (" P # - set pin # to INPUT_PULLUP");
Serial.println (" a # - analogRead (pin #)");
Serial.println (" c # - digitalWrite (pin #, LOW)");
Serial.println (" p # -- analogWrite (analogPin, #)");
Serial.println (" r # - digitalRead (pin #)");
Serial.println (" s - digitalWrite (pin #, HIGH)");
Serial.println (" t -- toggle pin # output");
Serial.println (" v - print version");
Serial.println (" \" - read string");
Serial.println (" ? - list of commands");
break;
default:
Serial.print ("unknown char ");
Serial.println (c,HEX);
break;
}
}
}
// -----------------------------------------------------------------------------
void
loop (void)
{
pcRead ();
}
// -----------------------------------------------------------------------------
void
setup (void)
{
Serial.begin(115200);
Serial.println (version);
#if defined(ARDUINO_ARCH_ESP32)
Serial.println ("esp32");
#endif
}
What, exactly, do you mean by "didn't work properly"? It's a lot easier to spot an error when you know what you are looking for.
I tried different code but still can't write all EEPROM memories. It only writes to 0-99 memories. I want to write all 1024 memories.
Here is my new code:
#include <Arduino.h>
#include <EEPROM.h>
int addr,val;
byte value;
String readString = "";
void setup() {
Serial.begin(9600);
Serial.println("Digite read + address or write + address + value");
}
void loop() {
while (Serial.available()){
readString = Serial.readString();
}
if (readString.startsWith("read")){
addr=readString.substring(5).toInt();
Serial.print("your adress is: ");
Serial.println(addr);
Serial.println();
value = EEPROM.read(addr);
Serial.print("EEPROM.read: ");
Serial.println(value);
}
if (readString.startsWith("write")){
addr= readString.substring(6).toInt();
val= readString.substring(8).toInt();
Serial.print("your adress is: ");
Serial.println(addr);
Serial.print("new value is: ");
Serial.println(val);
EEPROM.write(addr, val);
}
readString="";
}
It looks like the address starts at character 6 and the 'value' starts at character 8. Doesn't that mean that if the 'address' is >99 the 'value' will start in the middle of the 'address'?
I think you need a better way to locate the 'value' part of the string. Perhaps looking for the last ' ' in the string and starting there.
What do your serial prints tell you? Are the addresses that you received correct? Are the values that you want to write correctly received?
Think about what happens if you send address 100 or higher. addr will probably be correct but val will contain part of the address.
Robin's Serial Input Basics - updated might give you some ideas for solid communication.