Character Array - depict message on serial monitor


hey guys,

i given message "70.0,95,15:00" tells me temperature , humidity @ specific time. want access message piecewise. example want print temperature , humidity separately on serial monitor.

i have tried following code. can me figure out what's wrong?

code: [select]
int findcharacter(char msg[], char c);

void setup() {
  // put setup code here, 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 = ");
  serial.print(temperature);
  serial.print(" humidity = ");
  serial.print(humidity);
  serial.print(" @ ");
  serial.print(message + holdindex);
}


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

}

  /*****
   purpose: specific character in string

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

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

it may preferable not reinvent wheel. take @ strtok().


Arduino Forum > Using Arduino > Programming Questions > Character Array - depict message on serial monitor


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???