Strange restarts when passing an array between functions using pointers

This is a very peculiar one and I’m having trouble fully isolating the cause of the issue. It could be many things at this point, but after hours of trial and error I thing I’ve narrowed it down, although I don’t really know why this is an issue.

Here’s what I’ve got… Adafruit CC3000 REST API passing params from the URL to custom functions to control RGB LEDs.

here’s 2 example calls…

Set the LED to yellow: http://###.###.###.###/setRGB?params=255,255,0
Fade the LED to Cyan over 5secs http://###.###.###.###/fadeRGB?params=0,255,255,5000

The adafruit lib works like this… To access a function defined in your sketch, you have to declare it first, and then call it from with a REST call. Note that all functions needs to take a String as the unique argument (for parameters to be passed to the function) and return an integer.

So I wrote a universal function to split a string of comma separated integer values into an array of long ints to use from within my different functions. Here it is:

void splitCommand(String command, long *vals, byte len) {
  int i = 0;
  int j = 0;
  int inChar;
  String inString;
  
  
  do {
    inChar = command[i];
    if (isDigit(inChar)) {
      inString += (char)inChar;
    } else if (inChar == 44) {
      vals[j] = inString.toInt();
      inString = "";
      j++;
    }
    i++;
  } while(inChar > 0 && i < 255 && j < len);
  if(j < len) vals[j] = inString.toInt();
}

So in my function for setRGB I use it like this…

int setRGB(String command) {
  
  long vals[3];
  
  splitCommand(command, vals, 3);
   
  red = vals[0];
  green = vals[1];
  blue = vals[2];

//do some stuff...

for now splitCommand() is used in 2 functions setRGB() and fadeRGB() but I will be writing lots more functions. I have found if I only declare one of these functions in the REST api everything works. If I declare both functions as REST calls I get strange results. Mostly random resets but also sometimes the functions just arn’t available via the REST api. If I just pick one of the 2 RGB functions and only declare that everything works great. If I add another function it also works fine as long as it doesn’t make use of the splitCommand() function.

Does anyone have any ideas, I’m totally open to a differnt solution for splitCommand() I just need to turn the string of params into ints that can be accessed by different functions.

I would suggest that you not use the String class and use character arrays instead. Parse your command with strtok() and atoi().

Have you monitored your RAM usage? Running out of RAM is often a cause of strange and mysterious behaviour. The String class is notorious for having space-leaks and should be under suspicion immediately too.

If you are using a recent version of the IDE, the free() bug is fixed which caused Strings memory leaks.
However it can generate many temporaries, which can fill RAM quickly.

Pass the string using references, anything that is longer than 2 bytes will benefit from a reference ( speed & stack size ).

void splitCommand(String &command, long *vals, byte len)
//…
int setRGB(String &command);

In the splitCommand function, use reserve to prevent an malloc/free pair every time you add a char to inString:

String inString;
inString.reserve( 10  );

Next there is the clear operation:

inString = "";

Which is another malloc/free and also removes the reserved data. And fixing this to use the same block is so close to using c strings that you might as well just ditch the String class. The only major difference is really the call to atoi() rather than string.toInt()