fork download
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <Keypad.h>
  4. #include <Servo.h>
  5.  
  6. // กำหนดค่าหน้าจอ LCD
  7. LiquidCrystal_I2C lcd(0x27, 16, 2);
  8.  
  9. // กำหนดค่าคีย์แพด
  10. const byte ROWS = 4;
  11. const byte COLS = 4;
  12. char keys[ROWS][COLS] = {
  13. {'1', '2', '3', 'A'},
  14. {'4', '5', '6', 'B'},
  15. {'7', '8', '9', 'C'},
  16. {'*', '0', '#', 'D'}
  17. };
  18. byte rowPins[ROWS] = {9, 8, 7, 6};
  19. byte colPins[COLS] = {5, 4, 3, 2};
  20. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  21.  
  22. // กำหนดค่ามอเตอร์เซอร์โว
  23. Servo myServo;
  24. int servoPin = 10;
  25. String password = "1234"; // กำหนดรหัสผ่าน
  26. String inputPassword = "";
  27.  
  28. void setup() {
  29. lcd.begin();
  30. lcd.backlight();
  31. lcd.setCursor(0, 0);
  32. lcd.print("Enter Password:");
  33. myServo.attach(servoPin);
  34. myServo.write(0); // ล็อคประตู
  35. }
  36.  
  37. void loop() {
  38. char key = keypad.getKey();
  39. if (key) {
  40. lcd.setCursor(inputPassword.length(), 1);
  41. lcd.print("*");
  42. inputPassword += key;
  43. if (inputPassword.length() == 4) {
  44. delay(500);
  45. if (inputPassword == password) {
  46. lcd.clear();
  47. lcd.setCursor(0, 0);
  48. lcd.print("Access Granted");
  49. myServo.write(90); // ปลดล็อคประตู
  50. delay(5000);
  51. myServo.write(0); // ล็อคประตูอีกครั้ง
  52. } else {
  53. lcd.clear();
  54. lcd.setCursor(0, 0);
  55. lcd.print("Access Denied");
  56. delay(2000);
  57. }
  58. inputPassword = "";
  59. lcd.clear();
  60. lcd.setCursor(0, 0);
  61. lcd.print("Enter Password:");
  62. }
  63. }
  64. }
Success #stdin #stdout 0.04s 25844KB
stdin
Standard input is empty
stdout
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>

// กำหนดค่าหน้าจอ LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);

// กำหนดค่าคีย์แพด
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] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

// กำหนดค่ามอเตอร์เซอร์โว
Servo myServo;
int servoPin = 10;
String password = "1234"; // กำหนดรหัสผ่าน
String inputPassword = "";

void setup() {
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Enter Password:");
  myServo.attach(servoPin);
  myServo.write(0); // ล็อคประตู
}

void loop() {
  char key = keypad.getKey();
  if (key) {
    lcd.setCursor(inputPassword.length(), 1);
    lcd.print("*");
    inputPassword += key;
    if (inputPassword.length() == 4) {
      delay(500);
      if (inputPassword == password) {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Access Granted");
        myServo.write(90); // ปลดล็อคประตู
        delay(5000);
        myServo.write(0); // ล็อคประตูอีกครั้ง
      } else {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Access Denied");
        delay(2000);
      }
      inputPassword = "";
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter Password:");
    }
  }
}