Help please! Door Lock button not working

Hello. Im new to Arduino and Im currently working on my first projekt (in Tinkercad) which is a door lock system. Programing was going smoothly so far but now im stuck on a problem. As my last part of the projekt I added a button which was meant to be on the other side of of the door and when pressed it was suppose to open the door just as it would with the correct password from the other side. Im not sure whether its the code or the conections that is the problem. Im able to run the code but when I press the button nothing changes.

#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Servo.h>

#define Password_Length 5

Servo servo1;
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);   

int buzzerPin = 12;                
const int buttonPin = 8;                 
int buttonState = 0;

char Data[Password_Length];
char Pass[Password_Length] = "1111";
byte data_count = 0;

char customKey;

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {0, 1, 2, 3};
byte colPins[COLS] = {4, 5, 6, 7};

Keypad customKeypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  servo1.attach(9);
  servo1.write(0);
  lcd.begin(16, 2);
  lcd.print("door locked");
  lcd.clear();
  pinMode(10, OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(buttonPin, INPUT); 
}

void loop() {
  Open();
  
  if (buttonState == HIGH){
    void Button();
  }
}

void Button(){
  servo1.write(90);
    lcd.print("door open"); 
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW); 
    delay(5000); 
    lcd.clear(); 
    lcd.print("door locked"); 
    noTone(buzzerPin); 
    delay(1000); 
    servo1.write(0);
}
  

void clearData() {
  while (data_count != 0) {
    Data[data_count--] = 0;
  }
}

void deleteChar() {
  if (data_count > 0) {
    data_count--;
    Data[data_count] = 0;
    lcd.setCursor(data_count, 1);
    lcd.print(' ');
  }
}

void Open() {
  lcd.setCursor(0, 0);
  lcd.print("enter password");
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  noTone(buzzerPin);

  customKey = customKeypad.getKey();
  if (customKey) {
    if (customKey == '#') {
      deleteChar();
    } else {
      Data[data_count] = customKey;
      lcd.setCursor(data_count, 1);
      lcd.print(Data[data_count]);
      data_count++;
      tone(buzzerPin, 440);
      delay(500);
    }
  }

  if (data_count == Password_Length - 1) {
    if (!strcmp(Data, Pass)) {
      lcd.clear();
      servo1.write(90);
      lcd.print("door open");
      digitalWrite(10, HIGH);
      digitalWrite(11, LOW);
      tone(buzzerPin, 440);
      delay(5000);
      lcd.clear();
      lcd.print("door locked");
      noTone(buzzerPin);
      delay(1000);
      servo1.write(0);
    }
    else {
      lcd.clear();
      lcd.print("incorrect password");
      digitalWrite(10, LOW);
      digitalWrite(11, HIGH);
      noTone(buzzerPin);
      delay(2000);
    }
    delay(1000);
    lcd.clear();
    clearData();
  }
}

Welcome to the forum

Where in the code do you read the state of the button ?

Oh, I see, thats a line I forgot. So do I just add this into void loop ?

void loop() {
  
  buttonState = digitalRead(buttonPin);
  
  Open();
  
  if (buttonState == HIGH){
    void Button();
  }
}

Beacause this dosent seem to work

Check the wiring of the button very carefully. Note that the button is not square and if inserted 90 degrees out of place it may not be doing what you think it is

Is the button pin held LOW when the button is not pressed ?
Does the button pin go HIGH when the button is pressed ?

Print the value of buttonState every time you read the button pin

Ok so I added this along with Serial.begin(9600); in the setup

void loop() {
  
  buttonState = digitalRead(buttonPin);
  
  Open();
  
  if (buttonState == HIGH){
    Serial.println ("Yes");
    delay (1000); 
    void Button();
    
  }
}

And it dosent work. I mean nothing is beeing writen in the serial monitor

My conclusion would be that buttonState is never HIGH

Check those button connections again

Better still :

  • Use INPUT_PULLUP in pinMode() for buttonPin to enable the built in pullup resistor to keep the pin HIGH until it is pressed.
  • Change the wiring to take the pin LOW when the button is pressed (use diagonally opposite pins on the button).
  • Remove the external resistor
  • Change the program logic to test for LOW to detect a button press

Hello I looked into it againg after some time and realised how dumb I am :smile:. When I called the function in Loop I accidently wrote void Button(); instead of Button();. Thank you for your help.