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

PROGRAMMING CODE

// (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.

Leave a Reply

avatar
  Subscribe  
Notify of

Arduino Tutorials with Basic Electronics: CONTENT

Translate »