If statement not working

Hello guys,
I have a project that using QRCode Scanner (GM65) and the program it's look like this

void loop(void) {
  while(Serial2.available()>0)
  {
    delay(10);
  String d = Serial2.readStringUntil('\n');
  rng = d.toInt();
  Serial.print("rng : ");
  Serial.println(rng);
  }
  
  switch (rng){
      case 1:
      kodee = "dwasqw";

      break;
      case 2://sebelum e 4
      kodee = "awqfeed";
      break;
    }
    while(Serial3.available()>0)
  {
    delay(10);
  char c=Serial3.read();
  inputString += c;

  }
 if (inputString.length()>0)
  {
  if(kodee == inputString)
    {lcd.clear();
      Serial.println("MATCHED");
      delay(3000);
      lcd.clear();
      digitalWrite(3, HIGH);

      kodee= "";
     
    } else {
      lcdclear();
      Serial.println("WRONG CODE");
      lcd.setCursor(5,0);
      lcd.print("Kode");
      lcd.setCursor(6,1);
      lcd.print("WRONG");
      delay(3000);
      lcd.clear();
      
    }
    inputString= "";
  }
    
}

kodee is String type
inputString also
When the GM65 scanning the code, "inputString" will be same with "kodee" but it didn't go to the first statement
It skipped to the else statement, I tried change "kodee" to char but it still not working.

Is there something wrong or missing ?
Thanks for help

Pict : Serial monitor when the scanner scan the code, it matched but not for arduino

Please post your entire sketch.

I suspect that inputString contains a control character (maybe a newline). Print out the length of each string and see if they match then you'll know there is some other character in inputString.

When the GM65 scanning the code, "inputString" will be same with "kodee" but it didn't go to the first statement
It skipped to the else statement, I tried change "kodee" to char but it still not working.

A String type variable can contain non-printable characters. If your Arduino tells you they aren't equal, there is a difference.

I have a project that using QRCode Scanner (GM65) and the program it's look like this

No, that's not your program, that's just an excerpt. Post sketches that at least compile, otherwise we usually expect the error to be in that part of the sketch, you're hiding from us.

Thanks for the reply,

I already check about the control character (like newline etc) but it still not working as well.

pylon:
No, that's not your program, that's just an excerpt. Post sketches that at least compile, otherwise we usually expect the error to be in that part of the sketch, you're hiding from us.

The full program will be looks like this

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SoftwareSerial.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // set LCD
String inputString;
String rngg;
int rng;
String kodee;
void setup(void) {
  lcd.begin(16,2);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  Serial.begin(9600);
  Serial2.begin(9600);
  Serial3.begin(9600);
  Serial.println('\n');
}

void loop(void) {
  while(Serial2.available()>0)
  {
    delay(10);
  String d = Serial2.readStringUntil('\n');
//  Serial.println(c);
  rng = d.toInt();
  Serial.print("rng : ");
  Serial.println(rng);
  }
  
//  Serial.print(d);

  switch (rng){
      case 1:
      kodee = "dwasqw";

      break;
      case 2://sebelum e 4
      kodee = "awqfeed";

      break;
      case 3: //sebelum e 9
      kodee = "fafaew";
      break;
      case 4: // sebelum e 10
      kodee = "wwwwww";

      break;
      case 5: 
      kodee = "dawdawdsax";

      break;

    }
    while(Serial3.available()>0)
  {
    delay(10);
  char c=Serial3.read();
  inputString += c;


  
  }

  Serial.print("inputString : ");
  Serial.println(inputString);
  Serial.print("kodee : ");
  Serial.println(kodee);
  Serial.println();

 if (inputString.length()>0)
  {
  if(kodee == inputString)
    {lcd.clear();
      Serial.println("MATCHED");
      lcd.setCursor(1,0);
      lcd.print("ParkirBerhasil");
      lcd.setCursor(5,1);
      lcd.print("Dibuka");
      delay(3000);
      lcd.clear();
      digitalWrite(3, HIGH);
      Serial2.print("Terbuka");
      kodee= "";
     
    } else {
      lcd.clear();
      Serial.println("WRONG");
      lcd.setCursor(5,0);
      lcd.print("Kode");
      lcd.setCursor(6,1);
      lcd.print("Salah");
      delay(3000);
      lcd.clear();
      
    }
    inputString= "";
  }
    
}

pylon:
A String type variable can contain non-printable characters. If your Arduino tells you they aren't equal, there is a difference.

I think so, but I still dont see the difference between the data

So is inputString.length() equal to kodee.length()?

ToddL1962:
So is inputString.length() equal to kodee.length()?

Yes, it's same

Time to add some debug prints

Looks like the print statements should already show the problem.

You are building up inputString character by character and checking it every time. You check it before it is ever going to match, so it doesn’t.

Something has to indicate the end of building up inputString; only then does it make sense to see if it matches.

a7

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.