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
- Load sketch in arduino nano
- Write any string with command WRITE_SCALES=12345678
- Try to input to serial READ_SCALES
you will see:
START
READ_SCALES
command=11=READ_SCALES
12345678
- 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);
}
}
}