using em 18 rfid module with key pad and lcd

I tried to make a security system using a EM-18 RFID module, LCD and key pad. I have made my project that way when a valid card is placed in front of the module it detects and the lcd displays to "enter the password" but the problem is cant enter the alphabets or numbers but without rfid module it works can some one plzzz tell what mistake I am making???? I have attached the program. and I am using Arduino mega.

RFID.ino (2.47 KB)

Your state machine is weak. your loop goes through many steps checking serial or other conditions that you reset. Try to rebuilt it in the right way with proper state machine

states:

1 Waiting for RFID
2 Acquiring RFID
3 Acquiring Passcode
4 Action (like door opens)

you start in state 1

when you detect the presence of the RFID you move to state 2.

if ID matches a known one you go to step 3 otherwise go back to step 1

You stay in step 3 managing keypad input until either full passcode is entered, or a new RFID is presented.
if full passcode is presented and correct perform action and go to state 4 else go to either 3 or 1 (your choice)
if new RFID presented, go back to 2

in sate 4, wait n seconds and close the door or whatever

ok thnxx very much

hmm can u give me an example program of the states u were talking about plzzz

that would be an idea - not tested if this would even compile.... assuming all your stuff is connected correctly and that your tag reader is in Serial and properly sending codes in the right way.

#include <Wire.h>
#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {22, 23, 24, 25};
byte colPins[COLS] = {26, 27, 28, 29};

Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char customKey;

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

enum STATES {WAITING_RFID, ACQUIRING_RFID, ACQUIRING_PWD, ACTION} currentState;

const byte PasswordLength = 6;
char entryPassword[PasswordLength + 1];
const char MasterPassword[PasswordLength + 1] = "123456";

const unsigned long TimeOut = 10000ul;
unsigned long startTime;
unsigned long tickTime;


const byte tagLength = 12;
char correctTag[tagLength + 1] = "000003026869"; // could be declared as const, not sure what you want to do
char tagInput[tagLength + 1];

byte inputIndex;

const byte MOTOR_PIN1 = 31;
const byte MOTOR_PIN2 = 32;

void initializeState()
{
  lcd.clear();
  lcd.print(F("Present Card"));
  while (Serial.available()) Serial.read(); // empty the Serial buffer
  currentState = WAITING_RFID;
}

void setup()
{
  Serial.begin(9600);
  lcd.begin(20, 4); // Assuming you have 20 Cols and 4 Rows in your LCD
  initializeState();
}


void loop()
{
  // ------------------ STATE WAITING FOR RFID ------------------
  if (currentState == WAITING_RFID) {
    if (Serial.available()) {
      inputIndex = 0;
      currentState = ACQUIRING_RFID;
    }

    // ------------------ STATE ACQUIRING RFID ------------------
  } else if (currentState == ACQUIRING_RFID) {

    while (Serial.available()) {
      int c = Serial.read();
      if ((inputIndex < tagLength) && (c != -1)) tagInput[inputIndex++] = (char) c; // ignore extra ones
    }

    if (inputIndex == tagLength) {
      tagInput[tagLength] = '\0';
      if (!strcmp(correctTag, tagInput)) {
        lcd.setCursor(0, 0);
        lcd.print(F("Valid Card"));
        lcd.setCursor(0, 2);
        lcd.print(F("Welcome"));
        delay(1000);
        lcd.clear();
        lcd.print("Enter Password");
        inputIndex = 0;
        currentState = ACQUIRING_PWD;
      } else {
        lcd.clear();
        lcd.print(F("Invalid Card"));
        lcd.setCursor(0, 2);
        lcd.print(F("Go away"));
        delay(1000);
        initializeState();
      }
    }


    // ------------------ STATE ACQUIRING PWD ------------------
  } else if (currentState == ACQUIRING_PWD) {
    customKey = customKeypad.getKey();
    if (customKey != NO_KEY) {
      entryPassword[inputIndex] = customKey;
      lcd.setCursor(inputIndex, 1);
      lcd.print(customKey);
      if (++inputIndex == PasswordLength) {
        entryPassword[inputIndex] = '\0';
        if (!strcmp(MasterPassword, entryPassword)) {
          lcd.clear();
          lcd.print(F("CORRECT"));
          lcd.setCursor(0, 2);
          lcd.print(F("ACCESS GRANTED"));
          delay(3000);
          lcd.clear();


          
          // ===========================================================
          // password is good
          // TRIGGER THE MOTORS HERE MAY BE TO OPEN SOMETHING
          // ....
          // ....
          // ....
          // ===========================================================



          lcd.print(F("REMAINING TIME"));
          lcd.setCursor(0, 2);
          lcd.print(TimeOut / 1000);
          startTime = millis();
          tickTime = startTime + 1000;
          currentState = ACTION;
        } else {
          lcd.clear();
          lcd.print(F("INCORRECT"));
          lcd.setCursor(0, 2);
          lcd.print(F("ACCESS REFUSED"));
          delay(3000);
          initializeState();
        }
      }
    }


    // ------------------ STATE ACTION TRIGGERED ------------------
  } else  if (currentState == ACTION) {
    // stay in that state for TimeOut ms

    if (millis() - startTime >= TimeOut) {


      // ===========================================================
      // TRIGGER THE MOTORS HERE MAY BE TO CLOSE SOMETHING
      // ....
      // ....
      // ....
      // ===========================================================

      initializeState();
    } else {
      if (millis() - tickTime >= 1000) {
        lcd.setCursor(0, 2);
        lcd.print((TimeOut - (millis() - startTime)) / 1000);
        lcd.print(F("     ")); // erase previous numbers that could be trailing
        tickTime += 1000;
      }
    }
  }
}

thnk u very much J-M-L it worked

Glad to hear as I just typed it without checking

Hopefully I trust you studied the code structure to understand how a state machine can work... do your part of the learning.