I wrote Arduino code (code is given at the end) to compare two integer values. The steps I took was:
- Give input to DC motor to rotate it using serial port. Input was:
G254
- Then I store it in
XX
string and usingXX.remove(0,1);
I removedG
fromG254
.XX
now become254
- Then I used
Serial1.println("P");
to get position of motor after rotation. I stored this value inZZ
string usingString ZZ = Serial1.readString();
.ZZ
will beP254
. - And using
XX.remove(0,1);
I removedP
fromP254
.ZZ
now become254
. - Now, I convert both
XX
andZZ
into integer from string usingXX.toInt(); ZZ.toInt();
. - After that, I compare both values using:
if (XX == ZZ) { Serial.println("BOTH ARE EQUAL"); } else { Serial.println("BOTH ARE NOT EQUAL"); }
But it shows BOTH ARE NOT EQUAL
which is not true. I want to know where I'm making a mistake?
Code I used:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0)
{
Serial1.println(Serial.readString());
String XX = Serial1.readString();
XX.remove(0,1);
Serial1.println("P");
String ZZ = Serial1.readString();
ZZ.remove(0,1);
XX.toInt();
ZZ.toInt();
Serial.println(ZZ);
Serial.println(XX);
Serial.println(typeof(XX));
if (XX == ZZ)
{
Serial.println("BOTH ARE EQUAL");
}
else
{
Serial.println("BOTH ARE NOT EQUAL");
}
}
}
Thanks.
-
A question will get downvotes if question shows no effort. Fine. But I tried hard and do above coding by myself. I need to add int x (Thanks @Michel) in my code which I don't know and that's why I put my question here. I think this question should not be downvoted on the basis of efforts because I try to find out a solution from other sources but cann't and at last, I put my query here. I loose my points unnecessarily.Naseeb Gill– Naseeb Gill2017年10月31日 16:16:05 +00:00Commented Oct 31, 2017 at 16:16
2 Answers 2
You should use
int x = XX.toInt()
Without the int x = part, the string is converted and the result (the integer) is not used.
Also comparing strings should be done with the strcmp function. The problem is that if you compare two strings, you compare the pointers to those strings and two strings can have equal texts but (probably) not equal addresses.
-
2Might need to add that XX.toInt() returns the value of the string after converting to a number. It does not convert the string (internally) to a number. The string is still a string. Also it is possible to compare the strings directly to be equal.Mikael Patel– Mikael Patel2017年10月31日 14:01:43 +00:00Commented Oct 31, 2017 at 14:01
-
1@MikaelPatel. String comparison is alphabetic, not numeric.user31481– user314812017年10月31日 14:09:13 +00:00Commented Oct 31, 2017 at 14:09
-
1@LookAlterno True but equal is equal even for strings - isn't it :) arduino.cc/en/Tutorial/StringComparisonOperatorsMikael Patel– Mikael Patel2017年10月31日 14:14:35 +00:00Commented Oct 31, 2017 at 14:14
-
1@MikaelPatel There are people that learn JavaScript or PHP as a first language; unaware of the differences.user31481– user314812017年10月31日 14:24:22 +00:00Commented Oct 31, 2017 at 14:24
-
Thanks for all comments, I updated the answer accordingly.Michel Keijzers– Michel Keijzers2017年10月31日 16:21:35 +00:00Commented Oct 31, 2017 at 16:21
There are several issues with your code.
First, never assume that Serial.readString() will returns you a complete String; it can time out and return nothing, or return just an incomplete value.
Serial.available() > 0
doesn't imply that the reading is complete ...
Second, Serial.readString() returns everything, include the possible \r\n at the end of the string.
Here is a little sketch that shows a toInt
implementation fit for you case. Run this sketch to see how it works.
void setup() {
Serial.begin(9600);
while(!Serial);
}
void loop() {
String x = Serial.readString();
Serial.print(x); Serial.print("=");
Serial.println(myToInt(x));
}
int myToInt(String x) {
char* p = x.c_str();
int val = 0;
while(*p != 0 && !isdigit(*p)) {
p++;
}
while (*p != 0 && isdigit(*p)) {
val = val * 10 + (*p - '0');
p++;
}
return val;
}