Servo with keypad 4x4 and a push button

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

This user is confused with what’s being said. :woozy_face:


It appears there is no common ground between the Arduino and the LCD and servo.

The logic might be seen more clearly, with auto-indentation applied.

#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;
  }

}

sorry english isn't my first language, the Arduino, LCD and servo do have a common ground

jaja thank you, I have been trying a lot of things that maybe indentation was affected

My first language is Schematic.

2 Likes

You need to re-phrase this
image

that is to give signal to the lcd and it is at the third line of the code

LiquidCrystal lcd(22, 23, 24, 25, 26, 27); 

the main problem is the logic for the keypad and the button working together

How can you be sure, whether it is a software, or hardware problem?

Or, is there no hardware, only simulation? If simulation, how do you know you didn't make a mistake in the wiring mess of post #7?

because lcd works perfecly and post #7 is about lcd conections

What do your lcd prints say about the keypad entry and whether you can read a match or not?

Wokwi simulation or your keypad entry algorithm looks correct.

if the password is correct the lcd prints that the door is open, but at this moment with the code I shared the push button controls the servo, the keypad password only shows the password that you put and if it is correct or not but it is not controlling the servo

Non.

You need to refine your logic somewhat.
Add two new boolean variables initialized as false.

boolean buttonPressed = false;
boolean keypadValidEntry = false;

Read the button and set the boolean variable true if you see a transition from not pressed to pressed.

Read the keypad entries and set the boolean variable true if a valid password was entered. Set it false if an invalid password was entered.

Then, determine the servo action depending on whether you are open or closed.

 if (!abierta and (buttonPressed or keypadValidEntry))
  {
    puerta.write(90); //puerta abierta
    //Serial.println("abierto");
    delay(20);
    abierta = true;
    buttonPressed = false;
    keypadValidEntry = false;
  }

  else if (abierta  and (buttonPressed or keypadValidEntry))
  {
    puerta.write(0);
    //Serial.println("cerado");
    abierta = false;
    buttonPressed = false;
    keypadValidEntry = false;

  }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.