Sample Arduino keypad password Lock




Keypad Lock Schematic


ARD

Sketch

//Easy! How to Make an Arduino Keypad Lock
// Plase Add Keypad Library


#include <Keypad.h>


char password[] = "1234";  // change the password here
int position = 0;

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] = { 2,3,4,5 };
byte colPins[COLS] = { 6,7,8,9 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int RedLockPin = 12;
int GreenUnlockPin = 13;

void setup()
{
  
    pinMode(RedLockPin, OUTPUT);
    pinMode(GreenUnlockPin, OUTPUT);

    LockedPosition(true);

}

void loop()
{
    char key = keypad.getKey();
   
    if (key == '*' || key == '#')
    {
       position = 0;
       LockedPosition(true);
    }
    if (key == password[position])
    {
       position ++;
    }
    if (position == 4)
    {
       LockedPosition(false);
    }
    
    delay(100);
    
}
void LockedPosition(int locked)
{
if (locked)
{
    digitalWrite(RedLockPin, HIGH);
    digitalWrite(GreenUnlockPin, LOW);
}
else
{
    digitalWrite(RedLockPin, LOW);
    digitalWrite(GreenUnlockPin, HIGH);
}
}
UINO KEYPAD d

ความคิดเห็น