Arduino nano twice read from EEPROM gives crash

Arduino IDE 1.8.10
Arduino nano 328p 16mhz 5v

Try to read data from EEPROM twice and gives crash

I tried to read twice from eeprom arduino nano

  1. Load sketch in arduino nano
  2. Write any string with command WRITE_SCALES=12345678
  3. Try to input to serial READ_SCALES
    you will see:

START
READ_SCALES
command=11=READ_SCALES
12345678

  1. Try again input to serial READ_SCALES
    you will see
    R
    and thats all

Why second read from EEPROM gives crash?

#include <SoftwareSerial.h>
#include <EEPROM.h>
//==============================================================================================
String command;
int serial_trig=0;
unsigned long serial_millis=0;
//==============================================================================================
void write_string_EEPROM(String str) 
{
    int lng=str.length();
    for(int i = 0; i < lng; i++) {EEPROM.put(i, (byte)str[i]); delay(10); }
    if (lng<(EEPROM.length()-1)) {EEPROM.put(lng, 255); delay(10);}
    
}
//==============================================================================================
String read_string_EEPROM() 
{
    String str_read;
    for(int i = 0; i < 255; i++) 
    {
      byte c;
      //c=EEPROM.read(i);
      c=eeprom_read_byte(i);
      if(c==255) {break;}
      
      Serial.print((char)c);
      
      delay(10);
    }
    Serial.println();
}
//==============================================================================================
void setup() {
  Serial.begin(9600);
  Serial.println("START");
}
//==============================================================================================
void loop()
{

  if (serial_trig==0)command=""; 
  if (millis()-serial_millis>500)serial_trig=0;
  
  if (Serial.available() > 0) {
    serial_trig=1;
    byte c=Serial.read();
    command = command+(char)c;//считываем как строку  
    Serial.print((char)c);
    serial_millis=millis();
  }
    
   if (command.length()>0 && serial_trig==0)
   {
    Serial.println();
    Serial.println("command="+String(command.length())+"="+command);
    if (command.substring(0,11)=="READ_SCALES")
    {
      read_string_EEPROM();
    }else if (command.substring(0,13)=="WRITE_SCALES=")
    {
      write_string_EEPROM(command.substring(13,command.length()));
      Serial.println("SCALE_SAVED"); 
    }else 
    {
      Serial.println ("UNKNOWN "+command);
    }
   }
}

It is nothing to do with the EEPROM read, it is to do with overflowing the memory reserved for a string.

Appears to be caused by the lack of a return statement in the read_string_EEPROM function.

Wow, is it.. Thanks)) my eyes cant see that

Note that '255' is an 'int' constant so this will write 2 bytes. If you want to write one byte, cast it or convert it to 'byte', 'char', 'unsigned char', 'int8_t', or 'uint8_t':
EEPROM.put(lng, (byte)255);
or
EEPROM.put(lng, byte(255));

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.