String ,char array and other vegetables

Hello all,
I am basically fighting with Arduinos here.
I am trying to work with serial data.
What I am trying to do is simply collect 24 bytes into to a String called cmd.
Then I try to process the cmd to get all the data I need. My code worked great until the Arduino started to do all kind of weird things.

Before I show you the code, I want to say that at the first version I worked only with "Strings". After some internet research I found that the String library is not so perfect and with a lack of memory the device starts to get crazy. So I switched almost everything into to char*.

Now this is the code:

char* token[MAX_TOKEN];
char* fun[MAX_FUN];
char* device[MAX_FUN];
char* option[MAX_FUN];
char* timer[MAX_TIMER];


void serialEvent() {
  while (Serial.available())
  {
    char inChar = (char)Serial.read(); 
    cmd.concat(inChar);
    
    if(inChar == '&') // THE SIGN '&' ZERORIZE THE CMD
    {
      cmd = "";
    }
    
    if(cmd.length() > CMD_BYTES) // the cmd has to be CMD_BYTES long!
    {
      cmd = cmd.substring(0,CMD_BYTES);
    }
    
    if (cmd.length() == CMD_BYTES) // only if the cmd is CMD_BYTES long cmd is being proccess!
    {
          strcpy(*token, cmd.substring(1,8).c_str());
         // strcpy(*fun, cmd.substring(8,10).c_str());
          

      /*
         strcpy(*token, cmd.substring(1,8).c_str());
         strcpy(*fun, cmd.substring(8,10).c_str());
         strcpy(*device, cmd.substring(10,12).c_str());
         strcpy(*option, cmd.substring(12,14).c_str());
         strcpy(*timer, cmd.substring(14,24).c_str());
         */
        
        sw = true;
        Serial.println("Command is out of the oven!");
        cmd = "";

    }
    
  }
}

Now this code works only when one strcpy line is not commented, and ever so it shows me things like : "êއ
Ï']ø`5ƒ¿†ï¶,Ö#‰~\Xÿäóÿ½?þ“…bð,ÿu¿²ï¢½íÉm»…oeå"

Now I anderstand that I might be out of SRAM lack.
So what is the best way to work with Arduino and strings?\

**I am working with duemi' and pro mini 3.3 8Mhz

Thank you!

Hello :slight_smile:

I will just suggest to never use the String class, so convert your cmd to a char array. Additionally, you should read about char arrays, because actually you are making arrays of char pointers.

Finally, here is an example of how to parse a serial command, using the very useful function sscanf.

As guix points out, it seems you want arrays of characters, not arrays of character pointers. Therefore, you want

// char* token[MAX_TOKEN];    Don't use this

char token[MAX_TOKEN];     // Use this instead

Second, it seems like the code you've shown us should properly be in the loop() function. However, we cannot be sure until you post all of your code.

All I can suggest here is reading up a C tutorial on using pointers and strings.

I am basically fighting with Arduinos here.

You would have similar problems on any platform. Possibly they hit you sooner on the Arduino because there is less RAM, however processing 24 bytes is well within its capabilities.

I am basically fighting with Arduinos here.

And we are fighting with your snippets. Take them to http://snippets-r-us.com