Ho scritto il seguente codice che tramite due psw dovrebbe muovere un servo che a sua volta muove un cardine che chiude una porta. Il problema che ho riscontrato è che nonostante immetta il codice di blocco della serratura giusto mi da errore (incorrect pin) mentre per quello di sblocco non ci sono problemi:
#include <Keypad.h>
#include <ShiftLCD.h>
#include<Servo.h>
ShiftLCD lcd(2, 4, 3);
Servo myservo;
const int pinServo = 5;
const int button = 14;
int retry = 0; //variabile sulla quale viene memorizzato il numero delle volte che si sbaglia il codice
const byte ROWS = 4; //quattro righe
const byte COLS = 4; //quattro colonne
char keys[ROWS][COLS] =
{{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
byte rowPins[ROWS] = {13,12,11,10}; //pin arduino a cui sono connessi i pin delle righe del keypad
byte colPins[COLS] = {9, 8, 7, 6}; //pin arduino a cui sono connessi i pin delle colonne del keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char PIN_lock[6]={'1','2','A','D','5','6'}; //PIN segreto
char PIN_unlock[6]={'A','1','B','2','C','3'};
char attempt[6]={0,0,0,0,0,0}; //array usato per il confronto
int z=0;
void setup()
{
lcd.begin(16,2); // inizzializzazione dell LCD
lcd.print(" Enter PIN...");
myservo.attach( pinServo );
myservo.write(20);
}
void correctPIN_() //se il PIN è corretto
{
lcd.setCursor(1,0);
lcd.print("Unlocked door");
myservo.write( 60 );
delay(4000);
lcd.clear();
lcd.print(" Enter PIN...");
}
void correctPIN()
{
lcd.setCursor(3,0);
lcd.print("Lock door");
myservo.write(20);
delay(4000);
lcd.clear();
lcd.print(" Enter PIN...");
}
void incorrectPIN() //se il PIN è sbagliato
{
lcd.setCursor(1,0);
lcd.print("Incorrect PIN");
delay(2000);
lcd.clear();
lcd.print(" Enter PIN...");
}
void riprova() {
lcd.clear();
lcd.setCursor(5,0);
lcd.print("Alarm!");
delay(500);
}
void checkPIN()
{
int correct = 0;
int correct_ = 0;
for (int q = 0; q < 6; q++)
{
if (attempt[q] == PIN_lock[q])
{
correct++;
}
}
if (correct == 6)
{
correctPIN();
}
for (int q_ = 0; q_ < 6; q_++)
{
if (attempt[q_] == PIN_unlock[q_])
{
correct_++;
}
}
if (correct_ == 6)
{
correctPIN_();
}
else
{
incorrectPIN();
retry = retry++;
}
for (int zz = 0; zz < 6; zz++) //pulisco il tentativo
{
attempt[zz] = 0;
}
if (retry == 3) {
while(retry == 3) {
riprova();
}
}
}
void readKeypad()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
switch(key)
{
case '*':
z = 0;
break;
case '#':
delay(100); // for extra debounce
lcd.clear();
checkPIN();
break;
default:
attempt[z] = key;
z++;
}
}
}
void loop()
{
readKeypad();
if (digitalRead(button) == HIGH) {
myservo.write(60);
}
}
Dove è che sbaglio?
Grazie in anticipo per le risposte.