fork download
  1. #include <Keypad.h>
  2. #include <Servo.h>
  3.  
  4. // Define the password
  5. const String password = "1234"; // Change this to your desired password
  6. String input = "";
  7.  
  8. // Setup servo
  9. Servo lockServo;
  10. const int servoPin = 9;
  11. const int lockedPos = 0; // degrees
  12. const int unlockedPos = 90; // degrees
  13.  
  14. // Keypad setup
  15. const byte ROWS = 4;
  16. const byte COLS = 4;
  17. char keys[ROWS][COLS] = {
  18. {'1','2','3','A'},
  19. {'4','5','6','B'},
  20. {'7','8','9','C'},
  21. {'*','0','#','D'}
  22. };
  23.  
  24. byte rowPins[ROWS] = {2, 3, 4, 5}; // Connect to R1-R4 of keypad
  25. byte colPins[COLS] = {6, 7, 8, 10}; // Connect to C1-C4 of keypad
  26.  
  27. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  28.  
  29. void setup() {
  30. Serial.begin(9600);
  31. lockServo.attach(servoPin);
  32. lockServo.write(lockedPos); // Lock position
  33. Serial.println("Enter Password:");
  34. }
  35.  
  36. void loop() {
  37. char key = keypad.getKey();
  38.  
  39. if (key) {
  40. Serial.print(key);
  41.  
  42. if (key == '#') {
  43. if (input == password) {
  44. Serial.println("\nAccess Granted!");
  45. unlock();
  46. } else {
  47. Serial.println("\nAccess Denied!");
  48. lock();
  49. }
  50. input = ""; // Clear input after check
  51. }
  52. else if (key == '*') {
  53. input = ""; // Clear input on '*'
  54. Serial.println("\nInput Cleared");
  55. }
  56. else {
  57. input += key;
  58. }
  59. }
  60. }
  61.  
  62. void unlock() {
  63. lockServo.write(unlockedPos);
  64. delay(5000); // Stay unlocked for 5 seconds
  65. lock();
  66. }
  67.  
  68. void lock() {
  69. lockServo.write(lockedPos);
  70. }
  71.  
Success #stdin #stdout 0.03s 25688KB
stdin
Standard input is empty
stdout
#include <Keypad.h>
#include <Servo.h>

// Define the password
const String password = "1234"; // Change this to your desired password
String input = "";

// Setup servo
Servo lockServo;
const int servoPin = 9;
const int lockedPos = 0;    // degrees
const int unlockedPos = 90; // degrees

// Keypad setup
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {2, 3, 4, 5};   // Connect to R1-R4 of keypad
byte colPins[COLS] = {6, 7, 8, 10};  // Connect to C1-C4 of keypad

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

void setup() {
  Serial.begin(9600);
  lockServo.attach(servoPin);
  lockServo.write(lockedPos); // Lock position
  Serial.println("Enter Password:");
}

void loop() {
  char key = keypad.getKey();

  if (key) {
    Serial.print(key);

    if (key == '#') {
      if (input == password) {
        Serial.println("\nAccess Granted!");
        unlock();
      } else {
        Serial.println("\nAccess Denied!");
        lock();
      }
      input = ""; // Clear input after check
    }
    else if (key == '*') {
      input = ""; // Clear input on '*'
      Serial.println("\nInput Cleared");
    }
    else {
      input += key;
    }
  }
}

void unlock() {
  lockServo.write(unlockedPos);
  delay(5000); // Stay unlocked for 5 seconds
  lock();
}

void lock() {
  lockServo.write(lockedPos);
}