Go Down

Topic: string in eeprom (Read 1 time) previous topic - next topic

gretka

hi
I dont speak english very well, but i hope that i'll explain my problem understandable.
i want to write some words in eeprom but my idea doasn't work.
At beginning i tried write one letter
My code:
...
void eprom(){
  EEPROM.write(10, 0);
  delay(5);
int slowo;
  slowo = EEPROM.read(0);
  Serial.print(slowo);
...
But it doasnt metter wich nuber i write serial port monitor (i'm not sure this name in english) always shows 105 or letter "i" when slowo was a char.
than I wanted write all word and here is my code:
void eprom(){
EEPROM.write(109, 10);//mleko
  delay(5);
  EEPROM.write(108, 11);
  delay(5);
  EEPROM.write(101, 12);
  delay(5);
  EEPROM.write(107, 13);
  delay(5);
  EEPROM.write(111, 14);
  delay(5);
char slowo3[16];
for (int i=10; i<15; i++){
    slowo3=(int)EEPROM.read(10+i);
  }*/
  int slowo;
  slowo = EEPROM.read(0);
  Serial.print(slowo3);
}
and in this case serial port monitor shows  just a trash

Do you have any idea what am I doing bad ?


johnwasser

http://arduino.cc/en/Reference/EEPROMWrite
"EEPROM.write(address, value)"

Looks like you have 'address' and 'value' switched.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

gretka

thanks
i changed this mistake
but i have still a problem
nowmy code looks like here:
#include <EEPROM.h>
void setup() {
  Serial.begin(9600);
  eprom();
}
void loop(){
}
void eprom(){
  EEPROM.write(0, 109);//maka
  delay(5);
  EEPROM.write(1, 97);
  delay(5);
  EEPROM.write(2, 107);
  delay(5);
  EEPROM.write(3, 97);
  delay(5);
  EEPROM.write(4, 10);
  delay(5);
  EEPROM.write(5, 99);//cukier
  delay(5);
  EEPROM.write(6, 117);
  delay(5);
  EEPROM.write(7, 107);
  delay(5);
  EEPROM.write(8, 105);
  delay(5);
  EEPROM.write(9, 101);
  delay(5);
  EEPROM.write(10, 114);
  delay(5);
  EEPROM.write(11, 10);
  delay(5);
  EEPROM.write(12, 109);//mleko
  delay(5);
  EEPROM.write(13, 108);
  delay(5);
  EEPROM.write(14, 101);
  delay(5);
  EEPROM.write(15, 107);
  delay(5);
  EEPROM.write(16, 111);
  delay(5);
  EEPROM.write(17, 10);
  delay(5);
 
  char slowo1[5];
  char slowo2[7];
  char slowo3[6];
  for (int i=0; i<5; i++){
    slowo1=EEPROM.read(0+i);
    delay(5);
  }
for ( int i=0; i<7; i++){
    slowo2=EEPROM.read(5+i);
    delay(5);
  }
  for (int i=0; i<6; i++){
    slowo3=EEPROM.read(12+i);
    delay(5);
  }
// slowo1[16]='\0';
  //slowo2[16]='\0';
// slowo3[16]='\0';
//char slowo;
//slowo = EEPROM.read(1);
Serial.print(slowo1);
Serial.print(slowo2);
Serial.print(slowo3);
Serial.print("greta");
   
}
and i wanted to see on serial port monitor words:
maka
cukier
mleko
greta
------------
in this order,but i see:
maka
mleko
cukier
hcukier
hmleko
cukier
hgreta
-----------
Do you have any idea ?

robtillaart

[tip : spent some time on the tutorials about usiong arrays]

It is difficult to write character arrays of unequal length to eeprom. See code below (not tested). If you make a reservation in the eeprom for each string e.g. you use 12 bytes for each name it is far easier to extract the names from eeprom as you can calculate the start address.

(code not tested)
Code: [Select]

#include <EEPROM.h>
void setup()
{
  Serial.begin(9600);
  int addr = put_in_eeprom(0, "maka");
  addr = put_in_eeprom(addr, "cukier");
  addr = put_in_eeprom(addr, "mleko");
  // etc

  char buf[24]; // big enough I hope
  addr = get_from_eeprom(0, buf);
  Serial.println(buf);
  addr = get_from_eeprom(addr, buf);
  Serial.println(buf);
 
}

void loop()
{
}

void put_in_eeprom(int addr, char *p)
{
  int i = 0;
  while (p[i] != '\0');
  {
    EEPROM.write(addr+i, p[i]);
    delay(5);
    i++;
  }
  EEPROM.write(addr+i, p[i]);
  delay(5);
  return addr + i + 1; // next free place
}
 
int get_from_eeprom(int addr, char *p)
{
  int i = 0;
  char c = EEPROM.read(addr);
  while (c != '\0');
  {
    p[i++] = c;
    c = EEPROM.read(addr);
  }
   p[i] = '\0';
  return addr + i + 1;
}

// there might be an off by one error in this code :)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

johnwasser

You havet to put a null terminator on each string AND you have to make room for that terminator when you allocate the arrays:
Code: [Select]

  char slowo1[6];
  char slowo2[8];
  char slowo3[7];

  for (int i=0; i<5; i++) {
    slowo1[i]=EEPROM.read(0+i);
  }
  slowo1[5]='\0';

  for (int i=0; i<7; i++) {
    slowo2[i]=EEPROM.read(5+i);
  }
  slowo2[7]='\0';

  for (int i=0; i<6; i++){
    slowo3[i]=EEPROM.read(12+i);
  }
  slowo3[6]='\0';

  Serial.print(slowo1);
  Serial.print(slowo2);
  Serial.print(slowo3);
  Serial.print("greta");
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up