Help with coding arduino uno + smart rich shield

Hello everyone, I am working on a project where I have to create a safe, and simulate the door of these safe with the light sensor (when the sensor recognize the light the door is open, otherwise close). Now, I am missing the last part: when the code is correct and you open the door, and then close the door automatically, the safe turn back in block and you have to retype the password. I am trying different ways but I dunno why does not work:
void SensorDoor() // this is the sensor that recognize the door open or close

the thought is simple: when the condition are true back to the mode 0

THE FUNCTION:

 void SensorDoor() // this is the sensor that recognize the door open or cloose
  {
    int doorSensorValue = analogRead(DOOR_SENSOR);
    doorSensorValue = map (doorSensorValue, 0, 1023, 0, 200);
      
      if (checkPassword != PASSWORD && doorSensorValue > 100)
      {
          while (alarmRunning < 7) 
            {
              digitalWrite(FAILED_LED, HIGH);
              delay(200);
              digitalWrite(FAILED_LED, LOW); 
              delay(200);
              tone(ALARM_SOUND, 440, 1000);
              alarmRunning ++;
              Display.show("----");
                
            }

           
      }
       else if (checkPassword == PASSWORD && doorSensorValue < 100)
       {
        mode = 0;
        
       }

}

THE ENTIRE CODE

#include <Display.h>

  const int LEFT_BUTTON_PIN = 9;
  const int RIGHT_BUTTON_PIN = 8;
  const int OPEN_LED = 5; // if the password match with the default password a led turn on, as a proof of the correct password
  const int FAILED_LED = 4 ; // if the password is wrong, the red led switch on 
  const int ALARM_SOUND = 3; // if the door is opened with the security code still active, the alarm sounds
  const int DOOR_SENSOR = 16;
 
  const int press_Button = LOW; 
  const int not_PressButton = HIGH;

  const String digitSpace1 = "000"; // these number are the spaces for the display and allow to type from left to right
  const String digitSpace2 = "00";
  const String digitSpace3 = "0";
  const String PASSWORD = "1234"; // this is the default password

  String checkPassword = ""; // check password, show if the 4 input match the dafult password
  int alarmRunning = 0; 



  int mode   = 0; // these are the mode for entering the four digit

  String inputValue_1 = ""; // this is the number that will be stored typed from the visitor (1-5)
  String inputValue_2 = "";
  String inputValue_3 = "";
  String inputValue_4 = "";

  int numberValue = 0;

  

  int previousLeft = LOW;
  int previousRight = LOW;
     

void setup() {

  pinMode (LEFT_BUTTON_PIN, INPUT_PULLUP);
  pinMode (RIGHT_BUTTON_PIN, INPUT_PULLUP);
  pinMode(OPEN_LED, OUTPUT);
  pinMode(FAILED_LED, OUTPUT);
  pinMode (ALARM_SOUND, OUTPUT);
  pinMode (DOOR_SENSOR, INPUT);

  
  Serial.begin(9600);
 Display.show("0000");
} 

void GetInput();
void ConfirmInput();
void SensorDoor();

void loop() {
 
  GetInput(); 
  ConfirmInput();
  SensorDoor();
  

}

    


void GetInput() // this is the function to get all the for digit
{ 
  int leftButton = digitalRead(LEFT_BUTTON_PIN);
   
   if(leftButton != previousLeft)
    {
      if (leftButton == press_Button)
      {
        numberValue = numberValue % 5 + 1 ; // this is the loop for the five digits
        delay(200);

          if (mode == 0 )
          {
             inputValue_1 = numberValue;
             Display.show(inputValue_1 + digitSpace1);
          } 

          else if (mode == 1 )
          {
             inputValue_2 = numberValue;
             Display.show(inputValue_1 + inputValue_2 + digitSpace2);
           }

          else if (mode == 2 )
          {
              inputValue_3 = numberValue;
              Display.show(inputValue_1 + inputValue_2 + inputValue_3 + digitSpace3);
          }

          else if (mode == 3 )
          {
             inputValue_4 = numberValue;
             Display.show(inputValue_1 + inputValue_2 + inputValue_3 + inputValue_4 );
          }  
      }
    previousLeft = leftButton;
   }
}

 void ConfirmInput () // this is the mode that store and conferm the digit

{
    int rightButton = digitalRead (RIGHT_BUTTON_PIN);
      
      

    if(rightButton != previousRight)
     { 
        	if (rightButton == press_Button)
          {
            mode = (mode+1) % 5; 
            numberValue = 0;
          }

          else if (mode == 4)
          {
            checkPassword = inputValue_1 + inputValue_2 + inputValue_3 + inputValue_4;
          
              if (checkPassword == PASSWORD)
              {
                digitalWrite(OPEN_LED, HIGH);
                Display.off();
              
               
              } 

              else if(checkPassword != PASSWORD)
              {
                digitalWrite(FAILED_LED, HIGH);
                delay(1000);
                Display.clear();
            
                Display.show("0000");
                delay(1000);
                digitalWrite(FAILED_LED, LOW);
                mode = 0;               
                
              }
          }
            previousRight = rightButton;
           
           
     }
}

  void SensorDoor() // this is the sensor that recognize the door open or cloose
  {
    int doorSensorValue = analogRead(DOOR_SENSOR);
    doorSensorValue = map (doorSensorValue, 0, 1023, 0, 200);
      
      if (checkPassword != PASSWORD && doorSensorValue > 100)
      {
          while (alarmRunning < 7) 
            {
              digitalWrite(FAILED_LED, HIGH);
              delay(200);
              digitalWrite(FAILED_LED, LOW); 
              delay(200);
              tone(ALARM_SOUND, 440, 1000);
              alarmRunning ++;
              Display.show("----");
                
            }

           
      }
       else if (checkPassword == PASSWORD && doorSensorValue < 100)
       {
        mode = 0;
        
       }

}

i think your issue is

  • code needs to recognize that the correct password is entered and the door is ok to be opened
  • recognize that is has been opened after the correct password entered
  • recognize that it is subsequently closed and the password is cleared requiring it to be reentered

i think it's odd that you call each of your sub-functions, each of which monitors a separate input.

i would have a sub-function that monitors all the inputs and performs some action based on the button press

the left button would increment the current digit, the right button would advance to the next digit, the door sensor might turn on the alarm if the door is opened and the password not set correctly

if this is mimicking a combination lock, does the password need to be jumbled to reset it. could that automatically happen after the door is closed.

consider

// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

#define MAX_DIGIT  5
byte code  [] = { 1, 2, 3, 4 };
#define N_DIGIT     sizeof(code)

byte combo [N_DIGIT] = {};
byte digit           = 0;

char s [40];

// -----------------------------------------------------------------------------
void
dispCombo (void)
{
    for (unsigned n = 0; n < N_DIGIT; n++)
        Serial.print (combo [n]);
    Serial.println ();
}

// -----------------------------------------------------------------------------
void
jumble (void)
{
    for (unsigned n = 0; n < N_DIGIT; n++)
        combo [n] = random (0, MAX_DIGIT-1);
    dispCombo ();
}

// -----------------------------------------------------------------------------
bool
unlocked (void)
{
    for (unsigned n = 0; n < N_DIGIT; n++)
        if (code [n] != combo [n])
            return false;
    return true;
}

// -----------------------------------------------------------------------------
enum {
    ButInc,
    ButNext,
    Door,

    ButIncRel  = 10 + ButInc,
    ButNextRel = 10 + ButNext,
    DoorClosed = 10 + Door
};

int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

        if (butState [n] != but)  {
            butState [n] = but;

            delay (10);     // debounce

            if (On == but)
                return n;
            else
                return 10 + n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case DoorClosed:
        jumble ();
        break;

    case Door:
        if (unlocked ())
            Serial.println ("door");
        else
            Serial.println ("alarm");
        break;

    case ButNext:
        digit = (digit + 1) % N_DIGIT;
        break;

    case ButInc:
        combo [digit] = (combo [digit] + 1) % MAX_DIGIT;
        dispCombo ();
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }

    dispCombo ();
}

Thank you!!

Thank you for the suggestions