0% found this document useful (0 votes)
11 views13 pages

Microcontroller Codes

The document provides a detailed explanation of a microcontroller code that controls a servo motor, an LCD display, and a keypad for a door locking system. It outlines the setup and loop functions, including password entry, door unlocking, and error handling for incorrect password attempts. Additionally, it describes the countdown function and the display messages shown on the LCD during operation.

Uploaded by

Deus Zacharia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

Microcontroller Codes

The document provides a detailed explanation of a microcontroller code that controls a servo motor, an LCD display, and a keypad for a door locking system. It outlines the setup and loop functions, including password entry, door unlocking, and error handling for incorrect password attempts. Additionally, it describes the countdown function and the display messages shown on the LCD during operation.

Uploaded by

Deus Zacharia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

/////////////////MICROCONTROLLER CODES- EXPLANATIONS////////

#include <LiquidCrystal.h>
#include <Servo.h>
#include <Keypad.h>
Include Libraries: These lines import the necessary libraries for
controlling the LCD, the servo motor, and the keypad.
cpp
Copy code
Servo myservo;
int pos = 0; // Position of the servo
Servo Object and Position: Servo is an instance of the Servo class used to
control a servo motor. pos is an integer variable to track the servo's
position.
cpp
Copy code
LiquidCrystal lcd(A4, A5, A3, A2, A1, A0);
LCD Object: lcd is an instance of the LiquidCrystal class. The pins A4,
A5, A3, A2, A1, and A0 are connected to the LCD.
cpp
Copy code
const byte rows = 4;
const byte cols = 3;
Keypad Dimensions: These lines define the number of rows and columns
in the keypad.
cpp
Copy code
char ramkey[rows][cols] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
Keypad Layout: ramkey is a 2D array representing the characters on the
keypad buttons.
cpp
Copy code
byte rowPins[rows] = {0, 1, 2, 3};
byte colPins[cols] = {4, 5, 6};
Keypad Pins: rowPins and colPins arrays specify which Arduino pins are
connected to the rows and columns of the keypad.
cpp
Copy code
Keypad mykepad = Keypad(makeKeymap(ramkey), rowPins, colPins,
rows, cols);
Keypad Object: mykepad is an instance of the Keypad class, initialized
with the keypad layout and pin configurations.
cpp
Copy code
char* password = "0123";
int currentposition = 0;
Password and Position: password is a pointer to a string that holds the
correct password. currentposition tracks the current position in the
password entry.
Setup Function
cpp
Copy code
void setup() {
// put your setup code here, to run once:
displayscreen();
// serial.begin(9600)
myservo.attach(9); // Servo motor connection

lcd.begin(16, 2); // Initialize LCD


}
Initial Setup: setup function runs once at the start. It calls displayscreen to
show the initial message on the LCD. The servo is attached to pin 9, and
the LCD is initialized with 16 columns and 2 rows.
Loop Function
cpp
Copy code
void loop() {
// put your main code here, to run repeatedly:
if (currentposition == 0) {
displayscreen();
}
int l;
char code = mykepad.getKey();
if (code != NO_KEY) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PASSWORD:");
lcd.setCursor(7, 1);
lcd.print(" ");
lcd.setCursor(7, 1);
for (l = 0; l < currentposition; ++l) {
lcd.print("*");
// keypress
}

if (code == password[currentposition]) {
++currentposition;
if (currentposition == 4) {
unlockdoor();
currentposition = 0;
}
} else {
incorrect();
currentposition = 0;
}
}
}
Main Loop: The loop function runs continuously. If currentposition is 0,
it calls displayscreen.
Keypad Input: It checks for keypresses using mykepad.getKey().
Display Password: If a key is pressed, it clears the LCD and prompts for
the password. For each character entered, it displays an asterisk (*).
Password Check: If the entered key matches the next character in
password, currentposition is incremented. If all characters are correct,
unlockdoor is called.
Incorrect Password: If the entered key is incorrect, incorrect is called.
Unlock Door Function
cpp
Copy code
void unlockdoor() {
delay(900);
lcd.setCursor(0, 0);
lcd.println(" ");
lcd.setCursor(1, 0);
lcd.print("THE DOOR OPENED");
lcd.setCursor(4, 1);
lcd.println("WELCOME!!");
lcd.setCursor(15, 1);
lcd.println(" ");
lcd.setCursor(16, 1);
lcd.println(" ");
lcd.setCursor(14, 1);
lcd.println(" ");
lcd.setCursor(13, 1);
lcd.println(" ");

for (pos = 180; pos >= 0; pos -= 5) { // Open the door


myservo.write(pos);
delay(5);
}
delay(2000);

delay(1000);
counterbeep();

delay(1000);
for (pos = 0; pos <= 180; pos += 5) { // Close the door
myservo.write(pos);
delay(15);

currentposition = 0;

lcd.clear();
displayscreen();
}
}
Open Door: Displays "THE DOOR OPENED" and "WELCOME!!" on
the LCD.
Servo Motor Control: Rotates the servo to open and close the door.
Countdown and Reset: Calls counterbeep for a countdown before
relocking and resets the currentposition.
Incorrect Function
cpp
Copy code
void incorrect() {
delay(500);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("CODE");
lcd.setCursor(6, 0);
lcd.print("INCORRECT");
lcd.setCursor(15, 1);
lcd.println(" ");
lcd.setCursor(4, 1);
lcd.println("GET OUT!!!");

lcd.setCursor(13, 1);
lcd.println(" ");
Serial.println("CODE INCORRECT YOU UNAUTHORIZED!");
delay(3000);
lcd.clear();
displayscreen();
}
Incorrect Password: Displays "CODE INCORRECT" and "GET OUT!!!"
on the LCD and prints a message to the serial monitor.
Display Screen Function
cpp
Copy code
void displayscreen() {
lcd.setCursor(0, 0);
lcd.println("*ENTER THE CODE*");
lcd.setCursor(1, 1);
lcd.println("TO OPEN THE DOOR");
}
Initial Message: Displays a prompt to enter the code on the LCD.
Countdown Function
cpp
Copy code
void counterbeep() {
delay(1200);
lcd.clear();

lcd.setCursor(2, 15);
lcd.println(" ");
lcd.setCursor(2, 14);
lcd.println(" ");
lcd.setCursor(2, 0);
delay(200);
lcd.println("GET IN WITHIN:::!!");

lcd.setCursor(4, 1);
lcd.print("5 seconds");
delay(200);
lcd.clear();
lcd.setCursor(2, 0);
lcd.println("GET IN WITHIN::");
delay(1000);
lcd.setCursor(2, 0);
lcd.println("GET IN WITHIN::");
lcd.setCursor(4, 1);
lcd.print("4 seconds");
delay(100);
lcd.clear();
lcd.setCursor(2, 0);
lcd.println("GET IN WITHIN::");
delay(1000);

lcd.setCursor(2, 0);
lcd.println("GET IN WITHIN::");
lcd.setCursor(4, 1);
lcd.print("3 seconds");
delay(100);
lcd.clear();
lcd.setCursor(2, 0);
lcd.println("GET IN WITHIN::");
delay(1000);
lcd.setCursor(2, 0);
lcd.println("GET IN WITHIN::");
lcd.setCursor(4, 1);
lcd.print("2 seconds");
delay(100);
lcd.clear();
lcd.setCursor(2, 0);
lcd.println("GET IN WITHIN::");
delay(1000);

lcd.setCursor(4, 1);
lcd.print("1 second");
delay(100);
lcd.clear();
lcd.setCursor(2, 0);
lcd.println("GET IN WITHIN::");

delay(1000);
delay(40);
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("RE-LOCKING");
delay(500);
lcd.setCursor(12, 0);
lcd.print(".");
delay(500);
lcd.setCursor(13, 0);
lcd.print(".");
delay(500);
lcd.setCursor(14, 0);
lcd.print(".");
delay(500);
}
Countdown: Provides a countdown from 5 to 1 seconds, displaying
messages on the LCD to prompt the user to enter before the door relocks.
Relocking Message: Displays "RE-LOCKING" with

You might also like