My code is not working as intended. actually it just check only 12 character and show me valid or invalid. but i want it to check the whole id first then show me either its valid or not. can anyone help me with this? It will be very helpful

#include <LiquidCrystal.h>
int count = 0;
char c;
String id;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
Serial.println("Please scan your RFID TAG");
lcd.begin(16, 2);
lcd.print("Computer Locked!");

}

void loop() {
while(Serial.available()>0)
{
c = Serial.read();
count++;
id += c;
if(count == 12)
{
Serial.print(id);

  if(id=="Antor1234567")
  {
    Serial.println("Valid TAG");
    digitalWrite(13, HIGH);
    lcd.begin(16, 2);
lcd.print("Computer Unlocked");



  }
  else if(id=="Tanvir123456")
  {
    Serial.println("Valid TAG");
    digitalWrite(13, HIGH);
    lcd.begin(16, 2);
lcd.print("Computer Unlocked");
  }

  else
  {
  digitalWrite(12, LOW);
  Serial.println("Invalid TAG");
  }
}

}
count = 0;
id="";
delay(500);

}

...and if it isn't, clear the String and try again.

Please remember to use code tags when posting code

can you elaborate please? and sorry for the tag. but i am new here. i have attached a pic too

You are clearing the buffer too early. You should wait until you have gotten the 12 characters you want and have checked for matches.

Try replacing the last part of your sketch with this:

      else
      {
        digitalWrite(12, LOW);
        Serial.println("Invalid TAG");
      }

      delay(500);
      while (Serial.available())
        Serial.read(); // Remove anything left in the buffer
      count = 0;  // Start over
      id = "";

    }  // end if (count == 12)
  }
}

as this id was supposed to be invalid but its showing valid.

Please post your code as it looks now.

Don't forget the code tags.

Don't post pictures.

@antorroydravi
I have checked your sketch keeping only the codes relating to Serial Monitor and found it working.
The Sketch

int count = 0;
char c;
String id;

void setup() {
  Serial.begin(9600);
  Serial.println("Please scan your RFID TAG");
}

void loop()
{
  while (Serial.available() > 0)
  {
    c = Serial.read();
    count++;
    id += c;
    if (count == 12)
    {
      Serial.print(id);
      if (id == "Antor1234567")
      {
        Serial.println("Valid TAG");
      }
      else if (id == "Tanvir123456")
      {
        Serial.println("Valid TAG");
      }
      else
      {
        Serial.println("Invalid TAG");
      }
    }
  }
  count = 0;
  id = "";
  delay(500);
}

smtag

I saw no mention of a serial monitor in the original post?
Have I missed something?

I assumed this was meant to come out on the serial monitor, as were the other print statements.

a7

I saw only a print to a serial line.
I didn't see anything about the serial monitor.

How does "serial line" differ from "serial monitor"?

You're kidding, right?

The code has Serial prints and reads.
That is all we know.

What device is on the other end of the Serial connection is moot - do not assume.

... for a limited definition of "working".
You wouldn't get hired as a software tester.

Certainly, higher level of vision which was beyond my grasp. Now, I know the difference between "Serial line" and "Serial Monitor" in general perspective. Howvever in Arduino context, the device located at the other end of Serial line is the Serial Monitor.

The thing I object to in that sentence is the word "is".

It's like it's some kind of holy writ, and no-one ever uses terminal emulators, or, indeed terminals.

Either way, half a dozen examples does not a test plan make.

In the last 4 years of my Arduino Culture, I have seen only the Serial Monitor and IDE at the other end of the Serial line/Port - de facto and de jure!

This is the worst case of confirmational bias I've seen in a while - you should see a doctor.

Now, about your test strategy - have you figured out what's wrong yet?

Oh dear, another pi$$ing contest! :rofl:

1 Like

Hi, @antorroydravi
Please build and try the code in real time rather than use a simulator.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Heh heh ... :face_with_raised_eyebrow:

@antorroydravi your request or complaint, entirely in the title of your post, viz:

My code is not working as intended. actually it just check only 12 character and show me valid or invalid. but i want it to check the whole id first then show me either its valid or not. can anyone help me with this? It will be very helpful

Doesn't carefully explain how you want it to function. Please take a minute and tell us how you want it to work from the perspective of someone using your system.

The code does recognize valid and invalid 12 character IDs, but does so in a…, well let's just say not how I would do it, and not how it might better be done.

As for the moot-ness of what is attached to the "serial line", I assume @ AWOL means "subject to debate, dispute, or uncertainty", and not "having little or no practical relevance" as it could also be interpreted.

We would have been well served with just a few more of the precious life-time supply of words we are all granted on why it matters, perhaps sparing us some of this "debate".

The OP's code depends on the the ability of whatever is attached to the "serial line" to deliver 12 characters with no gaps greater than 500 milliseconds between them.

This is trivially accomplished with the serial monitor's input window, and perhaps by almost any external serial device or emulator. But there would be a surprising change in success if, for example, the characters arrived at the rate a human keyed them, as can be configured in some terminal programs or, perhaps, a keypad with a serial intercase.

The OP's code also quietly (and probably on accident of the coder) eats characters like CR and LF and shorter attempts and probably a few other things - only from an equilibrium state (serial buffer emtpy and count reset achieved after the gap in arrival of characters) is the program ready to take in the 12 char ID. This, too, may finally come to be an issue when the system is deployed with the eventual input device, serial or as it may even switched away from from a simulated by serial to a keypad getchars or whatever.

So that's an expenditure of 1 x 10e-5 percent of my lifetime supply of words, I certainly hope that helps y'all.

a7

1 Like