Project in progress
Arduino Password Lock Project

Arduino Password Lock Project © GPL3+

Developing projects like commercial products

  • 2 views
  • 0 comments
  • 0 respects

Components and supplies

About this project

You want to improve yourself on embedded electronic systems. When designing a project, you should think like producing a commercial product. Encrypted keypad project is designed in this way.

Arduino Password Lock Project

Arduino Password Lock Project

You can understand how the system works by watching this video.

When designing an advanced project, you need to progress step by step. Download the sample codes to your computer and follow the project number and apply.

Algorithm Block Diagram

The software consists of two main parts. The first part becomes the main loop area. In the second part, new password creation and saving are done.We hold down the P key for two seconds on the keypad and enter the settings area.

We enter the administrator password first. It is included in the administrator password software and cannot be changed externally. After entering the administrator password, we can enter the new password creation area. Let's enter the new password as four characters. We hold down the enter key for two seconds. Your new password is stored in eeprom memory. We return to the main loop area. We can now use the new password. RGB led speaks to us by changing color. It shows us in which area of the software. There is a time function to return to the main loop when the user takes an erroneous action. The time function is used in four different places. Buzzer makes various warning sounds.

Let's examine the materials used in the project.

Keypad Board

https://www.altiumdesignerprojects.com/product/16-button-keypad-board

We need the keypad for the password input. Schematic drawing and Arduino links are at the bottom of the page. You can download the driver file from the link. https://github.com/Chris--A/Keypad

Relay 5V Board

https://www.altiumdesignerprojects.com/product/relay-5v

We need one relay. The relay you see in the picture is resistant to high current. We can also use a smaller relay in the project.

Buzzer Board

https://www.altiumdesignerprojects.com/product/buzzer

Buzzer module communicates with the user by creating various sounds. Tells the person who entered the password what action he did

WS2812 Color Led Board

https://www.altiumdesignerprojects.com/product/ws2812b-rgb-led

Information is given to the person who enters the password with colored led

Link to the driver file. file https://github.com/FastLED/FastLED

Projects Details

I sell design files of electronic circuit boards.You can use it in your projects.

Code

1_HelloKeypadArduino
#include <Keypad.h>     // https://github.com/Chris--A/Keypad
/*
 Hello 
 I design developer tools for embedded electronic systems. You can buy my projects design files.
 https://www.altiumdesignerprojects.com
*/
const byte ROWS = 4;    //four rows
const byte COLS = 4;    //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'P', '0', 'E', 'D'}
};

byte rowPins[ROWS] = {5, 4, 3, 2};     // satr
//            ROW     1  2  3  4
byte colPins[COLS] = {A0, A1, A2, A3}; // stun
//            COL     1   2   3   4

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char key;

void setup(){
 Serial.begin(9600);
}
  
void loop(){
  
  key = keypad.getKey();
  
  if(key){
     Serial.println(key);
  }
    
}
2_userPasswordArduino
#include <Keypad.h>
/*
 Hello 
 I design developer tools for embedded electronic systems. You can buy my projects design files.
 https://www.altiumdesignerprojects.com
*/
const byte ROWS = 4; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},  
  {'4', '5', '6', 'B'},  
  {'7', '8', '9', 'C'}, 
  {'P', '0', 'E', 'D'}   // P program  E enter
};

byte rowPins[ROWS] = {5, 4, 3, 2}; 
byte colPins[COLS] = {A0, A1, A2, A3}; 

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

char userPassword[4]   = "0000";   
char eepromPassword[4] = "1234";   // there will be a change   

bool checkPassword = false;

byte cursorPosition;
char key;

#define relayPin 10

void setup(){
 Serial.begin(9600);

 pinMode(relayPin,OUTPUT);
 digitalWrite(relayPin,LOW);
 Serial.println("-- SYSTEM START ---");
 
 keypad.setDebounceTime(100);   // Default is 50mS
 cursorPosition = 0;
     
}

void loop(){
  
  key = keypad.getKey();
 
  if(isDigit(key)){  // numeric keys only
      
     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 

     userPassword[cursorPosition] = key;
     cursorPosition++;

       if(cursorPosition >= 4){ 
        
          for(byte i=0; i <= 3; i++){   
            Serial.print("userPassword[" + String(i) + "] ... " + String(userPassword[i]) + " ... " );  
            Serial.print("eepromPassword[" + String(i) + "] ... " + String(eepromPassword[i]) + " ... "); Serial.print("\r\n"); 
          }
          
          cursorPosition = 0;
          
           if(userPassword[0] == eepromPassword[0] && userPassword[1] == eepromPassword[1] && userPassword[2] == eepromPassword[2] && userPassword[3] == eepromPassword[3]){
              checkPassword = true;
           }else{
              checkPassword = false;
           }
           
       } // if(cursorPosition >= 4) ****************
   
  }  // if(isDigit(key)) *************

       
  if(checkPassword == true){
    
    digitalWrite(relayPin,HIGH);
    Serial.println(" ");
    Serial.println("RELAY ON ... ");
    Serial.println(" ");    
    delay(500);
    checkPassword = false;
    
  }else{
    digitalWrite(relayPin,LOW);
  }


 
} // loop *********
3_errorInputArduino
#include <Keypad.h>
/*
 Hello 
 I design developer tools for embedded electronic systems. You can buy my projects design files.
 https://www.altiumdesignerprojects.com
*/
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},  
  {'4', '5', '6', 'B'},  
  {'7', '8', '9', 'C'}, 
  {'P', '0', 'E', 'D'}   // P settings  E enter
};

byte rowPins[ROWS] = {5, 4, 3, 2}; 
byte colPins[COLS] = {A0, A1, A2, A3}; 

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

char userPassword[4]   = "0000";   
char eepromPassword[4] = "1234";     

bool checkPassword = false;

byte cursorPosition;
char key;

bool errorInput = false;     
byte errorInputCounter = 0;  
byte errorCounter = 0;      

#define relayPin    10

void setup(){
 Serial.begin(9600);
 pinMode(relayPin,OUTPUT);
 digitalWrite(relayPin,LOW);
 Serial.println("-- SYSTEM START ---");
 keypad.setDebounceTime(100);           // Default is 50mS
 cursorPosition = 0;
     
}

void loop(){
  
  key = keypad.getKey();
 
  if(isDigit(key)){

     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     userPassword[cursorPosition] = key;
     cursorPosition++;

       if(cursorPosition >= 4){ 
        
          for(byte i=0; i <= 3; i++){   
            Serial.print("userPassword[" + String(i) + "] ... " + String(userPassword[i]) + " ... " );  
            Serial.print("eepromPassword[" + String(i) + "] ... " + String(eepromPassword[i]) + " ... "); Serial.print("\r\n"); 
          }
          
          cursorPosition = 0;
          
           if(userPassword[0] ==  eepromPassword[0] && userPassword[1] ==  eepromPassword[1] && userPassword[2] ==  eepromPassword[2] && userPassword[3] ==  eepromPassword[3]){
              checkPassword = true;
           }else{
              Serial.println("\r\nerrorInputCounter ---- " + String(errorInputCounter) + " ---- \r\n"); 
              
              errorInputCounter++; 
              if(errorInputCounter >= 3){
                 errorInputCounter = 0;
                 errorInput = true;  
              }  
              checkPassword = false;
              
           }
           
       }
   
  }  // if(isDigit(key)) *************

       
  if(checkPassword == true){
    digitalWrite(relayPin,HIGH);
    Serial.println("\r\n-- RELAY ON --\r\n");
    delay(500);
    errorInputCounter = 0;   
    checkPassword = false;
  }else{
    digitalWrite(relayPin,LOW);
  }

  if(errorInput == true){  // incorrect access codes -- there will be a change
     // leds
     // buzzer and others

    Serial.print("ERROR ... ");
    Serial.println(errorCounter);
    delay(250);
    errorCounter++;
     if(errorCounter >= 16){
        errorInput = false;
     }

  } 
 
 
} // loop *********
4_systemSettingsArduino
#include <Keypad.h>
/*
 Hello 
 I design developer tools for embedded electronic systems. You can buy my projects design files.
 https://www.altiumdesignerprojects.com
*/
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},  
  {'4', '5', '6', 'B'},  
  {'7', '8', '9', 'C'}, 
  {'P', '0', 'E', 'D'}   // P program  E enter
};

byte rowPins[ROWS] = {5, 4, 3, 2}; 
byte colPins[COLS] = {A0, A1, A2, A3}; 

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

char key;

bool systemPassword = true;   // normal section
bool systemSettings = false;  // settings section

bool mainLoop = true;  
bool adminLoop = true;

void setup(){
 Serial.begin(9600);

 keypad.addEventListener(keypadEvent);   // Add an event listener for this keypad
 keypad.setHoldTime(1500);               // 2000mS
  
}

void loop(){
 
 key = keypad.getKey();

 if(systemPassword == true && systemSettings == false){   // main loop
  if(mainLoop == true){
     Serial.println("\r\n--- MAIN LOOP ---\r\n");
     mainLoop = false;
  }

     Serial.println("Password Area");  delay(500);
     
  

 }  // ************************************************  // main loop

 
 if(systemPassword == false && systemSettings == true){   // settings loop
   if(mainLoop == false){
      Serial.println("\r\n--- SETTING LOOP ---\r\n");
      mainLoop = true;
   }
   
   Serial.println("Settings Area");  delay(500);
   
  
 } // ***********************************************    // settings loop
 
} // **************** loop


void keypadEvent(KeypadEvent key){
    switch(keypad.getState()){
        
    case HOLD:
        if(key == 'P'){ 
          systemPassword = false;
          systemSettings = true;
        }
        
        if(key == 'E'){
          systemPassword = true;
          systemSettings = false;
        }
                
        break;
    }

}
5_adminSectionArduino
#include <Keypad.h>
/*
 Hello 
 I design developer tools for embedded electronic systems. You can buy my projects design files.
 https://www.altiumdesignerprojects.com
*/
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},  
  {'4', '5', '6', 'B'},  
  {'7', '8', '9', 'C'}, 
  {'P', '0', 'E', 'D'}   // P program  E enter
};

byte rowPins[ROWS] = {5, 4, 3, 2}; 
byte colPins[COLS] = {A0, A1, A2, A3}; 

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

char key;

bool systemPassword = true;  
bool systemSettings = false; 

bool mainLoop = true;  
bool adminLoop = true;

bool adminSection = true;
bool newPasswordSection = false;

void setup(){
 Serial.begin(9600);

 keypad.addEventListener(keypadEvent);   // Add an event listener for this keypad
 keypad.setHoldTime(1500);               // 2000mS
  
}

void loop(){
 
 key = keypad.getKey();

 if(systemPassword == true && systemSettings == false){   // main loop
  if(mainLoop == true){
     Serial.println("\r\n--- MAIN LOOP ---\r\n");
     mainLoop = false;
  }


  

 }  // ************************************************  // main loop

 if(systemPassword == false && systemSettings == true){  // settings loop
   if(mainLoop == false){
      Serial.println("\r\n--- SETTING LOOP ---\r\n");
      mainLoop = true;
   }
   
   if(adminSection == true && newPasswordSection == false){ // adminSection
    if(adminLoop == true){
       Serial.println("\r\n--- adminSection ---\r\n");
       adminLoop = false;
    }

   // administrator password will be entered


 }  // ------------------------------------------------- // adminSection ends

 
  if(adminSection == false && newPasswordSection == true){ // newPasswordSection
    if(adminLoop == false){
       Serial.println("\r\n--- newPasswordSection ---\r\n");
       adminLoop = true;
    }
  
   // new password creation commands


  

 } // ***********************************************    // newPasswordSection 

 } // ***********************************************    // settings loop
 
} // **************** loop


void keypadEvent(KeypadEvent key){
    switch(keypad.getState()){
        
    case HOLD:
        if(key == 'P'){ 
          systemPassword = false;
          systemSettings = true;
        }
        
        if(key == 'E'){

          // eeprom 
          systemPassword = true;
          systemSettings = false;
        }
                
        break;
    }

}
6_adminPasswordArduino
#include <Keypad.h>
/*
 Hello 
 I design developer tools for embedded electronic systems. You can buy my projects design files.
 https://www.altiumdesignerprojects.com
*/
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},  
  {'4', '5', '6', 'B'},  
  {'7', '8', '9', 'C'}, 
  {'P', '0', 'E', 'D'}   // P program  E enter
};

byte rowPins[ROWS] = {5, 4, 3, 2}; 
byte colPins[COLS] = {A0, A1, A2, A3}; 

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

bool systemPassword = true;        // normal alma
bool systemSettings = false;       // ayarlar ksm

char key;

char userPassword[4]   = "0000";       
char eepromPassword[4] = "1234";   
char adminPassword[4]  = "2356";   

bool checkPassword = false;
bool errorInput = false;

bool adminSection = true;
bool newPasswordSection = false;

bool mainLoop  = true;
bool adminLoop = true;

byte cursorPosition;

byte errorInputCounter = 0;
byte errorCounter = 0;

#define relayPin    10

void setup(){
 Serial.begin(9600);
 pinMode(relayPin,OUTPUT);
 digitalWrite(relayPin,LOW);
 
 keypad.setDebounceTime(100);            // Default is 50mS
 keypad.addEventListener(keypadEvent);   // Add an event listener for this keypad
 keypad.setHoldTime(1500);               // Default is 1000mS

 cursorPosition = 0;
  
}

void loop(){
 
 key = keypad.getKey();

 if(systemPassword == true && systemSettings == false){   // ana dng alan
  if(mainLoop == true){
     Serial.println("\r\n--- MAIN LOOP ---\r\n");
     cursorPosition = 0;
     mainLoop = false;
  }

  if(isDigit(key)){

     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     userPassword[cursorPosition] = key;
     cursorPosition++;

       if(cursorPosition >= 4){ 
        
          for(byte i=0; i <= 3; i++){   
            Serial.print("userPassword[" + String(i) + "] ... " + String(userPassword[i]) + " ... " );  
            Serial.print("eepromPassword[" + String(i) + "] ... " + String(eepromPassword[i]) + " ... "); Serial.print("\r\n"); 
          }
          
          cursorPosition = 0;
          
           if(userPassword[0] ==  eepromPassword[0] && userPassword[1] ==  eepromPassword[1] && userPassword[2] ==  eepromPassword[2] && userPassword[3] ==  eepromPassword[3]){
              checkPassword = true;
           }else{
              Serial.println("\r\nerrorInputCounter .... " + String(errorInputCounter) + " ....\r\n"); 
              errorInputCounter++;
              if(errorInputCounter >= 3){
                 errorInputCounter  = 0;
                 errorInput = true;
              }  
              checkPassword = false;
           }
       }
  }  // if(isDigit(key)) *************
  
  if(checkPassword == true){
    digitalWrite(relayPin,HIGH);
    Serial.println("\r\n-- RELAY ON --\r\n");
    delay(500);
    errorInputCounter = 0;       
    checkPassword = false;
    
  }else{
    digitalWrite(relayPin,LOW);
  }

  if(errorInput == true){  // error 
     // led color
     // buzzer

    Serial.print("ERROR ... ");
    Serial.println(errorCounter);
    delay(250);
    errorCounter++;
     if(errorCounter >= 16){
        errorInput = false;
     }

  } 
    
 } // if(system_password == true && system_settings == false)

 
 // ****************************************


 if(systemPassword == false && systemSettings == true){   // ayarlar ksm  --------------------------------------------
   if(mainLoop == false){
      Serial.println("\r\n--- SETTING LOOP ---\r\n");
      cursorPosition = 0;
      errorInputCounter = 0;
      mainLoop = true;
   }

 if(adminSection == true && newPasswordSection == false){ // admin section -------------------------------
   if(adminLoop == true){
      Serial.println("\r\n--- adminSection ---\r\n");
      adminLoop = false;
   }
   
  if(isDigit(key)){

     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     userPassword[cursorPosition] = key;
     cursorPosition++;
     
       if(cursorPosition >= 4){ 
        
          for(byte i=0; i <= 3; i++){   
            Serial.print("userPassword[" + String(i) + "] ... " + String(userPassword[i]) + " ... " );  
            Serial.println("adminPassword[" + String(i) + "] ... " + String(adminPassword[i]) + " ... ");
          }
          
          cursorPosition = 0;
          
           if(userPassword[0] ==  adminPassword[0] && userPassword[1] ==  adminPassword[1] && userPassword[2] ==  adminPassword[2] && userPassword[3] ==  adminPassword[3]){
              adminSection = false;
              newPasswordSection = true;
           }else{
              Serial.println("\r\nerrorInputCounter .... " + String(errorInputCounter) + " ....\r\n"); 
              errorInputCounter++;
              if(errorInputCounter >= 3){ // if the administrator password is entered incorrectly three times
                 delay(250); 
                 errorInputCounter = 0;
                 
                 systemPassword = true;      
                 systemSettings = false; 
                 
                 mainLoop = true; 
                 adminLoop = true;         
                 
                 adminSection = true;       
                 newPasswordSection = false; 

              }  
           }  //  if(password[0]  ******** sonu
       }
   }  // if(isDigit(key)) *************
 }  //  if(adminSection == true && newPasswordSection == false)     sonu

  
  // ------------------------------------
  // newPasswordSection
  
  if(adminSection == false && newPasswordSection == true){
    if(adminLoop == false){
       Serial.println("\r\n--- newPasswordSection ---\r\n");
       adminLoop = true;
    }
  
    // after

  

  }

  
 }// if(system_password == false && system_settings == true)   // ayarlar ksm sonu

 
} // **************** loop

void keypadEvent(KeypadEvent key){
    switch (keypad.getState()){
       
    case HOLD:
        if (key == 'P') {   // settings 
          systemPassword = false;
          systemSettings = true;
        }
        
        if(key == 'E') {   // enter button
          
          adminSection = true;       
          newPasswordSection = false;
           
          systemPassword = true;
          systemSettings = false;
        }
                
        break;
    }

}
7_newPasswordArduino
#include <Keypad.h>
#include <EEPROM.h>
/*
 Hello 
 I design developer tools for embedded electronic systems. You can buy my projects design files.
 https://www.altiumdesignerprojects.com
*/

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},  
  {'4', '5', '6', 'B'},  
  {'7', '8', '9', 'C'}, 
  {'P', '0', 'E', 'D'}   // P program  E enter
};

byte rowPins[ROWS] = {5, 4, 3, 2}; 
byte colPins[COLS] = {A0, A1, A2, A3}; 

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

bool systemPassword = true;        // normal alma
bool systemSettings = false;       // ayarlar ksm

char key;

char userPassword[4]   = "0000";       
char eepromPassword[4] = "0000";  // sfr deerini ykledik 
char adminPassword[4]  = "2356"; 
char newPassword[4]    = "0000";  // yeni oluturuldu 

bool checkPassword = false;
bool errorInput = false;

bool adminSection = true;
bool newPasswordSection = false;

bool mainLoop  = true;
bool adminLoop = true;

byte cursorPosition;
byte errorInputCounter = 0;
byte errorCounter = 0; 

bool eepromControl = true;
bool newPasswordInput = true;  

#define relayPin    10

void setup(){
 Serial.begin(9600);
 pinMode(relayPin,OUTPUT);
 digitalWrite(relayPin,LOW);
 
 keypad.setDebounceTime(100);            // Default is 50mS
 keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
 keypad.setHoldTime(1000);               // Default is 1000mS
 
 cursorPosition = 0;
  
}

void loop(){
  
 if(eepromControl == true){   
  if(EEPROM.read(4) == 120 && EEPROM.read(5) == 160 ){    // yeni ifre girilmi mi?  // is a new password created?
     eepromPassword[0] = EEPROM.read(0);  
     eepromPassword[1] = EEPROM.read(1);
     eepromPassword[2] = EEPROM.read(2);
     eepromPassword[3] = EEPROM.read(3);
  }else{
     strcpy(eepromPassword , "1234");     // default password
  }
   eepromControl = false;
 }
 
 key = keypad.getKey();

 if(systemPassword == true && systemSettings == false){   // ana dng alan
  if(mainLoop == true){
     Serial.println("\r\n--- MAIN LOOP ---\r\n");
     cursorPosition = 0;
     mainLoop = false;
  }

  if(isDigit(key)){

     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     userPassword[cursorPosition] = key;
     cursorPosition++;

       if(cursorPosition >= 4){ 
        
          for(byte i=0; i <= 3; i++){   
            Serial.print("userPassword[" + String(i) + "] ... " + String(userPassword[i]) + " ... " );  
            Serial.print("eepromPassword[" + String(i) + "] ... " + String(eepromPassword[i]) + " ... "); Serial.print("\r\n"); 
          }
          
          cursorPosition = 0;
          
           if(userPassword[0] ==  eepromPassword[0] && userPassword[1] ==  eepromPassword[1] && userPassword[2] ==  eepromPassword[2] && userPassword[3] ==  eepromPassword[3]){
              checkPassword = true;
           }else{
              Serial.println("\r\nerrorInputCounter .... " + String(errorInputCounter) + " ....\r\n"); 
              errorInputCounter++;
              if(errorInputCounter >= 3){
                 errorInputCounter  = 0; 
                 errorInput = true;
              }  
              checkPassword = false;
           }
           
       }
   
  }  // if(isDigit(key)) *************
  
  if(checkPassword == true){
    digitalWrite(relayPin,HIGH);
    Serial.println("\r\n-- RELAY ON --\r\n");
    delay(500);
    mainLoop = true; 
    checkPassword = false;
    
  }else{
    digitalWrite(relayPin,LOW);
  }

  if(errorInput == true){  // error
     // led color
     // buzzer

    Serial.print("ERROR ... ");
    Serial.println(errorCounter);
    delay(250);
    errorCounter++;
     if(errorCounter >= 16){
        errorInput = false;
     }

  } 
    
 } // if(system_password == tr dng alan sonu


 if(systemPassword == false && systemSettings == true){   // ayarlar ksm  --------------------------------------------
   if(mainLoop == false){
      Serial.println("\r\n--- SETTING LOOP ---\r\n");
      cursorPosition = 0;
      errorInputCounter = 0;
      mainLoop = true;
   }
  
  if(adminSection == true && newPasswordSection == false){  // admin section -------------------------------
   if(adminLoop == true){
      Serial.println("\r\n--- adminSection ---\r\n");
      cursorPosition = 0;
      adminLoop = false;
   }
   
  if(isDigit(key)){

     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     userPassword[cursorPosition] = key;
     cursorPosition++;
     
       if(cursorPosition >= 4){
        
          for(byte i=0; i <= 3; i++){   
            Serial.print("userPassword[" + String(i) + "] ... " + String(userPassword[i]) + " ... " );  
            Serial.println("adminPassword[" + String(i) + "] ... " + String(adminPassword[i]) + " ... ");
          } 
          
          cursorPosition = 0;
          
           if(userPassword[0] ==  adminPassword[0] && userPassword[1] ==  adminPassword[1] && userPassword[2] ==  adminPassword[2] && userPassword[3] ==  adminPassword[3]){
              adminSection = false;
              newPasswordSection = true;
           }else{
              Serial.println("\r\nerrorInputCounter .... " + String(errorInputCounter) + " ....\r\n"); 
              errorInputCounter++;
              if(errorInputCounter >= 3){ // 0 1 2 3
                 delay(250); 
                 errorInputCounter = 0;

                 systemPassword = true;      
                 systemSettings = false; 
                 
                 mainLoop = true; 
                 adminLoop = true;         
                 
                 adminSection = true;       
                 newPasswordSection = false; 
       
              }  
           }  //  if(password[0]  ******** sonu
           
       }
   
  }  // if(isDigit(key)) *************
 
  }
  
  
  // new paswords section
  
  if(adminSection == false && newPasswordSection == true){
    if(adminLoop == false){
       Serial.println("\r\n--- newPasswordSection ---\r\n");
       cursorPosition = 0;
       key = 0;
       adminLoop = true;
    }
    
    if(isDigit(key)){

     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     newPassword[cursorPosition] = key; 
     cursorPosition++;

       if(cursorPosition >= 4){ 

          for(byte i=0; i <= 3; i++){   
            Serial.println("newPassword[" + String(i) + "] ... " + String(newPassword[i]) + " ... " );  
          } 
          Serial.println("\r\n--- Press Enter ---\r\n"); 
          
          cursorPosition = 0;
          newPasswordInput = true;  
           
       }
   
    }  // if(isDigit(key)) *************

  } // if(ad  --------------------
  
 }// if(sys  // ayarlar ksm sonu

 
} // ana dng sonu

void keypadEvent(KeypadEvent key){
    switch (keypad.getState()){
        
    case HOLD:
        if(key == 'P') {     // settings program
           systemPassword = false;
           systemSettings = true;
        }
        
        if(key == 'E') { // enter button
          if(newPasswordInput == true){
                  
             EEPROM.write(0,newPassword[0]);    
             EEPROM.write(1,newPassword[1]); 
             EEPROM.write(2,newPassword[2]); 
             EEPROM.write(3,newPassword[3]); 

             EEPROM.write(4,120);
             EEPROM.write(5,160);

             delay(100);      
             Serial.println("\r\n--- NEW PASSWORD OK ---\r\n");
             
             newPasswordInput = false;            
             eepromControl = true;
             
             adminSection = true;       
             newPasswordSection = false;
             systemPassword = true;
             systemSettings = false;
    
          }

        }
                
        break;
    }

}
8_timerStartArduino
#include <Keypad.h>
#include <EEPROM.h>
/*
 Hello 
 I design developer tools for embedded electronic systems. You can buy my projects design files.
 https://www.altiumdesignerprojects.com
*/

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},  
  {'4', '5', '6', 'B'},  
  {'7', '8', '9', 'C'}, 
  {'P', '0', 'E', 'D'}   // P program  E enter
};

byte rowPins[ROWS] = {5, 4, 3, 2}; 
byte colPins[COLS] = {A0, A1, A2, A3}; 

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

bool systemPassword = true;        // normal alma
bool systemSettings = false;       // ayarlar ksm

char key;

char userPassword[4]   = "0000";       
char eepromPassword[4] = "0000";   
char adminPassword[4]  = "2356"; 
char newPassword[4]    = "0000";    

bool checkPassword = false;
bool eepromControl = true;

bool adminSection = true;
bool newPasswordSection = false;

bool mainLoop  = true;
bool adminLoop = true;

byte cursorPosition;

bool newPasswordInput = true; 

bool errorInput = false;
byte errorInputCounter = 0;
byte errorCounter = 0;

bool timerStart = false;

byte Second;
byte lastSecond;

#define relayPin    10

void setup(){
 Serial.begin(9600);
 pinMode(relayPin,OUTPUT);
 digitalWrite(relayPin,LOW);
 
 keypad.addEventListener(keypadEvent);    // Add an event listener for this keypad
 keypad.setHoldTime(1500);                // Default is 1000mS
 keypad.setDebounceTime(100);             // Default is 50mS
 
 cursorPosition = 0;


 noInterrupts();      // disable all interrupts
                // Timer1 kesmesi ayarlanyor
  TCNT1  = 0;   // initialize counter value to 0
  TCCR1A = 0;   // same for TCCR1B
  TCCR1B = 0;   // set entire TCCR1A register to 0
  OCR1A = 15624;  // set compare match register for 1hz increments // = (16*10^6) / (1*1024) - 1 ( must be < 65536 )
  TCCR1B |= (1 << WGM12);               // turn on CTC mode   
  TCCR1B |= (1 << CS12) | (1 << CS10);  // Set CS10 and CS12 bits for 1024 prescaler
  TIMSK1 |= (1 << OCIE1A);              // enable timer compare interrupt
 
 interrupts();        // enable
 
}

void loop(){
  
 if(eepromControl == true){    // led lerin bir defa almas gerekiyor
  if(EEPROM.read(4) == 160 && EEPROM.read(5) == 140 ){    // yeni ifre girilmi mi?  
     eepromPassword[0] = EEPROM.read(0);  
     eepromPassword[1] = EEPROM.read(1);
     eepromPassword[2] = EEPROM.read(2);
     eepromPassword[3] = EEPROM.read(3);
  }else{
     strcpy(eepromPassword , "1234");     // default password
  }
   eepromControl = false;
 }
 
 key = keypad.getKey();

 if(systemPassword == true && systemSettings == false){   // ana dng alan
  if(mainLoop == true){
     Serial.println("\r\n--- MAIN LOOP ---\r\n");
     timerStart = false;    
     Second = 0;           
     cursorPosition = 0;
     mainLoop = false;
  }

  if(isDigit(key)){
     
     timerStart = true;  
     
     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     userPassword[cursorPosition] = key;
     cursorPosition++;

       if(cursorPosition >= 4){ 
        
          for(byte i=0; i <= 3; i++){   
            Serial.print("userPassword[" + String(i) + "] ... " + String(userPassword[i]) + " ... " );  
            Serial.print("eepromPassword[" + String(i) + "] ... " + String(eepromPassword[i]) + " ... "); Serial.print("\r\n"); 
          }
          
          cursorPosition = 0;
          
           if(userPassword[0] ==  eepromPassword[0] && userPassword[1] ==  eepromPassword[1] && userPassword[2] ==  eepromPassword[2] && userPassword[3] ==  eepromPassword[3]){
              checkPassword = true;
           }else{
              Serial.println("\r\nerrorInputCounter .... " + String(errorInputCounter) + " ....\r\n"); 
              errorInputCounter++;
              if(errorInputCounter >= 3){
                 errorInputCounter = 0; 
                 Second = 0;
                 timerStart = true;   
                 errorInput = true;
              }  
              checkPassword = false;
           }
       }
  } 
  
  if(checkPassword == true){
    digitalWrite(relayPin,HIGH);
    Serial.println("\r\n-- RELAY ON --\r\n");
    delay(500);
    errorInputCounter = 0;   
    timerStart = false;     
    Second = 0;               
    digitalWrite(relayPin,LOW);
    checkPassword = false;
  }else{
    digitalWrite(relayPin,LOW);
  }
   // ////////////////

   if(timerStart == true && errorInput == true){    // ifre  defa yanl girildi ise 
     if(Second >= 30){  // 30
        Serial.println("\t\t\t\t\t\tTimer >>> Error Input Done");
        errorInput = false;     
        timerStart = false;
        Second = 0;
        systemPassword = true;   // return main loop
        systemSettings = false;
        mainLoop = true; 
        digitalWrite(relayPin,LOW); // gvenlik amacyla
     }
   }

   if(timerStart == true && errorInput == false){      // butona basld m timer al
      if(Second >= 10){  // 16 
         Serial.println("\t\t\t\t\t\tTimer >>> Button Input Done");
         timerStart = false;
         Second = 0;
         systemPassword = true;
         systemSettings = false;
         mainLoop = true; 
      }
   }  // if(timerStart == true && errorInput == false) *************
   
 } // if(system_password == true && system_settings == false){   // ana dng alan sonu

 if(systemPassword == false && systemSettings == true){   // ayarlar ksm  -----------------------------------
   if(mainLoop == false){
      Serial.println("\r\n--- SETTING LOOP ---\r\n");
      mainLoop = true;
   }

  if(adminSection == true && newPasswordSection == false){  // admin section -------------------------------
   if(adminLoop == true){
      Serial.println("\r\n--- adminSection ---\r\n");
      errorInputCounter = 0;
      cursorPosition = 0;
      Second = 0;  
      timerStart = true;  
      adminLoop = false;
   }
   
  if(isDigit(key)){

     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     userPassword[cursorPosition] = key;
     cursorPosition++;
     
       if(cursorPosition >= 4){ // 0 1 2 3 
        
          for(byte i=0; i <= 3; i++){   
            Serial.print("userPassword[" + String(i) + "] ... " + String(userPassword[i]) + " ... " );  
            Serial.println("adminPassword[" + String(i) + "] ... " + String(adminPassword[i]) + " ... ");
          } 
          
          cursorPosition = 0;
          
           if(userPassword[0] ==  adminPassword[0] && userPassword[1] ==  adminPassword[1] && userPassword[2] ==  adminPassword[2] && userPassword[3] ==  adminPassword[3]){
              adminSection = false;
              newPasswordSection = true;
           }else{
              Serial.println("\r\nerrorInputCounter .... " + String(errorInputCounter) + " ....\r\n"); 
              errorInputCounter++;
              if(errorInputCounter >= 3){ 
                 delay(250); 
                 errorInputCounter = 0;
                 
                 systemPassword = true;      
                 systemSettings = false; 
                 
                 mainLoop = true; 
                 adminLoop = true;         
                 
                 adminSection = true;       
                 newPasswordSection = false; 
              }  
           }  
       }  
  } //  if(isDigit(key))   


   if(timerStart == true){  // timerStart == true && adminSection == true
    if(Second >= 16){      // 10
        Serial.println("\t\t\t\t\t\tTimer >>> adminSection Done");
        timerStart = false;
        Second = 0;
        systemPassword = true;  // main loop dn
        systemSettings = false;
        mainLoop = true; 
        adminLoop = true;
    }
   }  // if(timerStart == true) ***********
   
  } // if(adminSection == true && newPasswordSection == false) *********
  
  
  if(adminSection == false && newPasswordSection == true){   // new paswords section -------------
    if(adminLoop == false){
       Serial.println("\r\n--- newPasswordSection ---\r\n");
       cursorPosition = 0;
       key = 0;
       Second = 0;
       timerStart = true;
       adminLoop = true;
    }
    
    if(isDigit(key)){

     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     newPassword[cursorPosition] = key;
     cursorPosition++;

       if(cursorPosition >= 4){ 
          for(byte i=0; i <= 3; i++){ Serial.println("newPassword[" + String(i) + "] ... " + String(newPassword[i]) + " ... " ); } 
          Serial.println("\r\n--- Press Enter ---\r\n"); 
          cursorPosition = 0;
          newPasswordInput = true;
           
       } //  if(cursorPosition >= 4)****
    }  // if(isDigit(key)) *************

   
   // ---------------------
   if(timerStart == true){
    if(Second >= 16){  // 16
        Serial.println("\t\t\t\t\t\tTimer >>> newPasswordSection Done");
        newPasswordSection = false;     
        timerStart = false;
        Second = 0;

        newPasswordInput = false;   
           
        systemPassword = true;      // return main loop 
        systemSettings = false;
       
        mainLoop = true;      
        adminLoop = true;
        
        adminSection = true;       
        newPasswordSection = false; 
           
    }
   }  // if(timerStart == true) **************


   
  } // if(adminSection == false && newPasswordSection == true)  **********
 }// if(system_password == false && system_settings == true)*******  ayarlar ksm son 

 
} // ana dng sonu

          
void keypadEvent(KeypadEvent key){
    switch (keypad.getState()){
        
    case HOLD:
        if(key == 'P') {     // settings program
           systemPassword = false;
           systemSettings = true;
        }
        
        if(key == 'E') { // enter button
          if(newPasswordInput == true){
             EEPROM.write(0,newPassword[0]);    
             EEPROM.write(1,newPassword[1]); 
             EEPROM.write(2,newPassword[2]); 
             EEPROM.write(3,newPassword[3]); 

             EEPROM.write(4,160);
             EEPROM.write(5,140);

             delay(250);     
             Serial.println("\r\n--- NEW PASSWORD OK ---\r\n");
             newPasswordInput = false; 
             eepromControl = true;           
             
             adminSection = true;       
             newPasswordSection = false; 
             
             systemPassword = true;
             systemSettings = false;
             Serial.println("\r\n--- NEW PASSWORD OK ---\r\n");  
          }

        }
                
        break;
    }

}

// ----------  Functions --------------------

ISR(TIMER1_COMPA_vect){   
 
 if(timerStart == true){ 
    Second++; 
   
    if(Second != lastSecond){ 
      Serial.print("\t\t\t\t\t\tSecond .... ");  Serial.println(Second);
    }
    lastSecond = Second; 
 
 }

}  // ISR(TIMER1_COMPA_vect)  ********
9_buttonsBuzzerArduino
#include <Keypad.h>
#include <EEPROM.h>
/*
 Hello 
 I design developer tools for embedded electronic systems. You can buy my projects design files.
 https://www.altiumdesignerprojects.com
*/
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},  
  {'4', '5', '6', 'B'},  
  {'7', '8', '9', 'C'}, 
  {'P', '0', 'E', 'D'}   // P program  E enter
};

byte rowPins[ROWS] = {5, 4, 3, 2}; 
byte colPins[COLS] = {A0, A1, A2, A3}; 

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

bool systemPassword = true;        // normal alma
bool systemSettings = false;       // ayarlar ksm

char key;

char userPassword[4]   = "0000";       
char eepromPassword[4] = "0000";   
char adminPassword[4]  = "2356"; 
char newPassword[4]    = "0000";    

bool checkPassword = false;

bool adminSection = true;
bool newPasswordSection = false;

bool mainLoop  = true;
bool adminLoop = true;
bool eepromControl = true;

byte cursorPosition;

bool newPasswordInput = true; 

bool errorInput = false;
byte errorInputCounter = 0;
byte errorCounter = 0;

bool timerStart = false;

byte Second;
byte lastSecond;

#define relayPin    10
#define buzzerPin   9

void setup(){
 Serial.begin(9600);
 pinMode(relayPin,OUTPUT);
 digitalWrite(relayPin,LOW);
 
 pinMode(buzzerPin, OUTPUT);
 
 keypad.addEventListener(keypadEvent);    // Add an event listener for this keypad
 keypad.setHoldTime(1000);                // Default is 1000mS
 keypad.setDebounceTime(100);           // Default is 50mS

 cursorPosition = 0;


 noInterrupts();      // disable all interrupts
                // Timer1 kesmesi ayarlanyor
  TCNT1  = 0;   // initialize counter value to 0
  TCCR1A = 0;   // same for TCCR1B
  TCCR1B = 0;   // set entire TCCR1A register to 0
  
  OCR1A = 15624;  // set compare match register for 1hz increments // = (16*10^6) / (1*1024) - 1 ( must be < 65536 )
  TCCR1B |= (1 << WGM12);               // turn on CTC mode   
  TCCR1B |= (1 << CS12) | (1 << CS10);  // Set CS10 and CS12 bits for 1024 prescaler
  TIMSK1 |= (1 << OCIE1A);              // enable timer compare interrupt
 
 interrupts();        // enable
 
}

void loop(){
 if(eepromControl == true){  
  if(EEPROM.read(4) == 160 && EEPROM.read(5) == 140 ){   
     eepromPassword[0] = EEPROM.read(0);  
     eepromPassword[1] = EEPROM.read(1);
     eepromPassword[2] = EEPROM.read(2);
     eepromPassword[3] = EEPROM.read(3);
  }else{
     strcpy(eepromPassword , "1234");     // default password
  }
   eepromControl = false;
 }
 
 key = keypad.getKey();

 if(systemPassword == true && systemSettings == false){   // ana dng alan
  if(mainLoop == true){
     Serial.println("\r\n--- MAIN LOOP ---\r\n");
     timerStart = false;    
     Second = 0;           
     cursorPosition = 0;
     mainLoop = false;
  }

  if(isDigit(key)){
     buttonsBipBuzzer();
     timerStart = true;  
     
     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     userPassword[cursorPosition] = key;
     cursorPosition++;

       if(cursorPosition >= 4){ 
        
          for(byte i=0; i <= 3; i++){   
            Serial.print("userPassword[" + String(i) + "] ... " + String(userPassword[i]) + " ... " );  
            Serial.println("eepromPassword[" + String(i) + "] ... " + String(eepromPassword[i]) + " ... ");
          }
          
          cursorPosition = 0;
          
           if(userPassword[0] ==  eepromPassword[0] && userPassword[1] ==  eepromPassword[1] && userPassword[2] ==  eepromPassword[2] && userPassword[3] ==  eepromPassword[3]){
              checkPassword = true;
           }else{
              Serial.println("\r\nerrorInputCounter .... " + String(errorInputCounter) + " ....\r\n"); 
              errorInputCounter++;
              if(errorInputCounter >= 3){
                 errorInputCounter = 0; 
                 Second = 0;
                 timerStart = true;   
                 errorInput = true;
              }  
              checkPassword = false;
           }
       }
  } 
  
  if(checkPassword == true){
    digitalWrite(relayPin,HIGH);
    Serial.println("\r\n-- RELAY ON --\r\n");
    //delay(250);
    relayOnBuzzer();
    digitalWrite(relayPin,LOW);    
    errorInputCounter = 0;   
    timerStart = false;     
    Second = 0;               
    checkPassword = false;
  }else{
    digitalWrite(relayPin,LOW);
  }
   // ////////////////
   
   if(timerStart == true && errorInput == false){      // butona basld m timer al
      if(Second >= 16){  // 12 
         Serial.println("\t\t\t\t\t\tTimer >>> Button Input Done");
         errorInput = false;     
         timerStart = false;
         Second = 0;
         systemPassword = true;
         systemSettings = false;
         mainLoop = true; 
      }
   }  // if(timerStart == true && errorInput == false) *************

   if(timerStart == true && errorInput == true){    // ifre  defa yanl girildi ise 
     passwordAlarmBuzzer();
     digitalWrite(relayPin,LOW); // gvenlik amacyla
     if(Second >= 30){  // 30
        Serial.println("\t\t\t\t\t\tTimer >>> Error Input Done");
        errorInput = false;     
        timerStart = false;
        Second = 0;
        systemPassword = true;   // return main loop
        systemSettings = false;
        mainLoop = true; 
     }
   }

 } // if(system_password == true && system_settings == false){   // ana dng alan sonu

 if(systemPassword == false && systemSettings == true){   // ayarlar ksm  -----------------------------------
   if(mainLoop == false){
      Serial.println("\r\n--- SETTING LOOP ---\r\n");
      mainLoop = true;
   }

  if(adminSection == true && newPasswordSection == false){  // admin section -------------------------------
   if(adminLoop == true){
      Serial.println("\r\n--- adminSection ---\r\n");
      errorInputCounter = 0;
      cursorPosition = 0;
      Second = 0;  
      timerStart = true;  
      adminLoop = false;
   }
   
  if(isDigit(key)){
     buttonsBipBuzzer();
     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     userPassword[cursorPosition] = key;
     cursorPosition++;
     
       if(cursorPosition >= 4){ // 0 1 2 3 
        
          for(byte i=0; i <= 3; i++){   
            Serial.print("userPassword[" + String(i) + "] ... " + String(userPassword[i]) + " ... " );  
            Serial.println("adminPassword[" + String(i) + "] ... " + String(adminPassword[i]) + " ... ");
          } 
          
          cursorPosition = 0;
          
           if(userPassword[0] ==  adminPassword[0] && userPassword[1] ==  adminPassword[1] && userPassword[2] ==  adminPassword[2] && userPassword[3] ==  adminPassword[3]){
              adminSection = false;
              newPasswordSection = true;
           }else{
              Serial.println("\r\nerrorInputCounter .... " + String(errorInputCounter) + " ....\r\n"); 
              errorInputCounter++;
              if(errorInputCounter >= 3){ 
                 delay(250); 
                 errorInputCounter = 0;
                 
                 adminLoop = true;          
                 mainLoop = true;
                 
                 adminSection = true;         
                 newPasswordSection = false;  
                 
                 systemPassword = true;
                 systemSettings = false;
              }  
           }  
       }  
  } //  if(isDigit(key))   


   if(timerStart == true){  // timerStart == true && adminSection == true
    if(Second >= 10){      // 10
        Serial.println("\t\t\t\t\t\tTimer >>> adminSection Done");
        timerStart = false;
        Second = 0;
        systemPassword = true;  // main loop dn
        systemSettings = false;
        adminLoop = true;
        mainLoop = true;        
    }
   }  // if(timerStart == true) ***********
  } // if(adminSection == true && newPasswordSection == false) *********
  
  
  if(adminSection == false && newPasswordSection == true){   // new paswords section -------------
    if(adminLoop == false){
       Serial.println("\r\n--- newPasswordSection ---\r\n");
       cursorPosition = 0;
       key = 0;
       Second = 0;
       timerStart = true;
       adminLoop = true;
    }
    
    if(isDigit(key)){
     buttonsBipBuzzer();
     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     newPassword[cursorPosition] = key;
     cursorPosition++;

       if(cursorPosition >= 4){ 
          for(byte i=0; i <= 3; i++){ Serial.println("newPassword[" + String(i) + "] ... " + String(newPassword[i]) + " ... " ); } 
          Serial.println("\r\n--- Press Enter ---\r\n"); 
          cursorPosition = 0;
          newPasswordInput = true;
           
       } //  if(cursorPosition >= 4)****
    }  // if(isDigit(key)) *************

   
   // ---------------------
   if(timerStart == true){
    if(Second >= 16){  // 16
        Serial.println("\t\t\t\t\t\tTimer >>> newPasswordSection Done");
        newPasswordSection = false;     
        timerStart = false;
        Second = 0;

        newPasswordInput = false;   
           
        systemPassword = true;      // return main loop 
        systemSettings = false;
           
        mainLoop = true;    
        adminLoop = true;

        adminSection = true;       
        newPasswordSection = false; 
    }
   }  // if(timerStart == true) **************
  } // if(adminSection == false && newPasswordSection == true)  **********
 }// if(system_password == false && system_settings == true)*******  ayarlar ksm son 

 
} // ana dng sonu

          
void keypadEvent(KeypadEvent key){
    switch (keypad.getState()){
        
    case HOLD:
        if(key == 'P') {     // settings program
           systemPassword = false;
           systemSettings = true;
        }
        
        if(key == 'E') { // enter button
          if(newPasswordInput == true){
             EEPROM.write(0,newPassword[0]);    
             EEPROM.write(1,newPassword[1]); 
             EEPROM.write(2,newPassword[2]); 
             EEPROM.write(3,newPassword[3]); 

             EEPROM.write(4,150);
             EEPROM.write(5,180);

             passwordRecordBuzzer();
             newPasswordInput = false;  
             eepromControl = true;          
             
             adminSection = true;       
             newPasswordSection = false; 
             
             systemPassword = true;
             systemSettings = false;
             Serial.println("\r\n--- NEW PASSWORD OK ---\r\n");       

          }

        }
                
        break;
    }

}

// ----------  Functions --------------------

ISR(TIMER1_COMPA_vect){   
 
 if(timerStart == true){ 
    Second++; 
   
    if(Second != lastSecond){ 
      Serial.print("\t\t\t\t\t\tSecond .... ");  Serial.println(Second);
    }
    lastSecond = Second; 
 
 }

}  // ISR(TIMER1_COMPA_vect)  ********


void buttonsBipBuzzer(){
  tone(buzzerPin, 1500, 50);
}

void relayOnBuzzer(){
  tone(buzzerPin, 1500, 50);
  delay(250); 
  tone(buzzerPin, 1500, 50);
  delay(250);
}

void passwordRecordBuzzer(){
  tone(buzzerPin,2000,50);
  delay(250);   
  tone(buzzerPin,2000,50);
  delay(1000);
  tone(buzzerPin,1500,60);
  delay(250);   
  tone(buzzerPin,1500,60);
  delay(1000); 
}

void passwordAlarmBuzzer(){
  tone(buzzerPin,2000,50);
  delay(250);
  tone(buzzerPin,2000,50);
  delay(750);
  tone(buzzerPin,1500,60);
  delay(150);
  tone(buzzerPin,1500,60);
  delay(150);
  tone(buzzerPin,1500,60);
  delay(750); 
}
10_Keypad_Lock_ProjectArduino
#include <Keypad.h>
#include <EEPROM.h>
#include <Adafruit_NeoPixel.h>
/*
 Hello 
 I design developer tools for embedded electronic systems. You can buy my projects design files.
 https://www.altiumdesignerprojects.com
*/
#define PIXEL_PIN    11  // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT  1  // Number of NeoPixels

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},  
  {'4', '5', '6', 'B'},  
  {'7', '8', '9', 'C'}, 
  {'P', '0', 'E', 'D'}   // P program  E enter
};

byte rowPins[ROWS] = {5, 4, 3, 2}; 
byte colPins[COLS] = {A0, A1, A2, A3}; 

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

bool systemPassword = true;        // normal alma
bool systemSettings = false;       // ayarlar ksm

char key;

char userPassword[4]   = "0000";       
char eepromPassword[4] = "0000";   
char adminPassword[4]  = "2356"; 
char newPassword[4]    = "0000";    

bool checkPassword = false;

bool adminSection = true;
bool newPasswordSection = false;

bool mainLoop  = true;
bool adminLoop = true;

byte cursorPosition;

bool newPasswordInput = true; 

bool errorInput = false;
byte errorInputCounter = 0;
byte errorCounter = 0;

bool eepromControl = true;

bool timerStart = false;

byte Second;
byte lastSecond;

#define relayPin    10
#define buzzerPin   9

void setup(){
 Serial.begin(9600);
 pinMode(relayPin,OUTPUT);
 digitalWrite(relayPin,LOW);
 
 pinMode(buzzerPin, OUTPUT);

 strip.begin(); 
 strip.show();
 
 keypad.addEventListener(keypadEvent);    // Add an event listener for this keypad
 keypad.setHoldTime(1000);                // Default is 1000mS
 keypad.setDebounceTime(100);             // Default is 50mS

 cursorPosition = 0;

 noInterrupts();      // disable all interrupts
                // Timer1 kesmesi ayarlanyor
  TCNT1  = 0;   // initialize counter value to 0
  TCCR1A = 0;   // same for TCCR1B
  TCCR1B = 0;   // set entire TCCR1A register to 0
  OCR1A = 15624;  // set compare match register for 1hz increments // = (16*10^6) / (1*1024) - 1 ( must be < 65536 )
  TCCR1B |= (1 << WGM12);               // turn on CTC mode   
  TCCR1B |= (1 << CS12) | (1 << CS10);  // Set CS10 and CS12 bits for 1024 prescaler
  TIMSK1 |= (1 << OCIE1A);              // enable timer compare interrupt
 
 interrupts();        // enable
 
}

void loop(){
  
 if(eepromControl == true){   
  if(EEPROM.read(4) == 120 && EEPROM.read(5) == 160 ){    // yeni ifre girilmi mi?  // is a new password created?
     eepromPassword[0] = EEPROM.read(0);  
     eepromPassword[1] = EEPROM.read(1);
     eepromPassword[2] = EEPROM.read(2);
     eepromPassword[3] = EEPROM.read(3);
  }else{
     strcpy(eepromPassword , "1234");     // default password
  }
   eepromControl = false;
 }
     
 key = keypad.getKey();

 if(systemPassword == true && systemSettings == false){   // ana dng alan
  if(mainLoop == true){
     Serial.println("\r\n--- MAIN LOOP ---\r\n");
     timerStart = false;    
     Second = 0;           
     cursorPosition = 0;
     fadeColorChance(0, 0, 255);    // blue
     mainLoop = false;
  }

  if(isDigit(key)){
     buttonsBipBuzzer();
     timerStart = true;  
     
     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     userPassword[cursorPosition] = key;
     cursorPosition++;

       if(cursorPosition >= 4){ 
        
          for(byte i=0; i <= 3; i++){   
            Serial.print("userPassword[" + String(i) + "] ... " + String(userPassword[i]) + " ... " );  
            Serial.print("eepromPassword[" + String(i) + "] ... " + String(eepromPassword[i]) + " ... "); Serial.print("\r\n"); 
          }
          
          cursorPosition = 0;
          
           if(userPassword[0] ==  eepromPassword[0] && userPassword[1] ==  eepromPassword[1] && userPassword[2] ==  eepromPassword[2] && userPassword[3] ==  eepromPassword[3]){
              checkPassword = true;
           }else{
              Serial.println("\r\nerrorInputCounter .... " + String(errorInputCounter) + " ....\r\n"); 
              errorInputCounter++;
              if(errorInputCounter >= 3){
                 errorInputCounter = 0; 
                 Second = 0;
                 timerStart = true;   
                 errorInput = true;
              }  
              checkPassword = false;
           }
       }
  } 
  
  if(checkPassword == true){
    digitalWrite(relayPin,HIGH);
    fadeColorChance(0, 255, 0); // gr
    Serial.println("\r\n-- RELAY ON --\r\n");
    relayOnBuzzer();
    digitalWrite(relayPin,LOW);    
    errorInputCounter = 0;   
    timerStart = false;     
    Second = 0;    
    mainLoop = true;
    checkPassword = false;
  }else{
    digitalWrite(relayPin,LOW);
  }
   // ////////////////
   
   if(timerStart == true && errorInput == false){      // butona basld m timer al

      if(Second >= 15){  // 12 
         Serial.println("\t\t\t\t\t\tTimer >>> Button Input Done");
         timerStart = false;
         Second = 0;
         systemPassword = true;
         systemSettings = false;
         mainLoop = true; 
      }
   }  // if(timerStart == true && errorInput == false) *************

   if(timerStart == true && errorInput == true){    // ifre  defa yanl girildi ise 
     passwordAlarmBuzzer();
     fadeColorChance(255, 0, 0); 
     digitalWrite(relayPin,LOW); // gvenlik amacyla
     if(Second >= 20){  // 30
        Serial.println("\t\t\t\t\t\tTimer >>> Error Input Done");
        errorInput = false;     
        timerStart = false;
        Second = 0;
        systemPassword = true;   // return main loop
        systemSettings = false;
        mainLoop = true; 
     }
   }

 } // if(system_password == true && system_settings == false){   // ana dng alan sonu

 if(systemPassword == false && systemSettings == true){   // ayarlar ksm  -----------------------------------
   if(mainLoop == false){
      Serial.println("\r\n--- SETTING LOOP ---\r\n");
      mainLoop = true;
   }

  if(adminSection == true && newPasswordSection == false){  // admin section -------------------------------
   if(adminLoop == true){
      fadeColorChance(255, 255, 0); 
      Serial.println("\r\n--- adminSection ---\r\n");
      errorInputCounter = 0;
      cursorPosition = 0;
      Second = 0;  
      timerStart = true;  
      adminLoop = false;
   }
   
  if(isDigit(key)){
     buttonsBipBuzzer();
     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     userPassword[cursorPosition] = key;
     cursorPosition++;
     
       if(cursorPosition >= 4){ // 0 1 2 3 
        
          for(byte i=0; i <= 3; i++){   
            Serial.print("userPassword[" + String(i) + "] ... " + String(userPassword[i]) + " ... " );  
            Serial.println("adminPassword[" + String(i) + "] ... " + String(adminPassword[i]) + " ... ");
          } 
          
          cursorPosition = 0;
          
           if(userPassword[0] ==  adminPassword[0] && userPassword[1] ==  adminPassword[1] && userPassword[2] ==  adminPassword[2] && userPassword[3] ==  adminPassword[3]){
              adminSection = false;
              newPasswordSection = true;
           }else{
              Serial.println("\r\nerrorInputCounter .... " + String(errorInputCounter) + " ....\r\n"); 
              errorInputCounter++;
              if(errorInputCounter >= 3){ 
                 delay(250); 
                 errorInputCounter = 0;
                 
                 systemPassword = true;      
                 systemSettings = false; 
                 
                 mainLoop = true; 
                 adminLoop = true;         
                 
                 adminSection = true;       
                 newPasswordSection = false; 
              }  
           }  
       }  
  } //  if(isDigit(key))   


   if(timerStart == true){  // timerStart == true && adminSection == true
    if(Second >= 10){      // 10
        Serial.println("\t\t\t\t\t\tTimer >>> adminSection Done");
        timerStart = false;
        Second = 0;
        systemPassword = true;  // main loop dn
        systemSettings = false;
        adminLoop = true;
        mainLoop = true;        
    }
   }  // if(timerStart == true) ***********
  } // if(adminSection == true && newPasswordSection == false) *********
  
  
  if(adminSection == false && newPasswordSection == true){   // new paswords section -------------
    if(adminLoop == false){
       fadeColorChance(255, 0, 255);  // pink
       Serial.println("\r\n--- newPasswordSection ---\r\n");
       cursorPosition = 0;
       key = 0;
       Second = 0;
       timerStart = true;
       adminLoop = true;
    }
    
    if(isDigit(key)){
     buttonsBipBuzzer();
     Serial.print("keypadClick ... " + String(key) + " ... "); Serial.println("cursorPosition ... " + String(cursorPosition)); 
    
     newPassword[cursorPosition] = key;
     cursorPosition++;

       if(cursorPosition >= 4){ 
          for(byte i=0; i <= 3; i++){ Serial.println("newPassword[" + String(i) + "] ... " + String(newPassword[i]) + " ... " ); } 
          Serial.println("\r\n--- Press Enter ---\r\n");
          fadeColorChance(0, 255, 255);  // cyan 
          cursorPosition = 0;
          newPasswordInput = true;
           
       } //  if(cursorPosition >= 4)****
    }  // if(isDigit(key)) *************

   
   // ---------------------
   if(timerStart == true){
    if(Second >= 16){  // 16
        Serial.println("\t\t\t\t\t\tTimer >>> newPasswordSection Done");
        newPasswordSection = false;     
        timerStart = false;
        Second = 0;

        newPasswordInput = false;   
           
        systemPassword = true;      // return main loop 
        systemSettings = false;
        
        mainLoop = true;       
        adminLoop = true;

        adminSection = true;       
        newPasswordSection = false; 
        
    }
   }  // if(timerStart == true) **************
  } // if(adminSection == false && newPasswordSection == true)  **********
 }// if(system_password == false && system_settings == true)*******  ayarlar ksm son 

 
} // ana dng sonu

          
void keypadEvent(KeypadEvent key){
    switch (keypad.getState()){
        
    case HOLD:
        if(key == 'P') {     // settings program
           systemPassword = false;
           systemSettings = true;
        }
        
        if(key == 'E') { // enter button
          if(newPasswordInput == true){
             
             EEPROM.write(0,newPassword[0]);    
             EEPROM.write(1,newPassword[1]); 
             EEPROM.write(2,newPassword[2]); 
             EEPROM.write(3,newPassword[3]); 

             EEPROM.write(4,120);
             EEPROM.write(5,160);
			 
             passwordRecordBuzzer();
             newPasswordInput = false;            
             eepromControl = true;
             
             adminSection = true;       
             newPasswordSection = false; 
             
             systemPassword = true;
             systemSettings = false;

             Serial.println("\r\n--- NEW PASSWORD OK ---\r\n");             
          }
        }
      
        break;
    }

}

// ----------  Functions --------------------

ISR(TIMER1_COMPA_vect){   
 
 if(timerStart == true){ 
    Second++; 
   
    if(Second != lastSecond){ 
      Serial.print("\t\t\t\t\t\tSecond .... ");  Serial.println(Second);
    }
    lastSecond = Second; 
 
 }

}  // ISR(TIMER1_COMPA_vect)  ********

void buttonsBipBuzzer(){
  tone(buzzerPin, 1500, 50);
}

void relayOnBuzzer(){
  tone(buzzerPin, 1500, 50);
  delay(250); 
  tone(buzzerPin, 1500, 50);
  delay(250);
}

void passwordRecordBuzzer(){
  tone(buzzerPin,2000,50);
  delay(250);   
  tone(buzzerPin,2000,50);
  delay(1000);
  tone(buzzerPin,1500,60);
  delay(250);   
  tone(buzzerPin,1500,60);
  delay(1000); 
}

void passwordAlarmBuzzer(){
  tone(buzzerPin,2000,50);
  delay(250);
  tone(buzzerPin,2000,50);
  delay(750);
  tone(buzzerPin,1500,60);
  delay(150);
  tone(buzzerPin,1500,60);
  delay(150);
  tone(buzzerPin,1500,60);
  delay(750); 
}


// /////////// WS2812 

void colorPixel(byte red, byte green, byte blue,byte ledNumber) {
 strip.setPixelColor(ledNumber, strip.Color(red, green, blue)); 
 strip.show();                       
}

void colorAll(byte red, byte green, byte blue) {
  for(int i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, strip.Color(red, green, blue)); 
    strip.show();                       
  }
}

void fadeInPixel(byte red, byte green, byte blue){
  float r, g, b;
     
  for(int k = 0; k < 256; k=k+1) {
    r = (k/256.0)*red;
    g = (k/256.0)*green;
    b = (k/256.0)*blue;
    colorAll(r,g,b);
    delay(2);
  }
}

void fadeOutPixel(byte red, byte green, byte blue){
  float r, g, b;
     
  for(int k = 255; k >= 0; k = k - 2) {
    r = (k/256.0) * red;
    g = (k/256.0) * green;
    b = (k/256.0) * blue;
    colorAll(r,g,b);
    delay(2);
  }
}

void fadeInOutPixel(byte red, byte green, byte blue){
  float r, g, b;
     
  for(int k = 0; k < 256; k = k + 1) {
    r = (k/256.0) * red;
    g = (k/256.0) * green;
    b = (k/256.0) * blue;
    colorAll(r,g,b); 
    delay(2);
  }
     
  for(int k = 255; k >= 0; k=k-2) {
    r = (k/256.0) * red;
    g = (k/256.0) * green;
    b = (k/256.0) * blue;
    colorAll(r,g,b);
    delay(2);
  }
}

void fadeColorChance(byte red, byte green, byte blue){
 byte colorRed,colorGreen,colorBlue;
 uint32_t readColor;
 
 readColor = strip.getPixelColor(1);
 strip.show();

 colorRed   = (uint8_t)(readColor >> 16),
 colorGreen = (uint8_t)(readColor >>  8),
 colorBlue  = (uint8_t)readColor;

 //Serial.println("colorRed " + String(colorRed)); 
 //Serial.println("colorGreen " + String(colorGreen)); 
 //Serial.println("colorBlue " + String(colorBlue)); 
 fadeOutPixel(colorRed, colorGreen, colorBlue);
 fadeInPixel (red, green, blue); 
 
}

Schematics

Keypad Board
Keypad yyelzjdn2w
Relay 5V Board
Relay ehlfjlzgxv
Buzzer Board
Buzzer gouibdqpep
WS2812 Color Led Board
Ws2812 fvnlpdeneq

Comments

Similar projects you might like

Arduino and Android Based Password Protected Door Lock

Project tutorial by Md. Khairul Alam

  • 20,085 views
  • 8 comments
  • 60 respects

Password Based Door Lock

Project tutorial by Rishabh

  • 18,549 views
  • 6 comments
  • 32 respects

Intelligent Door Lock

Project in progress by Md. Khairul Alam

  • 31,338 views
  • 25 comments
  • 132 respects

Password Saver

Project tutorial by Westpol

  • 1,764 views
  • 1 comment
  • 2 respects

Keypad Entry Lock

Project in progress by 3 developers

  • 10,086 views
  • 5 comments
  • 28 respects

Smart BT Lock with Custom Number

Project tutorial by jegatheesan

  • 4,698 views
  • 2 comments
  • 22 respects
Add projectSign up / Login