Code entering if statement when it shouldn't

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //RX, TX

String motorNum;
String mSpeed;
String distance;


void setup() {
  Serial.begin(9600);   // Open serial communications with computer and wait for port to open:
  mySerial.begin(9600);  // Open serial communications with the other Arduino board
  delay(20);
}

void loop() {
  Serial.println("Please choose a motor to move: 1, 2, 3");
  while(Serial.available() == 0){}  // Wait for user input

  motorNum = Serial.readString();
  Serial.println(motorNum);

  if((motorNum != '1') && (motorNum != '2') && (motorNum != '3')){
    Serial.println("Please choose a valid motor, enter '1', '2', or '3'");
    while(Serial.available() == 0){}
    motorNum = Serial.readString();
  }

  Serial.println("Please select motor speed and direction (Sign determines direction)");
  while(Serial.available() == 0){}
  mSpeed = Serial.readString();

  Serial.println("Please input travel distance");
  while(Serial.available() == 0){}
  distance = Serial.readString();

  Serial.println("");
  Serial.print("Motor ");
  Serial.print(motorNum);
  Serial.println(" selected");
  Serial.print("Motor set to speed ");
  Serial.println(mSpeed);
  Serial.print("Motor will travel ");
  Serial.print(distance);
  Serial.println(" units");
  Serial.println("");
  


}

motorNum is initialized as a string. I want it to enter the if statement if the user enters something other than 1, 2, or 3. For some reason, it enters the if statement even when a valid input is given. My guess at what's happening is the if statement is searching for a non-string value, but I surrounded the check in quotes so it should be a string shouldn't it?

What for loop?

Please don't post code snippets. It's against forum rules for a good reason.

The if statement* my bad. I've updated the post to show the whole sketch

Comparing a String to a single character?

(This isn't an installation issue)

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

Hi,

Now understand why a piece of code does not help?

motorNum is string and "1', '2' and '3' are characters.
no match

I suggest you slow down and write the simplest code you can to receive characters from the serial monitor, do something simple with them and respond in some way. When you understand how that works and what is possible only then proceed towards something more complicated. You are making the common mistake of trying to do something more complicated than you know how to do without understanding the basics first.

Have fun experimenting.

The Serial Input Basics tutorial on this forum is a pretty good introduction.

Not sure how you would make this more simple. I thought ‘1’ would register as a string. How do I check for a string rather than a character then?

Remove all the other stuff, the stuff about motors. Remove the multiple conditions in your if statement and have one simple condition with a simple response.

Forget Strings, you are sending a character, check for a character. See the tutorial @jremington linked to.

If you set the warning level to all in file → preferences in the IDE, you will (e.g.) get the following warning

C:\Users\sterretje\AppData\Local\Temp\.arduinoIDE-unsaved2023218-1916-47mln0.wtgdd\sketch_mar18a\sketch_mar18a.ino: In function 'void loop()':
C:\Users\sterretje\AppData\Local\Temp\.arduinoIDE-unsaved2023218-1916-47mln0.wtgdd\sketch_mar18a\sketch_mar18a.ino:24:20: warning: invalid conversion from 'char' to 'const char*' [-fpermissive]
   if ((motorNum != '1') && (motorNum != '2') && (motorNum != '3'))
                    ^~~

That's a clear indication that something is wrong with your if statement.

You were wrong. As already explained, it is a single ASCII character, representing the numeral 1.

"1" is a C-string, a two byte character array, the ASCII representation of the digit 1 followed by a terminating zero byte.

A String is something entirely different from either of the above. Strings should be avoided on Arduinos because they cause memory problems and unexpected program failures.

Hello falcormoor

Try and check this mods for your sketch.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //RX, TX

String motorNum;
String mSpeed;
String distance;


void setup() {
  Serial.begin(9600);   // Open serial communications with computer and wait for port to open:
  mySerial.begin(9600);  // Open serial communications with the other Arduino board
  delay(20);
}

void loop() {
  Serial.println("Please choose a motor to move: 1, 2, 3");
  while (Serial.available() == 0) {} // Wait for user input

  motorNum = Serial.readStringUntil('\n');
  Serial.println(motorNum);

  if ((motorNum != "1") && (motorNum != "2") && (motorNum != "3")) {
    Serial.println("Please choose a valid motor, enter 1,2 or 3");
    while (Serial.available() == 0) {}
    motorNum = Serial.readStringUntil('\n');
  }

  Serial.println("Please select motor speed and direction (Sign determines direction)");
  while (Serial.available() == 0) {}
  mSpeed = Serial.readStringUntil('\n');

  Serial.println("Please input travel distance");
  while (Serial.available() == 0) {}
  distance = Serial.readStringUntil('\n');

  Serial.println("");
  Serial.print("Motor ");
  Serial.print(motorNum);
  Serial.println(" selected");
  Serial.print("Motor set to speed ");
  Serial.println(mSpeed);
  Serial.print("Motor will travel ");
  Serial.print(distance);
  Serial.println(" units");
  Serial.println("");
}

Have a nice day and enjoy coding in C++.

Ok, so this is for a class project where I need to send orders over an XBee connection that can only transfer 1 byte of data at a time. I need to send orders like the motor speed where the range is +-400. When I asked my professor how I can send a value grater than 256 over the xbee, he told me to use these Strings. I was also running into a lot of issues with Serial.read() not clearing the serial buffer and often getting random values for any inputs and reads after the first and using Strings seemed to have solved that problem.

Since Strings are not recommended, how would I do this using strings or characters instead?

Your professor gives bad advice. Use two bytes to send a 16 bit integer.

Could you explain how you would do that please?

Post the code showing how you are sending a single byte. Use code tags.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //RX, TX

int mSpeed;
int rotation

void setup() {
  Serial.begin(9600);   // Open serial communications with computer and wait for port to open:
  mySerial.begin(9600);  // Open serial communications with the other Arduino board
  delay(20);
}
void loop() {
  Serial.println("Please select motor speed and direction (Sign determines direction)");
  while(Serial.available() == 0){}
  mSpeed = Serial.read();
  mySerial.write(mspeed);
}

Here's how I would try to do it with a single byte. The problem with this however is there were often issues with the serial buffer using this method and if I were to ask for a second input I would often get things other than what I put in returned. For example if I had this directly afterwards:

delay(20)
Serial.println("Please input rotation in degrees");
while(Serial.available() == 0){}
rotation = Serial.read();
Serial.print(rotation);

I input 20, it will say I input some random thing like 50.

The above code asks for a sign and value, but you read in and send a single character.

That can't possibly work as intended. See the tutorial linked in post #8.

Ok, I have read the post, does this resolve the issue with the xbee being limited to the single byte? Lets say I've built a string to say -400 named char mSpeed, if I do mySerial.write(mSpeed);, it would work?