Arduino char array comparisson not working

Hello,

I’m trying to compare a string (i.e. a char array) that I’m reading from another arduino connected with bluetooth. The first arduino sends the following string:

Serial.println("GO_UP");

Now the second arduino I’m doing the following:

void loop() {
  int serialData = Serial3.available();
  char conReq[serialData];
  for (int i = 0; i < serialData; i++) {
    conReq[i] = Serial3.read(); 
  }
  conReq[serialData] = '\0';
    
  
  if (strcmp(conReq, "C") == 0) {
    Serial3.print("OK");
  } else if (strcmp(conReq, "GO_UP") == 0){
      Serial.println("GoingUp");  
  }
}

The above code never enters to the second if statement. I then tried to convert the char array to String (I know it is a bad idea) and then perform trim() in order to remove CR and LF characters:

void loop() {
  int serialData = Serial3.available();
  char conReq[serialData];
  for (int i = 0; i < serialData; i++) {
    conReq[i] = Serial3.read(); 
  }
  conReq[serialData] = '\0';
    
  String request(conReq);
  request.trim();
  if (request == "C") {
    Serial3.print("OK");
  } else if (request == "GO_UP"){
      Serial.println("GoingUp");  
  }
}

But neither this worked.

Any ideas??

I suggest that you look at Serial input basics - updated and use the techniques it explains

Because you go beyond the size of the array here:conReq[serialData] = '\0'; you should declare it a tad biggerchar conReq[serialData+1];

UKHeliBob:
I suggest that you look at Serial input basics - updated and use the techniques it explains

Problem fixed.
I used start/end characters and that did the job!
Many thanks...

  int serialData = Serial3.available();
  char conReq[serialData];
  for (int i = 0; i < serialData; i++) {
    conReq[i] = Serial3.read(); 
  }

Think very carefully about this, particularly given that available() may return only 1 for each iteration.

AWOL:

  int serialData = Serial3.available();

char conReq[serialData];
 for (int i = 0; i < serialData; i++) {
   conReq[i] = Serial3.read();
 }



Think very carefully about this, particularly given that available() may return only 1 for each iteration.

Probably this was the problem because when received only a ‘C’ it was entered in the first statement. But why is this happening? Does the serial data being transmitted slower?

mandim:
Probably this was the problem because when received only a 'C' it was entered in the first statement. But why is this happening? Does the serial data being transmitted slower?

depends on the BAUD rate, if not the whole statement has been received before you read it's size, only the characters available at the time will be read, the rest stay in the buffer, where they end up in front of the next part received (or they get read as a separate string) i think the main problem was you assign a teminating '\0' to the address of the array-size, which is just above it's limit, there might not be to much to worry about, if there is no other variable declared for that space, but either way, the char-array will add it's own '\0' (i think) making you lose your final character, and you test for the whole thing.