memory limit on a string?

Do not know what happened with my computer there, but what i was going to say is i have this peice of code which would go into the eeprom memory and add the character to a localized string. however when i ran this snippet of code the arduino would concatinate roughly 19 characters and then completely wipe the string and start adding the rest of the characters to the beginign of the string.
Here is the code

String messageconstruct(int start, int endd){
  String messagi = "";
  for (int i=start; i<endd; i++){
    Serial.print(i);
    Serial.print(" "+String(char(EEPROM.read(i))));
    messagi = messagi + String(char(EEPROM.read(i)));
    Serial.println(" " + messagi);
  }
  Serial.println("::" + messagi);
  return messagi;
}

which would show up on the serial monitor like …

0 c c
1 m cm
2 d cmd
3 cmd
4 o cmd o
5 n cmd on
6 e cmd one
7 e cmd onee
8 cmd onee
9 c cmd onee c
10 m cmd onee cm
11 d cmd onee cmd
12 cmd onee cmd
13 t cmd onee cmd t
14 w cmd onee cmd tw
15 o cmd onee cmd two
16 o cmd onee cmd twoo
17 cmd onee cmd twoo
18 c cmd onee cmd twoo c
19 m cmd onee cmd twoo cm
20 d cmd onee cmd twoo cmd
21 cmd onee cmd twoo cmd
22 t
23 h h
24 r hr
25 e hre
26 hre

Serial.print(" "+String(char(EEPROM.read(i))));

Serial.print( ) on the arduino will only print one thing at a time. It won't stick several things together, like Java does, which seems to be what you are trying to do.

Fix that problem first, and then see what the next problem is.

And I don't know what you're doing but drop the String class. See Gammons Trap #23.

Also, making very long strings on a micro isn't a good idea nor useful...

michinyon: Serial.print( ) on the arduino will only print one thing at a time. It won't stick several things together, like Java does, which seems to be what you are trying to do.

Fix that problem first, and then see what the next problem is.

The serial print at that point was just used for debugging. The purpose of this function was to retrieve characters from the EEPROM memory and create a string that can be compared with serial monitor input. Is there a better way to concatenate characters into a string?

Septillion is right: the String class is a resource hog and should be avoided. Also, in your code you have:

String messageconstruct(int start, int endd){
  String messagi = "";
  // more code...
  return messagi;
}

When you define a variable within a function block, like you did with messagi, that variable goes out of scope when you leave the function. So the function cannot return the String.

What you might do is figure out the maximum size of your expected message, say 50 characters, and then try something like:

void loop() {
   static char messagi[51];     // Need a byte for the null terminator
   // some code...
   MessageConstruct(messagi, start, end);
   // whatever you need to do...
}

void MessageConstruct(char *msg, int start, int end) {
  for (int i=start; i<end; i++){
     // whatever
     msg[i] = (char) EEPROM.read(i);
  }
  msg[i] = '\0';    // Now it's a string
}

I’m not at my machine, so this is just a rough starting point.

econjack:
Septillion is right: the String class is a resource hog and should be avoided. Also, in your code you have:

String messageconstruct(int start, int endd){

String messagi = “”;
  // more code…
  return messagi;
}




When you define a variable within a function block, like you did with *messagi*, that variable goes out of scope when you leave the function. So the function cannot return the String.

What you might do is figure out the maximum size of your expected message, say 50 characters, and then try something like:



void loop() {
  static char messagi[51];    // Need a byte for the null terminator
  // some code…
  MessageConstruct(messagi, start, end);
  // whatever you need to do…
}

void MessageConstruct(char *msg, int start, int end) {
  for (int i=start; i<end; i++){
    // whatever
    msg[i] = (char) EEPROM.read(i);
  }
  msg[i] = ‘\0’;    // Now it’s a string
}




I'm not at my machine, so this is just a rough starting point.

Thank you for the response, this makes sense. The only error i am having is with "msg = ‘\0’" which produces a compile time error “error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]” and with a function like this do i need to add in a return statement?

Its basically not happy with you using the variable i. That variable is your loop counter and as we're outside that loop on that line, i is out of scope.

Just replace with:

msg[end] = 0;

My bad: I didn’t notice that i was defined with statement block scope. Try:

void MessageConstruct(char *msg, int start, int end) {

  int i;         // Move definition of i here

  for (i = start; i < end; i++){
     // whatever
     msg[i] = (char) EEPROM.read(i);
  }
  msg[i] = '\0';    // Now it's a string
}

The only error i am having is with "msg = '\0'"

This is supposed to be '\0' not '|0'