Sunday, November 24, 2024

Keypad_Based_Password_Protected_System

 #include <Keypad.h> // Include Keypad library


// Define the keypad configuration

const byte ROWS = 4; // Four rows

const byte COLS = 4; // Four columns


// Define the keys on the keypad

char keys[ROWS][COLS] = {

  {'1', '2', '3', 'A'},

  {'4', '5', '6', 'B'},

  {'7', '8', '9', 'C'},

  {'*', '0', '#', 'D'}

};


// Connect the keypad rows and columns to Arduino pins

byte rowPins[ROWS] = {2, 3, 4, 5}; // Connect rows to pins 2-5

byte colPins[COLS] = {6, 7, 8, 9}; // Connect columns to pins 6-9


// Create the Keypad object

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);


// Define the password

const char password[] = "1234"; // Change this to your desired password

char inputPassword[5]; // Store the entered password (4 digits + null terminator)

byte passwordIndex = 0; // Track the current input position


// Define LED pins

const int greenLED = 10; // Green LED pin

const int redLED = 11;   // Red LED pin


void setup() {

  // Initialize LED pins

  pinMode(greenLED, OUTPUT);

  pinMode(redLED, OUTPUT);


  // Turn off both LEDs initially

  digitalWrite(greenLED, LOW);

  digitalWrite(redLED, LOW);


  // Start Serial Monitor for debugging

  Serial.begin(9600);

}


void loop() {

  // Get the key pressed on the keypad

  char key = keypad.getKey();


  if (key) { // If a key is pressed

    Serial.print("Key pressed: ");

    Serial.println(key);


    if (key == '#') { // '#' indicates the user is done entering the password

      inputPassword[passwordIndex] = '\0'; // Null-terminate the input

      if (strcmp(inputPassword, password) == 0) { // Compare input to stored password

        Serial.println("Correct Password!");

        blinkLED(greenLED); // Blink green LED

      } else {

        Serial.println("Incorrect Password!");

        blinkLED(redLED); // Blink red LED

      }

      passwordIndex = 0; // Reset input index for the next attempt

    } else if (passwordIndex < 4 && key != '*' && key != '#') {

      // Only allow up to 4 characters, ignore '*' and '#' as input

      inputPassword[passwordIndex] = key; // Store the key

      passwordIndex++;

    }

  }

}


// Function to blink an LED

void blinkLED(int ledPin) {

  for (int i = 0; i < 5; i++) { // Blink 5 times

    digitalWrite(ledPin, HIGH);

    delay(200);

    digitalWrite(ledPin, LOW);

    delay(200);

  }

}


No comments:

Post a Comment

MUD Three Mode operation Manual Automatic GPS

 Code for three mode operation: /*   3-Mode Headlight Controller   - Manual mode (driver uses a toggle to pick high/low)   - Auto mode (LDR...