Arduino stops running a certain function

Hi. I have some code that I am working on. Basically it waits for Serial input and then gets the string you give it. It gives the string to a function called print_nums which parses it. For some reason every time I run the sketch, after 3 or 4 times print_nums no longer does anything. The loop function which gets the input and prints it back to me still echoes what I send it, but print_nums seems to just stop running.

Here is my code:

#include <IRremote.h>
#include <string.h>

IRsend irsend;

void setup() {
	Serial.begin(19200);	// opens serial port, sets data rate to 9600 bps
        Serial.println("starting");
}

unsigned int command[128];
int len;
void print_nums(const char *str)
{
    char *tempstr = strdup(str);
    char *p = NULL;
    const char *delim = ",";
    len=0;
        while ((p = strtok_r(tempstr, delim, &tempstr)) != NULL) {
        int n = atoi(p);
        command[len] = n;
        Serial.println(command[len]);  /* or whatever you want to do with each number */
        len++;}

    if (tempstr != NULL) {
        free(tempstr);
        tempstr = NULL;
    }
}

void loop(){
  char commandC[400];
  int i=0;
  while(Serial.available()==0){continue;}
  while(Serial.available()>0){
    commandC[i] = Serial.read();
    Serial.print(commandC[i]);
    i++;
    delay(10);
  }
  print_nums(commandC);
}

Frogging101:

    char *tempstr = strdup(str);

:
            :
        free(tempstr);

The heap on Arduino is so small, it is easy to go beyond the RAM available with just a little fragmentation. I would recommend letting print_nums() modify commandC[] in place t parse it, rather than making a copy. There is a good thread below about what's up with the heap and stack and how to measure if you are overstepping what's available:

But I think the real problem might be here:

void loop(){

char commandC[400];
  int i=0;
  while(Serial.available()==0){continue;}
  while(Serial.available()>0){
    commandC[i] = Serial.read();
    Serial.print(commandC[i]);
    i++;
    delay(10);
  }

commandC[i] = '\0';  // You forgot to null terminate this string, causing trailing garbage to be sent to print_nums()

print_nums(commandC);
}

You forgot to null terminate this string, causing trailin garbage to be sent to print_nums().
You also can overflow commandC[] causing various forms of havoc.

Clean those items up, and it will likely run.

Thanks, gardner. I'll try what you have said...

About the null-terminating, I will try it but I don't think it's the issue. I assumed it might be an overflow/resource problem. The communication between me (through the serial monitor) to the arduino is this:

Me: 1, 2, 3, 4

Arduino: 1, 2, 3, 4
1
2
3
4


Me: 1, 2, 3, 4

Arduino: 1, 2, 3, 4
1
2
3
4


Me: 1, 2, 3, 4

Arduino: 1, 2, 3, 4


Me: 1, 2, 3, 4

Arduino: 1, 2, 3, 4


As you can see, after a few iterations the arduino continues to echo back to me what I sent it, but stops parsing it and sending me back the parsed numbers.

Frogging101:
I don't think it's the issue.

I can nevertheless assure you that it is an issue.

char *tempstr = strdup(str);

If this strdup() started returning NULL due to your heap filling up, or because the non-null-terminated memory garbage in commandC[] was too big to fit, your program would behave exactly as you describe.

Thanks. That helps a lot. By the way do I need to add anything to my code that checks for null terminators? Or will it automatically stop when it encounters a NULL?

strdup() strtok() and atoi() are all designed to require, handle and create null terminations on the strings you send them and that they return. You don't need to add code to check for null terminations on their results, if that's what you're asking.