1

I wrote Arduino code (code is given at the end) to compare two integer values. The steps I took was:

  1. Give input to DC motor to rotate it using serial port. Input was: G254
  2. Then I store it in XX string and using XX.remove(0,1); I removed G from G254. XX now become 254
  3. Then I used Serial1.println("P"); to get position of motor after rotation. I stored this value in ZZ string using String ZZ = Serial1.readString();. ZZ will be P254.
  4. And using XX.remove(0,1); I removed P from P254. ZZ now become 254.
  5. Now, I convert both XX and ZZ into integer from string using XX.toInt(); ZZ.toInt(); .
  6. 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 Oct 31 '17 at 16:16
0

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.

  • 2
    Might 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 Oct 31 '17 at 14:01
  • 1
    @MikaelPatel. String comparison is alphabetic, not numeric. – user31481 Oct 31 '17 at 14:09
  • 1
    @LookAlterno True but equal is equal even for strings - isn't it :) arduino.cc/en/Tutorial/StringComparisonOperators – Mikael Patel Oct 31 '17 at 14:14
  • 1
    @MikaelPatel There are people that learn JavaScript or PHP as a first language; unaware of the differences. – user31481 Oct 31 '17 at 14:24
  • Thanks for all comments, I updated the answer accordingly. – Michel Keijzers Oct 31 '17 at 16:21
0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.