//Servo goes from 0 to 180 degrees
#include<Servo.h>
#include <Keypad.h>
//Declaration
const int servo_pin = 13;
char dugmici[4][4]={
{'1','2','3','A'},
{'4','5','6', 'B'},
{'7','8','9','C'},
{'*','0','#','D'},
};
Servo servo;
byte pinovi_vrsta[4] = {4,5,6,7};
byte pinovi_kolona[4] = {8,9,10,11};
int ugao = 0;
bool ok = false;
char taster;
int broj =0;
Keypad key = Keypad(makeKeymap(dugmici),pinovi_vrsta,pinovi_kolona,4,4);
//Setup
void setup() {
pinMode(servo_pin, OUTPUT);
Serial.begin(9600);
servo.attach(servo_pin);
servo.write(ugao);
}
//Pressed button
void Pritisnut_Taster(){
static String password = ""; // Menyimpan password yang dimasukkan
const String correctPassword = "1234"; // Password yang benar
if (taster == 'C') {
broj = 0;
ugao = 0;
password = ""; // Reset password jika tombol 'C' ditekan
Serial.println("Cancel ");
}
if (taster >= '0' && taster <= '9') {
password += taster; // Menambahkan karakter ke password
Serial.println("Entered: " + password);
if (password == correctPassword) {
Serial.println("Correct password entered. Opening the safe.");
servo.write(180); // Buka servo
delay(2000);
servo.write(0); // Tutup servo setelah 2 detik
password = ""; // Reset password setelah membuka
}
}
}
//Main loop
void loop() {
taster = key.getKey();
if (taster!=NO_KEY) {
Serial.println("Type");
Pritisnut_Taster();
}
}