Skip to main content
Formatted code. Improved text.
Source Link
sa_leinad
  • 3.2k
  • 2
  • 24
  • 53

I wrote arduinoArduino code (code is given in lastcode 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 XXstring string and using XX.remove(0,1); I removed G from from G254. XX now become 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 will be P254.
  4. And using XX.remove(0,1); I removed P from from P254. ZZ now become now become 254.
  5. Now, I convert both XXXX and ZZZZ 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 " EQUAL");
      }
      else 
      {
      Serial.println("BOTH ARE NOT EQUAL" );
        }

butBut it shows BOTH ARE NOT EQUAL which is not true. I wantswant to know where I'm doingmaking a mistake?

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 > 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 " EQUAL");
        }
        else 
        {
             Serial.println("BOTH ARE NOT EQUAL" );
    }
   
  } 
    
 }    
}

I wrote arduino code (code is given in last) 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 XXstring 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 wants to know where I'm doing mistake?

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

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?

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");
        } 
    }    
}
Source Link

Unable to compare two integer values in Arduino Mega

I wrote arduino code (code is given in last) 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 XXstring 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 wants to know where I'm doing 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.