Hi everyone, I'm carring out the project of using a password to move a servo (door) using also a keypad 4x4 it'is a well known project the thing is that there is a push button which can open and close the door (for example you open the door using the password, you can close it using the push button and vice versa) I'm presenting problems join together the keypad and the push button, the servo moves only with the push button, is there any mechanism to these two elements to this work?
#include <Servo.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(22, 23, 24, 25, 26, 27); // crea objeto y asigna pines a los cuales se
// encuentran conectados RS, E, D4, D5, D6, D7
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 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
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char password[7];
char realpassword[7]= "123456";
byte index=0;
Servo puerta; // se nombra al servo
const int BOTON = 53;
int val = 0; //val se emplea para almacenar el estado del boton
int state = 0; // 0 LED apagado, mientras que 1 encendido
int old_val = 0; // almacena el antiguo valor de val
boolean abierta= false;
void setup(){
lcd.begin(16, 2);
puerta.attach(11);// conecta el servo a un pin
pinMode(BOTON,INPUT); // y BOTON como señal de entrada
}
void loop(){
lcd.setCursor(0,0);
char key = keypad.getKey();
val= digitalRead(BOTON); // lee el estado del Boton
if ((val == HIGH) && (old_val == LOW)){
state=1-state;
delay(10);
}
old_val = val; // valor del antiguo estado
if (key != NO_KEY){
lcd.println(key);
password[index]= key;
index++;
}
if (index==6){
byte check =0;
for(int i=0;i<6;i++){
lcd.print(password[i]);
if (password[i]==realpassword[i]){
check++;
}
}
if (check== 6 ) {
lcd.setCursor(0,1);
lcd.println("abierto");
puerta.write(90); //puerta abierta
delay(20);
abierta=true;
}else {
lcd.setCursor(0,1);
lcd.println("cerrado");
puerta.write(0); //puera cerrada
delay(20);
abierta=false;
}
index=0;
}
if( state==1){
puerta.write(90); //puerta abierta
delay(20);
abierta=true;
}else{
puerta.write(0);
abierta=false;
}
if( state!=1){
puerta.write(0); //puerta abierta
delay(20);
abierta=false;
}else{
puerta.write(90);
abierta=true;
}
}
the button must to be in one state until you pushed it again, I'm using an arduino mega
thank you