Giovanni Gentile
Published © GPL3+

ELEGOO Keypad lock Powered by Arduino

This is a very complete project to create a keypad lock system by using the new Elegoo kit. Lock and unlock a servo motor by using a code

BeginnerFull instructions provided1 hour1,527
ELEGOO Keypad lock Powered by Arduino

Things used in this project

Hardware components

ELEGOO UNO R3 Project Complete Starter Kit
ELEGOO UNO R3 Project Complete Starter Kit
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematics ELEGOO Keypad Lock

Code

ELEGOO keypad lock

Arduino
/******************************************************************
|| @name ELEGOO Keypad lock
|| @date 2021.09.03
|| @version 1.0
|| @author Giovanni Gentile
|| @contact www.projectg.it
||
|| @description
|| Open the door by unlocking the servo motor.
|| Digit the right code on the keypad. Reset by using '#' or '*'
|| Fine tune the code: int closeServo
||                     int openServo
||                     char* password = "putyourpassword";  
|| Connect to the USB for debugging (9600 baud) 
||
|| @connections
|| Servo pin 10
|| Keypad pin fron 2 to 9
||        row:  9, 8, 7, 6
||        cols: 5, 4, 3, 2
|| Green Led: pin 11
|| Red Led: pin 12
|| Piezo: pin 13
|| 
*******************************************************************/

#include <Keypad.h>
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
int closeServo = 125; // Adjust the fine movement
int openServo = 20; // Adjust the fine movement

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns

//define the two-dimensional array 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); 

// PASSWORD MANAGER
char* password = "456730AA";  // change the password here, just pick any numbers/letters
int lengthpwd = 8; // change the length of the password
int carriage = 0; 
int attempt = 0;

// LED and sound
unsigned int redLed = 12;
unsigned int greenLed = 11;
unsigned int piezo = 13;
unsigned int note = 800;
// Millis
unsigned long previousMillis = 0; // will store last time LED was updated
int ledState = LOW;             // ledState used to set the LED
// constants won't change:
const long interval = 800;    

void setup(){
  Serial.begin(9600);
  myservo.attach(10);  // attaches the servo on pin 10 to the servo object
  myservo.write(closeServo);
  
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  digitalWrite(redLed, 1);
  tone(piezo, note, 20);
  delay(500);
  digitalWrite(greenLed, 1);
  tone(piezo, note, 20);
  delay(500);
  digitalWrite(greenLed, 0);
  digitalWrite(redLed, 0);
  
}
  
void loop(){
  char key = customKeypad.getKey();
  unsigned long currentMillis = millis();
  
  if (key){
    tone(piezo, note, 20);
    Serial.println("");
    Serial.println("________________");
    Serial.print("Button pressed: ");
    Serial.println(key);
    Serial.print("Carriage: ");
    Serial.println(carriage);
    Serial.print("Attempt: ");
    Serial.println(attempt);
    Serial.println("_______________");
    
    attempt++;
  }

  if (key == '*' || key == '#')
  {
    carriage = 0;
    attempt = 0;
    myservo.write(closeServo);
    Serial.println("Clear");
    tone(piezo, 800,300);
    delay(80);
    tone(piezo, 800,100);
    delay(80);
    tone(piezo, 700,100);
    delay(80);
    tone(piezo, 600,100);
    delay(100);
    digitalWrite(redLed, 1);
    digitalWrite(greenLed, 1);
    delay(200);
    digitalWrite(redLed, 0);
    digitalWrite(greenLed, 0);
  }

  if (key == password[carriage])
  {
    carriage ++;
    Serial.println("--> Right!");
  } 

  if (carriage == lengthpwd)
  {
    Serial.println("--> Bingo! <--");
    carriage = 0;
    attempt = 0;
    myservo.write(openServo);
    tone(piezo, 600,100);
    delay(50);
    tone(piezo, 700,100);
    delay(50);
    tone(piezo, 800,100);
    delay(60);
    tone(piezo, 800,100);
    delay(80);
    tone(piezo, 800,300);
    for (int i = 0; i < 6; i++) {
      digitalWrite(greenLed, 1);
      delay(200);
      digitalWrite(greenLed, 0);
      delay(100);
    }
    digitalWrite(greenLed, 1);
  }

  if (attempt >= lengthpwd) {
    Serial.println("--> Not the right code! <--");
    digitalWrite(redLed, 1);
    tone(piezo, 450, 100);
    delay(100);
    tone(piezo, 400, 200);
    delay(500);
    digitalWrite(redLed, 0);
    carriage = 0;
    attempt = 0;
  }

  if (carriage > lengthpwd)
  {
    Serial.println("Superior");
    carriage = 0;
  }
  if (currentMillis - previousMillis >= interval) {
   // save the last time you blinked the LED
   previousMillis = currentMillis;

   // if the LED is off turn it on and vice-versa:
   if (ledState == LOW) {
     ledState = HIGH;
   } else {
     ledState = LOW;
   }

   // set the LED with the ledState of the variable:
   digitalWrite(redLed, ledState);
}
}

Credits

Giovanni Gentile

Giovanni Gentile

36 projects • 98 followers
Graduated in Psychology Artificial Intelligence department. Expert in electronics, automation and IoT. Now working on VR-AR experiences.

Comments