Code for keypad/relay/pushbutton

Hi. Hoping that someone can help me out.

I am trying to get together code for my ESP32 WROOM to control door opening via a relay and keypad. I have made this work but I want a push button on the inside of the door that opens the door, this button should be toggeling the relay. I also want to be able to use at least 2 different codes, only works with 1 today. I have tried but my knowledge is not enough for this, am a beginner in this world. Anyone that can help me?

#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

#define RELAY_PIN   26

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[ROWS] = {27, 14, 12, 13}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 5, 15}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows


String password = "1111";
String mypassword;

int redled = 17;
int lock = 16;

int counter = 0; 
int attempts = 0; 
int max_attempts = 3; 

void setup(){
  Serial.begin(9600);
  // set up the LCD's number of columns and rows: 
  lcd.init(); // initialize the lcd
  lcd.backlight();
  //lcd.noBacklight();
  pinMode(RELAY_PIN, OUTPUT); 
  digitalWrite(RELAY_PIN, HIGH); // lock the door
  
  pinMode(redled, OUTPUT);
  pinMode(lock, OUTPUT);
  
  digitalWrite(redled, LOW);
  digitalWrite(lock, LOW);
  
  Serial.println("  PINKOD  +  *  ");
    lcd.print("  PINKOD  +  *  ");
}
  
void loop()
{
  
 keypadfunction();
 
}

void keypadfunction()
{
 char key = keypad.getKey();
  
  if (key){
    Serial.println(key);
    counter = counter + 1; 
    lcd.setCursor(counter, 1);
    lcd.print("*");
  }
  if (key == '1')
  {

    mypassword = mypassword + 1;   
  }
  
    if (key == '2')
  {

    mypassword = mypassword + 2;  
  }
  
  if (key == '3')
  {
 
    mypassword = mypassword + 3; 
  }
  
   if (key == '4')
  {
  
    mypassword = mypassword + 4;  
  }
  
  if (key == '5')
  {
  
    mypassword = mypassword + 5;
  }
  
   if (key == '6')
  {
   
    mypassword = mypassword + 6; 
  }
  
   if (key == '7')
  {

    mypassword = mypassword + 7; 
  }

   if (key == '8')
  {

    mypassword = mypassword + 8; 
  }
  
  if (key == '9')
  {

    mypassword = mypassword + 9;
  }
             
                 if (key == '0')
  {

    mypassword = mypassword + 0; 
  }
  
  
        if (key == '*')
  {
    Serial.println(mypassword); 
    
if ( password == mypassword )
{
lcd.clear(); 
lcd.println("-  Stig - In  -");
lcd.setCursor(0,1);
lcd.println("");
digitalWrite(RELAY_PIN, LOW);

mypassword = ""; 
counter = 0; 
lcd.clear();
lcd.setCursor(0,0); 
lcd.println("  PINKOD  +  *  ");
}
else
{
Serial.println("FEL PINKOD");
digitalWrite(lock, LOW);
attempts = attempts + 1; 
if (attempts >= max_attempts )
{
  lcd.clear();
  lcd.setCursor(0,0); 
  lcd.print("5 min Lockout");
  
digitalWrite(redled, HIGH);
delay(5000); 
digitalWrite(redled, LOW); 
attempts = 0; 

}
mypassword = ""; 
counter = 0; 
lcd.clear(); 
lcd.setCursor(0,0); 
lcd.print("Fel Pinkod");
delay(1000);

lcd.setCursor(0,1); 
lcd.print("Max ggr 3");
delay(1000);

lcd.clear(); 
lcd.println("  PINKOD  +  *  ");
lcd.setCursor(0,1); 
}
    
  }  
  
  
}

Can you explain that in more detail? I guess you want the button to open the door but what did you mean by "toggling"?

Do you mean 2 different passwords?

String password = "1111";
String password2 = "2222";
...
if ( password == mypassword || password == mypassword2)

Before you post your code next time, please click Tools->Auto Format in the IDE before you copy the code.

Sorry my bad.
Yes of course it was two different passwords I meant.
The toggeling button - You push i ones and it open the door, you push it again, it closes the door.

Ok, have you done some tutorials about using pushbuttons? Do you know how to write code to detect the button changing state? Do you know about button bounce and how to debounce them? If not, that is what you should focus on. Save your code above and start from an empty sketch. Write code to detect if a button is pressed or not. Then change the sketch to detect when the button changes state. Then change the sketch to debounce the button. After that, you can take what you have learned and apply it to your door lock sketch.

Declare another pin as input and connect the push button to that. Here is an example of toggle switch. It may help you. You will find many others. Instead of LED, you'll have to switch your relay connected to pin 26.

Hello
The usage of the delay() function is inhibiting the implemenation of a serious button handlers.

Line 159: delay(5000); 
	Line 169: delay(1000);
	Line 173: delay(1000);

Take some time and run button tutorials to gain the knowledge provided in the WWW.
Have a nice day and enjoy coding in C++.

Or maybe not... @rpiloverbd did you forget to attach something?

That section can be replaced with:

    if (key >= '0' && key <= '9')
      mypassword = mypassword + key;

Thanks Johnwasser, worked like a charm :slight_smile:

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