I wrote arduinoArduino code (code is given in lastcode 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
XXstring string and usingXX.remove(0,1);I removedG fromfromG254.XX now becomenow become254 - Then I used
Serial1.println("P");to get position of motor after rotation. I stored this value inZZstring usingString ZZ = Serial1.readString();.ZZ will bewill beP254. - And using
XX.remove(0,1);I removedP fromfromP254.ZZ now becomenow become254. - Now, I convert both XX
XXand ZZZZinto integer from string usingXX.toInt(); ZZ.toInt();. - 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" );
}
}
}
}