Created
September 10, 2023 17:47
-
-
Save dawksh/70a1177e003edcc281847d7b787aeae7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
#include <Keypad.h> | |
Servo servoMotor; | |
const int servoPin = 913; // Digital pin connected to the servo | |
const byte ROWS = 4; //four rows | |
const byte COLS = 4; //four columns | |
char keys[ROWS][COLS] = { | |
{'1','2','3','A'}, | |
{'4','5','6','B'}, | |
{'7','8','9','C'}, | |
{'*','0','#','D'} | |
}; | |
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad | |
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad | |
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); | |
const char* correctPIN = "6969"; // Change this to your correct PIN | |
void setup(){ | |
servoMotor.attach(servoPin); | |
servoMotor.write(0); // Initialize servo at 0 degrees (closed position) | |
Serial.begin(9600); | |
} | |
void loop(){ | |
char key = keypad.getKey(); | |
if (key){ | |
Serial.println(key); | |
static char enteredPIN[5]; // Stores entered digits | |
static byte index = 0; // Index to keep track of digit position | |
if (key == '#'){ | |
// Check the entered PIN | |
enteredPIN[index] = '\0'; // Null-terminate the string | |
if (strcmp(enteredPIN, correctPIN) == 0){ | |
// Correct PIN entered, do something here (e.g., unlock the servo) | |
servoMotor.write(90); // Rotate servo to 90 degrees (open position) | |
delay(2000); // Wait for 2 seconds | |
servoMotor.write(0); // Rotate servo back to 0 degrees (closed position) | |
index = 0; // Reset index | |
} else { | |
// Incorrect PIN entered, do something here (e.g., indicate error) | |
// In this example, we rotate the servo slightly as an error indication | |
servoMotor.write(45); // Rotate servo to 45 degrees (partial open position) | |
delay(500); // Wait for 0.5 seconds | |
servoMotor.write(0); // Rotate servo back to 0 degrees (closed position) | |
index = 0; // Reset index | |
} | |
} else { | |
if (index < 4){ | |
enteredPIN[index] = key; | |
index++; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment