#include <Keypad.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int closeServo = 125; // Adjust the fine movement
int openServo = 20; // Adjust the fine movement
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the two-dimensional array on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// PASSWORD MANAGER
char* password = "456730AA"; // change the password here, just pick any numbers/letters
int lengthpwd = 8; // change the length of the password
int carriage = 0;
int attempt = 0;
// LED and sound
unsigned int redLed = 12;
unsigned int greenLed = 11;
unsigned int piezo = 13;
unsigned int note = 800;
// Millis
unsigned long previousMillis = 0; // will store last time LED was updated
int ledState = LOW; // ledState used to set the LED
// constants won't change:
const long interval = 800;
void setup(){
Serial.begin(9600);
myservo.attach(10); // attaches the servo on pin 10 to the servo object
myservo.write(closeServo);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
digitalWrite(redLed, 1);
tone(piezo, note, 20);
delay(500);
digitalWrite(greenLed, 1);
tone(piezo, note, 20);
delay(500);
digitalWrite(greenLed, 0);
digitalWrite(redLed, 0);
}
void loop(){
char key = customKeypad.getKey();
unsigned long currentMillis = millis();
if (key){
tone(piezo, note, 20);
Serial.println("");
Serial.println("________________");
Serial.print("Button pressed: ");
Serial.println(key);
Serial.print("Carriage: ");
Serial.println(carriage);
Serial.print("Attempt: ");
Serial.println(attempt);
Serial.println("_______________");
attempt++;
}
if (key == '*' || key == '#')
{
carriage = 0;
attempt = 0;
myservo.write(closeServo);
Serial.println("Clear");
tone(piezo, 800,300);
delay(80);
tone(piezo, 800,100);
delay(80);
tone(piezo, 700,100);
delay(80);
tone(piezo, 600,100);
delay(100);
digitalWrite(redLed, 1);
digitalWrite(greenLed, 1);
delay(200);
digitalWrite(redLed, 0);
digitalWrite(greenLed, 0);
}
if (key == password[carriage])
{
carriage ++;
Serial.println("--> Right!");
}
if (carriage == lengthpwd)
{
Serial.println("--> Bingo! <--");
carriage = 0;
attempt = 0;
myservo.write(openServo);
tone(piezo, 600,100);
delay(50);
tone(piezo, 700,100);
delay(50);
tone(piezo, 800,100);
delay(60);
tone(piezo, 800,100);
delay(80);
tone(piezo, 800,300);
for (int i = 0; i < 6; i++) {
digitalWrite(greenLed, 1);
delay(200);
digitalWrite(greenLed, 0);
delay(100);
}
digitalWrite(greenLed, 1);
}
if (attempt >= lengthpwd) {
Serial.println("--> Not the right code! <--");
digitalWrite(redLed, 1);
tone(piezo, 450, 100);
delay(100);
tone(piezo, 400, 200);
delay(500);
digitalWrite(redLed, 0);
carriage = 0;
attempt = 0;
}
if (carriage > lengthpwd)
{
Serial.println("Superior");
carriage = 0;
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(redLed, ledState);
}
}
Comments