Unit 23: Access Codes
Code:
#include
// Pin definitions
const int ledPin = 12;
const int buzzerPin = 11;
// Keypad settings
const byte ROWS = 4; // 4 rows
const byte COLS = 3; // 3 columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {10, 9, 8, 7}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 5, 4}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Variables for code input
const String correctCode = "3107"; // Set the correct 4-digit code
String inputCode = ""; // Store the user’s input
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(ledPin, LOW); // Start with LED off
digitalWrite(buzzerPin, LOW); // Start with buzzer off
}
void loop() {
char key = keypad.getKey(); // Read the pressed key
if (key) { // If a key is pressed
inputCode += key; // Append the key to the input string
if (inputCode.length() >= 4 && (key == '*' || key == '#')) { // After at least 4 digits and a final *, #
if (inputCode.substring(0, 4) == correctCode) {
digitalWrite(ledPin, HIGH); // Turn on the LED for correct code
delay(2000); // Keep the LED on for 2 seconds
digitalWrite(ledPin, LOW);
} else {
digitalWrite(buzzerPin, HIGH); // Sound the buzzer for wrong code
delay(1000); // Buzzer sounds for 1 second
digitalWrite(buzzerPin, LOW);
}
inputCode = ""; // Reset the input string for the next attempt
}
}
}
Code Two (Serial output):
#include
// Pin definitions
const int ledPin = 12;
const int buzzerPin = 11;
// Keypad settings
const byte ROWS = 4; // 4 rows
const byte COLS = 3; // 3 columns
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {10, 9, 8, 7}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 5, 4}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Variables for code input
const String correctCode = "2222"; // Set the correct 4-digit code
String inputCode = ""; // Store the user’s input
void setup() {
Serial.begin(115200); // Start serial monitor at high speed
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(ledPin, LOW); // Start with LED off
digitalWrite(buzzerPin, LOW); // Start with buzzer off
Serial.println("System ready. Waiting for keypad input...");
}
void loop() {
char key = keypad.getKey(); // Read the pressed key
if (key) { // If a key is pressed
Serial.print("Key Pressed: ");
Serial.println(key);
inputCode += key; // Append the key to the input string
Serial.print("Current Input: ");
Serial.println(inputCode);
if (inputCode.length() >= 4 && (key == '*' || key == '#')) { // After 4 digits + final key
Serial.print("Entered Code: ");
Serial.println(inputCode.substring(0, 4));
if (inputCode.substring(0, 4) == correctCode) {
Serial.println("Correct Code! LED ON");
digitalWrite(ledPin, HIGH); // Turn on the LED for correct code
delay(2000); // Keep the LED on for 2 seconds
digitalWrite(ledPin, LOW);
Serial.println("LED OFF");
} else {
Serial.println("Wrong Code! Buzzer ON");
digitalWrite(buzzerPin, HIGH); // Sound the buzzer for wrong code
delay(1000); // Buzzer sounds for 1 second
digitalWrite(buzzerPin, LOW);
Serial.println("Buzzer OFF");
}
inputCode = ""; // Reset the input string for the next attempt
Serial.println("Input reset. Waiting for new code...");
}
}
}