Serial to EEPROM to Oled problem

Hello, I am trying to save a String from Serial to EEPROM and print on the Oled screen, if I move from the conditional "IF" the print command immediately gives an error and I have to reset my Seeeduino XIAO Samd21.

#include <FlashAsEEPROM_SAMD.h>  // work whit flash eeprom

#include <Wire.h>                   //display oled
#include <Adafruit_GFX.h>           //display oled
#include <Adafruit_SSD1306.h>       //display oled

#include <Fonts/FreeSansBoldOblique9pt7b.h>  //font to oled

#define SCREEN_WIDTH 128 // OLED display width, in pixels    //display oled
#define SCREEN_HEIGHT 32 // OLED display height, in pixels   //display oled

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);    //display oled

String myString;  // To save in eeprom
String a;         // To read and print in eeprom

int button_del = 7;   // To delete eeprom



void setup() {

  pinMode(button_del, INPUT_PULLUP);

  
  Serial.begin (9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));                               //display oled
    for(;;);
  }
  display.clearDisplay();    //clear display oled
}

void loop() {
 
  if (Serial.available() || digitalRead(button_del) == LOW){
   
   myString=Serial.readString();
   
   EEPROM.put (0, myString);
   delay (4);
  
   a=EEPROM.get (0,myString); //If I move this line out of the 
                             //conditional so that it is read from the EEPROM it gives an error.
   Serial.print (a); //Test "OK" only one word, THIS IS BAD since I need the top line outside the IF to load on startup.

         }

   
  if (digitalRead(button_del) == LOW) {
     
                 a="";                       //OK clear the screen, I don't know if it clears 
                 display.clearDisplay();     //the eeprom and I don't know if it is actually recorded.
                 EEPROM.update (0, 0);          
       
         } 
  
   
  display.clearDisplay();
  display.setFont(&FreeSansBoldOblique9pt7b);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  a.toUpperCase();
  display.setCursor(0, 15);
  display.print(a); 
  display.display();

  }

The "String" object doesn't work like that. The object (the part you are saving) contains a pointer to the data but not the data itself. Try this to write:

    byte length = myString.length();
    EEPROM.update(0, length);
    for (int i = 0; i < length; i++)
      EEPROM.update(1 + i, myString[i]);

and use this to read:

    length = EEPROM.read(0);
    myString = "";
    for (int i = 0; i < length; i++)
      myString += EEPROM.read(1 + i);

I structured it this way, as I could, but I am getting numbers instead of letters.

#include <FlashAsEEPROM_SAMD.h>

String myString;
String myString_read;

void setup() {
  
 Serial.begin(9600);
       
}

void loop() {
  if (Serial.available()){
 myString=Serial.read();


byte length = myString.length();
    EEPROM.update(0, length);
    for (int i = 0; i < length; i++)
      EEPROM.update(1 + i, myString[i]);


length = EEPROM.read(0);
    myString_read= "";
    for (int i = 0; i < length; i++)
      myString_read+= EEPROM.read(1 + i);

 Serial.println (myString_read);
 
}

}

Sorry, I forgot that String will treat a 'byte' as a number and not as a single character, Try changing this line:
myString_read += (char)EEPROM.read(1 + i);

I still have problems, the code works but it returns ASCII instead of characters.

#include <FlashAsEEPROM_SAMD.h>

String myString;
String myString_read;
char lectura;
void setup() {
  
 Serial.begin(9600);
       
}

void loop() {
  if (Serial.available()){
 myString=Serial.read();


byte length = myString.length();
    EEPROM.update(0, length);
    for (int i = 0; i < length; i++)
      EEPROM.update(1 + i, myString[i]);


length = EEPROM.read(0);
    myString_read= "";
    for (int i = 0; i < length; i++)
      myString_read+= (char)EEPROM.read(1 + i);
     

 Serial.println (myString_read);

 
}

}

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