My keypad don't run with a global code but with an another it works

Hello everyone. I am doing a door control system. I have a problem with my keypad 4 * 4.
When I put the code to make it work in my global program it does not work. The goal is that the user types a code, the arduino verifies if the code exists in the MySQL database. this part of SQL verification has not been done yet. For the moment I tested to see if the keypad works in my general code but it does not work. But when I try the Keypad outside my general code, it works.
I put you the general code afterwards.

#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <SPI.h>
#include <RFID.h>
#include <Keypad.h>
#include <Password.h>

int led = 13;                // the pin that the LED is atteched to
int sensor = 7;              // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected
int val = 0;                 // variable to store the sensor status (value)
bool detected = false;
int buzzerPin = 3;

//mot de passe

Password pass = Password( "1234" );
const byte ROWS = 4; // Four rows
const byte COLS = 4; //  columns
char keys[ROWS][COLS] = {

byte rowPins[ROWS] = { 33,32,31,30 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 37,36,35,34, };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

byte mac_addr[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
#define SS_PIN 9
#define RST_PIN 8

IPAddress server_addr(192,168,1,8);  // IP of the MySQL *server* here
char user[] = "hello";              // MySQL user login username
char password[] = "hello";        // MySQL user login password
String requete, carte;
char INSERT_SQL[] = "SELECT prenom FROM maison_intelligente.utilisateurs WHERE carte = '";

// Left-over from original code, variables needed to read the ID of the tag
// Setup variables:
    int serNum0;
    int serNum1;
    int serNum2;
    int serNum3;
    int serNum4;
EthernetClient client;
MySQL_Connection conn((Client *)&client);

void setup()
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  pinMode(buzzerPin, OUTPUT);
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  while (!Serial); // wait for serial port to connect

  Serial.println("\n\nRequete SQL:");
  if (conn.connect(server_addr, 3306, user, password)) {
    Serial.println("Sucess connexion to DataBase!");
    Serial.println("Connection failed.");

void loop()
  val = digitalRead(sensor);

  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds 
    if (state == LOW) {
      Serial.println("Motion detected!"); 
      state = HIGH;       // update variable state to HIGH
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(100);             // delay 200 milliseconds 
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
        detected = true;
        Serial.println("Presenter Carte: ");

 bool card_found = false;
  row_values *row = NULL;    
    if (rfid.isCard() && detected == true) 
      Serial.print("Numero de Carte : ");
      if (rfid.readCardSerial()) 
          serNum0 = rfid.serNum[0];
          serNum1 = rfid.serNum[1];
          serNum2 = rfid.serNum[2];
          serNum3 = rfid.serNum[3];
          serNum4 = rfid.serNum[4];
          requete = INSERT_SQL+String(carte)+"'";
          delay(10000);    // wait 10 seconds (or whatever you want if you change it)
          // start from the beginning again          


  //Serial.println("Recording data");
  requete.toCharArray(INSERT_SQL, requete.length()+2);
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);

  column_names *columns = cur_mem->get_columns();

  // Read the row (we are only expecting the one)
  do {
    row = cur_mem->get_next_row();
    if (row != NULL) {
      Serial.println(" ");
      Serial.print("Bonjour ");
      card_found = true;
  while (row != NULL);
      Serial.println(" ");
      Serial.println("Code Incorrect");
  delete cur_mem;

 // while(true){

    rfid.halt();  // close the stream

 //vraie = beep(100,50);
 //faux =  beep(100,2000);

void beep(unsigned char delayms, unsigned char pause ) { //creating function
  analogWrite(buzzerPin, delayms); //Setting pin to high
  delay(pause); //Delaying
  analogWrite(buzzerPin ,0); //Setting pin to LOW
  delay(pause); //Delaying
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
  //Serial.print("Pressed: ");
  switch (eKey){
    case '*': checkPassword(); break;
    case '#': pass.reset(); break;
    default: pass.append(eKey);

void checkPassword(){
  if (pass.evaluate()){
    //Add code to run if it works
    //add code to run if it did not work

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum


You should also be sure to define "doesn't work". What happens or doesn't happen that doesn't meet with your expectations. Describe the symptoms.

Your code is full of delay statements, I wouldn't expect it to be very responsive.


Out in the middle of nowhere, see of there is a key pressed. Be sure to completely ignore what key is pressed, if any.


  if (pass.evaluate()){
    //Add code to run if it works
    //add code to run if it did not work

So, why didn't you?