gamemaster8245
Published © CC0

Keypad Entry Lock With LEDs

Enter the keypad PIN specified in the code, to activate the green LED. Red LED activates when incorrect code is entered.

IntermediateShowcase (no instructions)440
Keypad Entry Lock With LEDs

Things used in this project

Hardware components

5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
5 mm LED: Green
5 mm LED: Green
×1
Arduino UNO
Arduino UNO
×1
Membrane Switch Keypad
×1
Male/Male Jumper Wires
×1
Resistor 220 ohm
Resistor 220 ohm
×3
USB-A to B Cable
USB-A to B Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Keypad Hookup + Design

Keypad + LEDs will need to be adjusted to work properly with the ARDUINO, some pins may need to be changed.

Code

KeypadCode.ino

C/C++
Keypad Entry Lock C++ Code
/* Passcode keypad input program
 * Functionality:
 * Power on and ready, light white led on
 * Correct code button press, light green led for 250 ms
 * Incorrect code button press, light red led for 500 ms and reset code sequence
 * Full correct code entry, light green light for 2000 ms and reset code sequence
 */
#include <Keypad.h>

//Define Pins (Variables)
#define GREEN 10                                                                                    
#define WHITE 11
#define RED 12

//Define Passcode Variables
#define pCode1 '3'
#define pCode2 '7'
#define pCode3 '0'
#define pCode4 '2'
//bool bCodeFail = true;
bool bCodeFail1 = true;
bool bCodeFail2 = true;
bool bCodeFail3 = true;
bool bCodeFail4 = true;

//Setup keypad pins and outputs
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);


void setup() {
  // put your setup code here, to run once:
pinMode(GREEN, OUTPUT);
pinMode(WHITE, OUTPUT);
digitalWrite(WHITE, HIGH);
pinMode(RED, OUTPUT);
Serial.begin(9600);
//bCodeFail = false;
}

void loop() {
  // put your main code here, to run repeatedly:
//TEST LEDS
//digitalWrite(GREEN, HIGH);
//digitalWrite(WHITE, HIGH);
//digitalWrite(RED, HIGH);
//delay(500);
//digitalWrite(GREEN, LOW);
//digitalWrite(WHITE, LOW);
//digitalWrite(RED, LOW);
//delay(500);

//TEST KEYPAD
//char customKey = customKeypad.getKey();
  
//  if (customKey)
//  {
//    Serial.println(customKey);
//    if (customKey == '1')
//    {
//      digitalWrite(GREEN, HIGH);
//      delay(250);
//      digitalWrite(GREEN, LOW);
//    }
//    if (customKey == '2' || customKey == '3')//,'4','5','6','7','8','9','0')
//    {
//      digitalWrite(RED, HIGH);
//      delay(500);
//      digitalWrite(RED, LOW);
//    }
//  }
  char kPress = customKeypad.getKey();
  if (kPress)
  {
    Serial.println(kPress);
    if (kPress == pCode1 && bCodeFail1 == true)
    {
      digitalWrite(GREEN, HIGH);
      delay(250);
      digitalWrite(GREEN, LOW);
      bCodeFail1 = false;
    }
    else
    {
      if (kPress == pCode2 && bCodeFail2 == true && bCodeFail1 == false)
      {
        digitalWrite(GREEN, HIGH);
        delay(250);
        digitalWrite(GREEN, LOW);
        bCodeFail2 = false;
      }
      else
      {
        if (kPress == pCode3 && bCodeFail3 == true && bCodeFail1 == false && bCodeFail2 == false)
        {
          digitalWrite(GREEN, HIGH);
          delay(250);
          digitalWrite(GREEN, LOW);
          bCodeFail3 = false;
        }
        else
        {
          if (kPress == pCode4 && bCodeFail4 == true & bCodeFail1 == false && bCodeFail2 == false && bCodeFail3 == false)
          {
            digitalWrite(GREEN, HIGH);
            delay(3000);
            digitalWrite(GREEN, LOW);
            bCodeFail1 = true;
            bCodeFail2 = true;
            bCodeFail3 = true;
            bCodeFail4 = true; 
          }
          else
          {
            digitalWrite(RED, HIGH);
            delay(500);
            digitalWrite(RED, LOW);
            bCodeFail1 = true;
            bCodeFail2 = true;
            bCodeFail3 = true;
            bCodeFail4 = true;
          }
        }
      }
    }
    //Serial.println(bCodeFail);
  }





  
}

Credits

gamemaster8245
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.