Arduino(17): Use 4x4 membrane keyboard password to control solenoid valve switch

I have written an article about using RFID to control solenoid valve switch: Arduino notes (23): Radio frequency RFID control solenoid valve switch , another way to control access control is to use the keyboard to enter a password. If the password is correct, you can enter, but if you make a mistake, you can’t. Activate the solenoid valve switch, and today’s implementation will use a password as a control measure for access control.
There are two common digital membrane keyboards: 3×4 and 4×4, 4×4 has an extra row of English letters ABCD. The membrane keyboard is a matrix composed of rows and columns. Each key is assigned to a certain row and column (see figure below).



On a 4-row x 4-column button keyboard, the first key is to establish a connection between row 1 and column 1 (R1C1). 2 is R1C2, 3 is R1C3, 5 is R2C1...etc. The 4×4 key keyboard is divided into two groups, column and row, with a total of 8 pins, which can be connected to the Pin pins of Arduino. This implementation is connected to Pin 2 ~ Pin 9.

[Install Keypad Library]

This implementation requires the following libraries to be installed:
For the installation method of the library (Library), please refer to another article: Arduino Notes: Install the Arduino IDE library.

[Material]

  • Arduino Uno x 1
  • 4x4 Keypad x 1
  • Relay x 1
  • 1602 LCD display x 1
  • LCD I2C adapter module x 1
  • 1.5V battery x 4
  • Battery holder x 1
  • Breadboard x 1
  • Connection line x N

[Wiring diagram]



[Code]

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

#define Password_Length 9 

int signalPin = 12;

char Data[Password_Length]; 
char Master[Password_Length] = "147*2580";  //password
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;

const byte ROWS = 4;
const byte COLS = 4;

// define keyboard array
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

// Pin corresponding to the keyboard row and column
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

LiquidCrystal_I2C lcd(0x27, 16, 2);  

void setup(){
  lcd.init(); 
  lcd.backlight();
  pinMode(signalPin, OUTPUT);
}

void loop(){

  lcd.setCursor(0,0);
  lcd.print("Enter Password:");

  // Get the key pressed 
  customKey = customKeypad.getKey();
  if (customKey){
    Data[data_count] = customKey; 
    lcd.setCursor(data_count,1); 
    lcd.print(Data[data_count]); 
    data_count++; 
    }

  if(data_count == Password_Length-1){
    lcd.clear();

    if(!strcmp(Data, Master)){
      lcd.print("Correct");
      
      // Set to enable Relay 
      digitalWrite(signalPin, HIGH); 
      delay(5000);
      digitalWrite(signalPin, LOW);
      }
    else{
      lcd.print("Incorrect");
      delay(1000);
      }
    
    lcd.clear();
    clearData();  
  }
}

void clearData(){
  while(data_count !=0){
    Data[data_count--] = 0; 
  }
  return;
}


[Result]


[Reference]


Post a Comment

Previous Post Next Post