• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

eeprom a lost character

Q&A Forum › eeprom a lost character
0 Vote Up Vote Down
Jean Pierre Daviau asked 5 years ago

Hi, 
 
Where is does my H went   in the readChar  function?
 

Start your code here
#include <EEPROM.h>
//#include <Arduino.h> #define EEPROM_SIZE 40
void writeChar(uint8_t ad, const char *ch)
{
printf("%d\n", ad);
printf("%d: %c\n", ad++, *ch);
EEPROM.write(ad, *ch);
EEPROM.commit();
while (*ch++ != '\0') {
EEPROM.write(ad, *ch);
EEPROM.commit();
printf("%d: %c\n", ad++, *ch);
}
}
void readChar(uint8_t ad)
{
uint8_t ch = '\0'; ch = EEPROM.read(ad++);
Serial.printf("%c", ch);
while (ch != '\0') {
Serial.printf("%c", ch);
ch = EEPROM.read(ad++);//ch address is always the same
}
}
void setup() {
Serial.begin (115200);
EEPROM.begin(EEPROM_SIZE);
const char *hel = "Hello once in a while.";
writeChar(7, hel);
Serial.println("\n----------------------------");
readChar(7); Serial.println("\n----------------");
Serial.println(EEPROM.read(0));
Serial.println(EEPROM.read(1));
Serial.println(EEPROM.read(2));
Serial.println(EEPROM.read(3));
Serial.println(EEPROM.read(4));
Serial.println("\n----------------");
Serial.println((char)EEPROM.read(7));
Serial.println((char)EEPROM.read(8)); //EEPROM.write(0,'H');//Reset memory value
//EEPROM.write(1,'e');
//EEPROM.write(2,'l');
//EEPROM.write(3,'l');
//EEPROM.write(4,'o');
//EEPROM.commit();
}
void loop() { }

 

Start your code hereentry 0x400806ac
7
7: H
8: e
9: l
10: l
11: o
12:
13: o
14: n
15: c
16: e
17:
18: i
19: n
20:
21: a
22:
23: w
24: h
25: i
26: l
27: e
28: .
29: --------- readChar ------------------- ⸮⸮ello once in a while. ---------------- 72 101 108 108 111 ---------------- ⸮ e  

Regards
 
JPDaviua

Question Tags: eeprom a lost character, ESP32
2 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.
I don’t know why it doesn’t work. But replacing your readChar function with this, solves the problem:

void readChar(uint8_t ad) {
  uint8_t ch = '\0'; 
  ch = EEPROM.read(ad);
  Serial.print((char)ch);
  while (ch != '\0') {
    ad++;
    ch = EEPROM.read(ad);//ch address is always the same
    Serial.print((char)ch);
  }
}

Regards,
Sara

0 Vote Up Vote Down
Jean Pierre Daviau answered 5 years ago

Thanks.

#include

#define EEPROM_SIZE 55

void writeChar(int16_t ad, const char *ch)
{
EEPROM.write(ad++, *ch);
EEPROM.commit();
printf("%d: %c\n", ad, *ch);
while (*ch++ != '\0') {
printf("%d: %c\n", ad, *ch);
EEPROM.write(ad++, *ch);
EEPROM.commit();

}
}

void readChar(int16_t ad)
{
uint8_t ch = '\0';

ch = EEPROM.read(ad++);
while (ch != '\0') {
Serial.printf("%c", ch);
ch = EEPROM.read(ad++);//ch address is always the same
}
}

void setup() {
Serial.begin (115200);
EEPROM.begin(EEPROM_SIZE);
const char *hel = "Hello once in a while.";
//int16_t lnth = strlen(hel);
uint8_t from = 6;
writeChar(from, hel);
Serial.println("\n----------------------------");
readChar(from);

Serial.println("\n----------------");
Serial.println((char)EEPROM.read(6));
Serial.println((char)EEPROM.read(7));
Serial.println((char)EEPROM.read(8));
}
void loop() {

}

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.