Character Array - depict message on serial monitor

Hey guys,

I am given a message "70.0,95,15:00" which tells me the temperature and humidity at a specific time. I want to access this message piecewise. So for example I want to print the temperature and the humidity separately on the serial monitor.

I have tried the following code. Can anyone help me figure out what's wrong?

int FindCharacter(char msg[], char c);

void setup() {
  // put your setup code here, to run once:
  char message[] = "70.0,95,15:00";

  int index;
  int holdIndex = 0;
  int temperature;
  int humidity;

  Serial.begin(9600);

  index = FindCharacter(message, ',');
  if(index > '0'){
    message[index] = '\0';
    temperature = atoi(message);
    holdIndex += index + 1;
  }

  index = FindCharacter(message + holdIndex, ',');
  if(index > '0') {
    message[index] = '\0';
    humidity = atoi(message + holdIndex);
    holdIndex += index + 1;
  }

  Serial.print("The temperature is = ");
  Serial.print(temperature);
  Serial.print(" with humidity = ");
  Serial.print(humidity);
  Serial.print(" at ");
  Serial.print(message + holdIndex);
}


void loop() {
  // put your main code here, to run repeatedly  

}

  /*****
   Purpose: Look for a specific character in a string

   Parameter list:
   char msg[]     an array of characters, null terminated
   char c         the char to find

   return value: 
   int            the position in the string where found or 0 if no match
   *****/
   int FindCharacter(char msg[], char c) {
    int i = 0;
    while(msg) {
      if(msg[i] == c){
        return i;
      }else{
      i++;
      }
    }
    return 0;
   }

It may be preferable not to reinvent the wheel. Take a look at strtok().

For your application even strchr() is enough, which you kind of reinvented in your sketch.
I think using a float to parse the temperature would be better.

void setup() {
  char message[] = "70.0,95,15:00";
  float temperature = -999;
  int humidity = -1;
  char * ptr = nullptr;
  Serial.begin(250000);
  temperature = strtod(message, &ptr);
  if (*ptr == ',') {
    humidity = atoi(++ptr);
    ptr = strchr(ptr, ',');
    if (ptr) {
      ptr++;
    }
  }
  Serial.print(F("The temperature is = "));
  Serial.print(temperature, 1);
  Serial.print(F(" with humidity = "));
  Serial.print(humidity);
  if (ptr) {
    Serial.print(F(" at "));
    Serial.print(ptr);
  }
  Serial.println();
}
void loop() {}
The temperature is = 70.0 with humidity = 95 at 15:00

Man! I wouldn't have thought to write it that way in a bazillion years. Also, I didn't even know that nullptr had been implemented in GCC. Thanks!

@ Whandall: Your solution is really neat, thx so much! I mean, I'm a programming beginner, so would you mind explaining to me why you initialized temperature = -999, humidity = -1 and char * ptr = nullptr ?

I have just run the code and it works without this initialization as well..

It is basically used to be able to detect elements that could not be parsed or set.

The variables are local, so they will contain garbage if not initialized, that could obscure errors or lead to them.

   int FindCharacter(char msg[], char c) {
    int i = 0;
    while(msg) {
      if(msg[i] == c){
        return i;
      }else{
      i++;
      }
    }
    return 0;

0 is a crappy value to mean "not found" since the character being looked for COULD be in the 0th position of the array. With this code, you can't distinguish between not found and found in the 0th element.