Two unconnected buttons are stuck together in the program

Hi everyone :slight_smile:

I am trying to create a circuit and a program for a locking mechanism. I am using a servo, two buttons, two LEDs, and a keypad.

What I wanted to do:

The first button to pin 2 locks the box.
The second button to pin 3 allows the pin code to be entered.
Without the second button being pushed, the lock should not accept the pin code, and remain locked.
If the second button has been pushed, the green led should flash. If the wrong code was entered, the red led should flash.
If the code is right, the lock opens, and the green light is on.

What happens, when I run my program on the Arduino:

Pressing the lock button also changes the ‚Äúallowed‚ÄĚ boolean to true, so the code can be entered without pressing the second button (and the green light keeps flashing)
The allow button seems to have no effect whatsoever.

Below is a picture of the wiring, and the program I wrote. Thank you for any help or insight! :slight_smile:

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

#define Password_Lenght 5

Servo myServo;

char Data[Password_Lenght]; // keypad and password settings
char Master[Password_Lenght] = "1234"; 
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;

const byte ROWS = 4; 
const byte COLS = 3; 

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {4, 5, 6, 7}; 
byte colPins[COLS] = {8, 9, 10}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

const int lockPin = 2;  // switch for locking the box
const int allowPin = 3; // switch for permission to enter pin code 
const int greenLed = 12; // signal led
const int redLed = 13;   // signal led

unsigned long previousMillis = 0; // settings for blinking the green led
const long interval = 1000;
int ledState = LOW;

int lockVal = LOW; // inital input from lockPin
int allowVal = LOW;// initial input from allowPin

boolean locked = false;
boolean allowed = false;
  
void setup(){
  myServo.attach(11);
  pinMode(lockPin, INPUT);
  pinMode(allowPin, INPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);

  Serial.begin(9600);
  digitalWrite(greenLed, HIGH);
  myServo.write(0);
  Serial.println("The box is open!");
}
  
void loop(){
  Serial.print("Locked: ");   //"diagnostics" information - for trying to figure out what's going on
  Serial.print(locked);
  Serial.print(", Allowed: ");
  Serial.print(allowed);
  Serial.print(", lockVal: ");
  Serial.print(lockVal);
  Serial.print(", allowVal: ");
  Serial.println(allowVal);
  
if(locked == false){         //When the box is unlocked, and the lock button is pressed, the box will be locked, and the red light will be on. 
  lockVal = digitalRead(lockPin);
  if(lockVal == HIGH){
    locked = true;
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, HIGH);
    myServo.write(90);
    Serial.println("The box is locked!");
    delay(1000);
  }
}
else {                       // If the box is locked, we are waiting for permission signal to enter the code. When the Allow button is pressed, the code can be entered, and the green led blinks.

  allowVal = digitalRead(allowPin);
  if(allowVal == HIGH);{
    allowed = true;
    if(allowed == true){
    unsigned long currentMillis = millis();            //if the allowed button has been pressed, the green light is blinking. 
    if(currentMillis - previousMillis >= interval){
      previousMillis = currentMillis;
      if(ledState == LOW){
        ledState = HIGH;
      } else {
        ledState = LOW;
      }
      digitalWrite(greenLed, ledState);
    }
      customKey = customKeypad.getKey();               // and we recieving and storing input from the keypad.
      if(customKey) {
        Data[data_count] = customKey;
        data_count++;
    }
    if(data_count == Password_Lenght-1){               // if the correct pin code is entered, the lock is opened, green light on, red light off, reset data from the keypad.
      if(!strcmp(Data, Master)){
        myServo.write(0);
        digitalWrite(redLed, LOW);
        digitalWrite(greenLed, HIGH);
        Serial.println("The box is open!");
        locked = false;
        allowed = false;
        delay(1000);
        clearData();
      }
      else {
        digitalWrite(redLed, LOW);                    // if the pin code is incorrect, the red light will blink a few times, and the keypad data is reset. 
        delay(300);
        digitalWrite(redLed, HIGH);
        delay(300);
        digitalWrite(redLed, LOW);
        delay(300);
        digitalWrite(redLed, HIGH);
        delay(300);
        digitalWrite(redLed, LOW);
        delay(300);
        digitalWrite(redLed, HIGH);
        delay(300);
        clearData();
        }
    }
  } else {                                           // if allowed is not true, the green light should stay off
    digitalWrite(greenLed, LOW);
  }
} 
}
}
void clearData(){                                    // information for performing clear data 
  while(data_count !=0){
    Data[data_count--] = 0;
  }
  return;
}

; is empty statement. after ‚Äėif‚Äô it is executed if the condition is true. the next line is then executed unconditionally

if(allowVal == HIGH);

{
}

it is better write ‚Äú!locked‚ÄĚ (read ‚Äúnot locked‚ÄĚ) then (locked == false)

it is better write "!locked" (read "not locked") then (locked == false)

Quicker to type maybe, but why better when both do the same ?

UKHeliBob:
"it is better write "!locked" (read "not locked") then (locked == false)"
Quicker to type maybe, but why better when both do the same ?

you see false and it is true

Thank you! It is working as intended! :slight_smile:

you see false and it is true

No, I see that locked is false.

Personally I would rather get rid of the negation and invert the logic so that you can write

if (locked)
{
  //code to run when in locked state
}