String Comparison (conditional statement)

i am having trouble with my code, it is for a robot car i am building. i have set some commands in it to be given over serial and a message to say when an invalid command is given. i started up the arduino with the program uploaded and typed in the "forward" command however received the invalid command message. Id like to know why this is any help much appreciated.

here is the code:

String command;  // variable to store the command in

void setup(){
  Serial.begin(9600);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
}

void loop(){
  if (Serial.available() > 0){
    char c = Serial.read();
    
    if (c == '\n'){
      handleCommand(command);
      command = "";
    }
    else{
      command += c;
    }
  }
}

void handleCommand(String com){
  if (com == "forward"){
    digitalWrite(7, 1);
    digitalWrite(12, 1);
    delay(500);
    digitalWrite(7, 0);
    digitalWrite(12, 0);
  } else if (com == "back"){
    digitalWrite(6, 1);
    digitalWrite(11, 1);
    delay(500);
    digitalWrite(6, 0);
    digitalWrite(11, 0);
  } else if (com == "left"){
    digitalWrite(6, 1);
    digitalWrite(12, 1);
    delay(100);
    digitalWrite(6, 0);
    digitalWrite(12, 0);
  } else if (com == "right"){
    digitalWrite(7, 1);
    digitalWrite(11, 1);
    delay(100);
    digitalWrite(7, 0);
    digitalWrite(11, 0);
  } else {
    Serial.println("Invalid Command");
  }
}

Try printing the String com when you first enter the handleCommand function and see if it is what you think it is.

i made it print back, typed the command, received the exact same back and the invalid command message. yes it is what i think it is

How do you have the serial monitor set up as to carriage returns and new line feeds being added? If you have both set, then one will be a trash character in what you capture.

i had it set to both originally so set to carriage return, that didn't send back the invalid command message. i set it to newline and that was the correct one, its just not turning the motors now

is digital pin 12 the same as pin 12? on the arduino nano board

I did a test with this code:

String command;  // variable to store the command in

void setup(){
  Serial.begin(9600);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
}

void loop(){
  if (Serial.available() > 0){
    char c = Serial.read();
    
    if (c == '\n'){
      handleCommand(command);
      command = "";
    }
    else{
      command += c;
    }
  }
}

void handleCommand(String com){
  if (com == "forward"){
    Serial.println("F");    
    digitalWrite(7, 1);
    digitalWrite(12, 1);
    delay(500);
    digitalWrite(7, 0);
    digitalWrite(12, 0);
  } else if (com == "back"){
    Serial.println("B");    
    digitalWrite(6, 1);
    digitalWrite(11, 1);
    delay(500);
    digitalWrite(6, 0);
    digitalWrite(11, 0);
  } else if (com == "left"){
    Serial.println("L");    
    digitalWrite(6, 1);
    digitalWrite(12, 1);
    delay(100);
    digitalWrite(6, 0);
    digitalWrite(12, 0);
  } else if (com == "right"){
    Serial.println("R");    
    digitalWrite(7, 1);
    digitalWrite(11, 1);
    delay(100);
    digitalWrite(7, 0);
    digitalWrite(11, 0);
  } else {
    Serial.println("Invalid Command: ");
    Serial.println(com);    
  }
}

and it work for me. As I don't have anything connected to the ports, I print a letter in the if to see if is working or not. I selected "Newline" in the Serial Monitor.

is digital pin 12 the same as pin 12 on the arduino nano board????

If your question is if this:

  pinMode(12, OUTPUT);

is the digital pin 12 (D12), the answer is yes.

its just not turning the motors now

How are you powering the motors? The most common mistake is trying to power motors/servos directly from the arduino. They need a separate external power supply.

im using an l293d motor driver IC

i have managed to fix this problem by adding a 6 volt power supply for the motors themselves, thank you