Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- #include <Keypad.h>
- Servo myservo;
- const int ROW_NUM = 4; //four rows
- const int COLUMN_NUM = 4; //three columns
- char keys[ROW_NUM][COLUMN_NUM] = {
- {'1', '2', '3', 'A'},
- {'4', '5', '6', 'B'},
- {'7', '8', '9', 'C'},
- {'*', '0', '#', 'D'}
- };
- byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
- byte pin_column[COLUMN_NUM] = {5, 4, 3 , 2}; //connect to the column pinouts of the keypad
- Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
- const String password_1 = "1234"; // change your password here
- const String password_2 = "5642"; // change your password here
- const String password_3 = "4545"; // change your password here
- String input_password;
- //*********************************************************************
- //||||||||||||||||||| Flags Setup to Know Password Status |||||||||||||
- //*********************************************************************
- int flag_p1 = 0; // 0 = password not entered yet
- int flag_p2 = 0; // 1 = password was entered
- int flag_p3 = 0;
- //*********************************************************************
- void setup() {
- Serial.begin(9600);
- input_password.reserve(5);
- myservo.attach(10); // maximum input characters is 5, change if needed
- //*********************************************************************
- //||||||||||||||||||| Setting Flags to 0 when Restart |||||||||||||||||
- //*********************************************************************
- flag_p1 = 0;
- flag_p2 = 0;
- flag_p3 = 0;
- //*********************************************************************
- }
- void loop() {
- char key = keypad.getKey();
- if (key) {
- Serial.println(key);
- if (key == 'C') {
- input_password = ""; // reset the input password
- }
- else if (key == '*') {
- //*********************************************************************
- //||||||||||||||||||| Conditions for Password #1 ||||||||||||||||||||||
- //*********************************************************************
- if (input_password == password_1 && flag_p1 == 0) {
- flag_p1 = 1;
- Serial.println("password is correct");
- runServo();
- }
- else if (input_password == password_1 && flag_p1 == 1) {
- Serial.println("password entered before, try diffrent password");
- }
- //*********************************************************************
- //||||||||||||||||||| Conditions for Password #2 ||||||||||||||||||||||
- //*********************************************************************
- else if (input_password == password_2 && flag_p2 == 0) {
- flag_p2 = 1;
- Serial.println("password is correct");
- runServo();
- }
- else if (input_password == password_2 && flag_p2 == 1) {
- Serial.println("password entered before, try diffrent password");
- }
- //*********************************************************************
- //||||||||||||||||||| Conditions for Password #3 ||||||||||||||||||||||
- //*********************************************************************
- else if (input_password == password_3 && flag_p3 == 0) {
- flag_p3 = 1;
- Serial.println("password is correct");
- runServo();
- }
- else if (input_password == password_3 && flag_p3 == 1) {
- Serial.println("password entered before, try diffrent password");
- }
- //*********************************************************************
- //|||||||||||||||||| Condition to reset All Flags |||||||||||||||||||||
- //*********************************************************************
- else if (input_password == "ABAD") {
- Serial.println("All Flags Reset");
- flag_p1 = 0;
- flag_p2 = 0;
- flag_p3 = 0;
- }
- //*********************************************************************
- //|||||||||||||||||| Condition for Wrong Password |||||||||||||||||||||
- //*********************************************************************
- else {
- Serial.println("password is incorrect, try again");
- }
- //*********************************************************************
- input_password = ""; // reset the input password
- }
- else {
- input_password += key; // append new character to input password string
- }
- }
- }//end of Loop
- //*********************************************************************
- //|||||||||||||||||| Function to run the Servo ||||||||||||||||||||||||
- //*********************************************************************
- void runServo() {
- Serial.println("ON");
- myservo.write(0);
- delay(3000);
- myservo.attach(10);
- delay(3000);
- myservo.detach();//Stop
- delay(2000);
- }
- //*********************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement