So I'm rather stuck with a security door project. The idea is to have X number of passwords, but only 1 of them works at a time.
For example, the code should start with password #1, so when you type a password, wether it is right or not, a counter goes up, setting password #2 as current password.
I already added a working counter and was trying with 2 passwords, however when the counter goes up to 2, is recognizes none of them as correct. Last thing I tried was to create a char array but didn't work either.
Any help will be appreciated, thank you!
Code:
The security mechanism works by activating a stepepr mottor which winds a bolt forwards/backwards.
There's a password to reset the counter ("reinicio"), open ("apertura", the ones I'm trying to iterate), and to close the lock ("cierre")
#include <Keypad.h>
#include <Password.h>
#include <Stepper.h>
//----------------------------
//---------------------------
String newPasswordString; //hold the new password
char newPassword[6]; //charater string of newPasswordString
// Number of steps per internal motor revolution
const float STEPS_PER_REV = 32;
// Amount of Gear Reduction
const float GEAR_RED = 64;
// Number of steps per geared output rotation
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
// Number of Steps Required
int StepsRequired;
char passwords[] = {'A' , 'B'};
//initialize password to 1234
//you can use password.set(newPassword) to overwrite it
Stepper steppermotor(STEPS_PER_REV, 10, 12, 11, 13);
Password Apertura = Password ("A");
Password Reinicio = Password( "6287F65F84253CE514" );
Password Cierre = Password( "1945" );
byte maxPasswordLength = 18;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
//Define the keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
//// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {9, 8, 7, 6};
// Connect keypad COL0, COL1, COL2 and COL3 to these Arduino pins.
byte colPins[COLS] = {5, 4, 3, 2};
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop(){
//DECLARACION DE CONTRASEÑAS MULTIPLES
char key = keypad.getKey();
if (key != NO_KEY){
delay(60);
switch (key){
case '#': checkPassword();
break;
case '*': resetPassword();
digitalWrite(LED_BUILTIN, LOW);
break;
default: processNumberKey(key);
}
}
}
void processNumberKey(char key) {
Serial.print(key);
Apertura.append(key);
Cierre.append(key);
}
void checkPassword() {
if (Apertura.evaluate()){
Serial.println(" Abierto");
Serial.println(contador);
contador++;
//Stepepr opens the lock
StepsRequired = STEPS_PER_OUT_REV;
steppermotor.setSpeed(1000);
steppermotor.step(StepsRequired);
}
else if (Reinicio.evaluate()){
contador=0;
Serial.println(contador);
}
else if (Cierre.evaluate()){
Serial.println(" Cerrado");
//Stepepr opens the lock
StepsRequired = - STEPS_PER_OUT_REV;
steppermotor.setSpeed(1000);
steppermotor.step(StepsRequired);;
}
else {
contador++;
Serial.println(" Wrong Password ");
Serial.println(contador);
}
resetPassword();
}