Psaidiwd
Psaidiwd

Reputation: 27

My Serial Command line for Arduino doesn't work and I don't know why

please help I'm blind

void commandline() {
  if (Serial.available() > 0)
  {
    String command = Serial.readStringUntil(' ');
    String commandvar = Serial.readString();

    Serial.print(command);
    Serial.print(" | ");
    Serial.println(commandvar);

    if ( command == "fans" || command == "Fans" || command == "FANS" )
    {
      Serial.println("Command Recognized");
      if ( commandvar == "on" || commandvar == "On" || commandvar == "ON" )
      {
        Serial.println("                  Fans are now ON");
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
      }
      else if ( commandvar == "off" || commandvar == "Off" || commandvar == "OFF" )
      {
        Serial.println("                  Fans are now OFF");
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
      }
      else
      {
      Serial.println("commandvar isn't valid");
    }
  }

when i look at the serial console and send "fans on", the console spits out

fans | on

Command Recognized
commandvar isn't valid

any help would be very much appreciated

=========================

Extra Info

1) I'm calling commandline() from loop() every 10 miliseconds

2) The fans command isn't the only command I have it's just that I have to convert them to work with the new command variables I created

3) I'm Using the Arduino Uno Rev 3

Upvotes: 0

Views: 102

Answers (1)

John3136
John3136

Reputation: 29266

The output is a hint: There is a blank line. Says to me that commandvar = "on\n" and therefore != "on"

You can confirm this is the problem by checking the length of the string - if it is > 2 then there is some special character(s) in there that you were not expecting. If the characters are just white space, then you probably just need commandvar.trim() to get just the important part (i.e. no whitespace)

Upvotes: 2

Related Questions