comparing strings is not working

I'm running out reasons why this doesn't work:

char data;
String result;
if(newcommand==true)
{
Serial.println(cmdline);
newcommand =false;

if(cmdline=="start")
{
myMotorA->setSpeed(10);
Serial.println("go");
}
if(cmdline =="stop")
{
Serial.println("end");
myMotorA->setSpeed(0);
}
cmdline = "";
}

A serial event grabs newcommand, and returns it. It prints the right output, but none the other conditions work. I've run out of ideas.

You’ll get a lot more help if you post the whole code, and please use code tags, so it appears in a box

like this

Also worth noting that you never declare ‘newcommand’ in the snippet above.

. I've run out of ideas.

line-ending setting in the serial monitor?

TheMemberFormerlyKnownAsAWOL:
line-ending setting in the serial monitor?

SerialMonitor.png

SerialMonitor.png

What data type is cmdline, String, char array, etc?

#include <Wire.h>
#include <String.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_DCMotor *myMotorA = AFMS.getMotor(1);
Adafruit_DCMotor *myMotorB = AFMS.getMotor(2);
Adafruit_DCMotor *myMotorC = AFMS.getMotor(3);
Adafruit_DCMotor *myMotorD = AFMS.getMotor(4);

String cmdline = "";
bool newcommand = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);           // set up Serial library at 9600 bps
  //cmdline.reserve(200);
  
  //Serial.println("9");
  //Serial.println("Motor test!");
  AFMS.begin();
  myMotorA->setSpeed(0);
  
  /*myMotorB->setSpeed(0);
  myMotorC->setSpeed(0);
  myMotorC->setSpeed(0);*/

}

void serialEvent()
{
  
  String newline;
  
  newcommand=false;
  char inchar;
  while(Serial.available())
  {
    inchar = Serial.read();
    if(inchar=='\n')
    {
      newcommand=true;
    }else
    {
      newline+=inchar;
    }
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  char data;
  String result;
  if(newcommand==true)
  {
    Serial.println(cmdline);
    newcommand =false;
    
  if(cmdline=="start")
  {
    myMotorA->setSpeed(10);
    Serial.println("go");
  }
  if(cmdline =="stop")
  {
    Serial.println("end");
    myMotorA->setSpeed(0);
  }
  cmdline = "";
  }
}

an Adafriut Motor Shield v2.3 on a Arduino Mega. The Motor Shield component works fine. The Serial Interrupt receives the message fine. Serial.println(cmdline); shows the right message.

None of the conditions are triggered.

And the line-ending is . . ?

I can't see where cmdline is getting set to anything but "".

...which makes this assertion

Serial.println(cmdline); shows the right message.

all the more remarkable

Are you simply printing the 'whole' string, but not seeing it.

Serial.print(F("<"));  // a prefix to show the 'real' start of the string
Serial.print(cmdLine);  // the command string - warts and all
Serial.println(F(">"));  // a suffix to show the 'real' end of the string

Will bracket the cmdLine - which could (undesirably) appear like...
<command
>

  • or ideally -

This will show 'what' you're comparing.

You are failing to copy newline to cmdline at the relevant point I think. This is either when you
see a newline in serialEvent, or when you recognize that newcommand has become true.

i found that now it returns nothing
i'm probably not capturing the data right. thanks for the help anyway

while(Serial.available())
  {
    inchar = Serial.read();
    if(inchar=='\n')
    {
      newcommand=true;
      cmdline=newline;
    }else
    {
      newline+=inchar;
    }
  }

This:

  String newline;

is defined only within the function serialEvent(). Try making it global.

But you should not be using Strings anyway.

that did it. :slight_smile: