KEYPAD CONTROLLED LOCK USING ARDUINO WITHOUT LIBRARY

In this section we make a password control lock (Keypad controlled lock using arduino without keypad library), Firstly we define our password here it is of 10 characters long but can be change accordingly our convenience.

In this we are using serial monitor for displaying result/message for matched or not matched conditions, However, you can use Liquid Crystal LCD 16X2. To operate the lock, the user must press * and then the secret number, followed by #.

If you earlier didn’t learn about Keypad I recommend you to first learn about it.

KEYPAD CONTROLLED LOCK USING ARDUINO WITHOUT KEYPAD LIBRARY

This can be done using any of the two codes written below 

KEYPAD CONTROLLED LOCK USING ARDUINO WITHOUT KEYPAD LIBRARY CODE – 1

int r1 = 2, r2 = 3, r3 = 4, r4 = 5;
int c1 = 8, c2 = 9, c3 = 10, c4 = 11;
int del = 400;

// n is total number of characters in password
#define n 10
// compare_count is used to compare between inputs and password
// count is used to count total number of input key pressed by user
int compare_count = 0, count = 0;

char password[n] = "1234567890";
char typed[n];
char key;

void setup() {
  Serial.begin(9600);
  for (int i = 8 ; i <= 11 ; i++) {
    pinMode(i, INPUT_PULLUP);
  }
  for (int i = 2 ; i <= 5 ; i++) {
    pinMode(i, OUTPUT);
  }
  pinMode(13, OUTPUT);
  Serial.println("Enter Password to unlock");
  Serial.println();
}

void loop() {
  key = row();

  // if pressed # then check input with password
  if (key == '#') {
    for (int i = 0 ; i < n ; i++) {
      if (typed[i] == password[i]) {
        compare_count++;
      }
    }
    if (compare_count == 10) {
      digitalWrite(13, HIGH);
      Serial.println();
      Serial.println("Matched");
      compare_count = 0;
      count = 0;
      for (int k = 0; k < n; k++) {
        typed[k] = 0;
      }
    }
    else {
      compare_count = 0;
      count = 0;
      Serial.println();
      Serial.println("NOT Matched");
    }
  }

  // if pressed * then reset all values, and start again
  else if (key == '*') {
    compare_count = 0;
    count = 0;
    Serial.println();
    Serial.println("Enter Again");
  }

  // if input is any thing between 0 to 9 read it and store in char array typed
  else if (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0') {
    typed[count] = key;
    count++;
  }

  // and if none of the above mentioned keys (#, *, 0 to 9) pressed then do nothing
  else {

  }

  // if input numbers are more than n (total number of characters in password) then reset message
  if (count == n + 1) {

    count = 0 ;
    compare_count = 0;
    Serial.println();
    Serial.println("Press * and reset again");
  }
}

char row() {
  digitalWrite(r1, LOW);
  digitalWrite(r2, HIGH);
  digitalWrite(r3, HIGH);
  digitalWrite(r4, HIGH);
  if (digitalRead(c1) == LOW) {
    Serial.print("1"); delay(del); return '1';
  }
  else if (digitalRead(c2) == LOW) {
    Serial.print("2"); delay(del); return '2';
  }
  else if (digitalRead(c3) == LOW) {
    Serial.print("3"); delay(del); return '3';
  }
  else if (digitalRead(c4) == LOW) {
    Serial.print("A"); delay(del); return 'A';
  }

  digitalWrite(r2, LOW);
  digitalWrite(r1, HIGH);
  digitalWrite(r3, HIGH);
  digitalWrite(r4, HIGH);
  if (digitalRead(c1) == LOW) {
    Serial.print("4"); delay(del); return '4';
  }
  else if (digitalRead(c2) == LOW) {
    Serial.print("5"); delay(del); return '5';
  }
  else if (digitalRead(c3) == LOW) {
    Serial.print("6"); delay(del); return '6';
  }
  else if (digitalRead(c4) == LOW) {
    Serial.print("B"); delay(del); return 'B';
  }

  digitalWrite(r3, LOW);
  digitalWrite(r1, HIGH);
  digitalWrite(r2, HIGH);
  digitalWrite(r4, HIGH);
  if (digitalRead(c1) == LOW) {
    Serial.print("7"); delay(del); return '7';
  }
  else if (digitalRead(c2) == LOW) {
    Serial.print("8"); delay(del); return '8';
  }
  else if (digitalRead(c3) == LOW) {
    Serial.print("9"); delay(del); return '9';
  }
  else if (digitalRead(c4) == LOW) {
    Serial.print("C"); delay(del); return 'C';
  }

  digitalWrite(r4, LOW);
  digitalWrite(r1, HIGH);
  digitalWrite(r2, HIGH);
  digitalWrite(r3, HIGH);
  if (digitalRead(c1) == LOW) {
    Serial.print("*"); delay(del); return '*';
  }
  else if (digitalRead(c2) == LOW) {
    Serial.print("0"); delay(del); return '0';
  }
  else if (digitalRead(c3) == LOW) {
    Serial.print("#"); delay(del); return '#';
  }
  else if (digitalRead(c4) == LOW) {
    Serial.print("D"); delay(del); return 'D';
  }

  return 'o';
}

KEYPAD CONTROLLED LOCK USING ARDUINO WITHOUT KEYPAD LIBRARY CODE – 2

// (1)
int count = 0;

#define c2 4
#define c1 3
#define c0 2
#define r3 7
#define r2 8
#define r1 9
#define r0 10
// (2)
char password[12] = "1234567890";
char typed[12];

// (3)
void setup() {
  Serial.begin(9600);
  pinMode(c0, INPUT_PULLUP);
  pinMode(c1, INPUT_PULLUP);
  pinMode(c2, INPUT_PULLUP);

  pinMode(r0, OUTPUT);
  pinMode(r1, OUTPUT);
  pinMode(r2, OUTPUT);
  pinMode(r3, OUTPUT);

  // FOR LED AND MOTOR
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
}
void loop() {
  // (4)
  char key1 = nfunc();

  // (8)
  if (key1) {
    typed[count] = key1;
    count++;
    Serial.print(key1);
  }
  // (9)
  if (count == 10) {
    if (strcmp(typed, password) == 0)
    {
      Serial.println(" correct");
      digitalWrite(11, HIGH);
      digitalWrite(12, LOW);
      delay(250);
      digitalWrite(11, LOW);
    }
    else {
      Serial.println(" intruder");
    }
    count = 0;
  }
}

// (5)
char nfunc(void) {
  char key = 'o';
  while (key == 'o')
    key = readkey();
  return key;
}

// (6)
char readkey(void) {
  digitalWrite(r0, LOW);
  digitalWrite(r1, HIGH);
  digitalWrite(r2, HIGH);
  digitalWrite(r3, HIGH);
  if (digitalRead(c0) == LOW) {
    delay(500);
    return '1';
  }
  else if (digitalRead(c1) == LOW) {
    delay(500);
    return '2';
  }
  else if (digitalRead(c2) == LOW) {
    delay(500);
    return '3';
  }
  digitalWrite(r0, HIGH);
  digitalWrite(r1, LOW);
  digitalWrite(r2, HIGH);
  digitalWrite(r3, HIGH);
  if (digitalRead(c0) == LOW) {
    delay(500);
    return '4';
  }
  else if (digitalRead(c1) == LOW) {
    delay(500);
    return '5';
  }
  else if (digitalRead(c2) == LOW) {
    delay(500);
    return '6';
  }
  digitalWrite(r0, HIGH);
  digitalWrite(r1, HIGH);
  digitalWrite(r2, LOW);
  digitalWrite(r3, HIGH);
  if (digitalRead(c0) == LOW) {
    delay(500);
    return '7';
  }
  else if (digitalRead(c1) == LOW) {
    delay(500);
    return '8';
  }
  else if (digitalRead(c2) == LOW) {
    delay(500);
    return '9';
  }
  digitalWrite(r0, HIGH);
  digitalWrite(r1, HIGH);
  digitalWrite(r2, HIGH);
  digitalWrite(r3, LOW);
  if (digitalRead(c0) == LOW) {
    delay(500);
    return '*';
  }
  else if (digitalRead(c1) == LOW) {
    delay(500);
    return '0';
  }
  else if (digitalRead(c2) == LOW) {
    delay(500);
    return '#';
  }
  // (7)
  return 'o';
}

CODE EXPLANATION

  1. First of all we are defining a few variables and constant terms like count=0, r0, r1, c0 etc. (same as describe in previous topics)
  2. Here we define two character arrays password and typed, password where we set our password and type where we store user input so that we can compare
  3. In void setup () we are defining pin Mode of pins as input and output for rows and columns and two more pin to control LED or motor
  4. In loop we are not doing much more, in this we are just calling a function nfunc() and storing its return value in character type variable key1
  5. Now we jumped to function nfunc() which returns character.
    There we assign ‘o’ to variable ‘key’ and in while block me made a condition like till key equals to ‘o’, while loop is infinite but if key has been pressed from 0 – 9, * or # then
  6. readkey() function returns number or (7) ‘o’ if nothing is pressed (all the logic we discuss in previous topics)
  7. At (8) as we pressed key we are saving key in array typed[] and increasing its (typed[]) index by 1 using count++, which increases count value on every successful hit.
  8. Then at (9), if count becomes 10, then we compared both character array typed and password using function strcmp(),
    If condition matched then we control digital pins 11 or 12 for led or motor as we need.
    Otherwise, it gives us message on serial monitor, and makes count value 0.

 

Total Visits: 357
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Translate »
0
Would love your thoughts, please comment.x
()
x