how to cumulate 2 actions

Hello, we are newbies in Arduino,
Trying to program these actions:

1-enter a secret code on a keypad

2-if right code is entered, blink the Led while waiting for…

3-a touch sensor to be activated to finally…

4-keep Led ON and unlock door

Can’t get to cumulate the code and the sensor to unlock the magnet solenoid

if somebody can light my lantern, thanks :wink:

code attached below

tester2.ino (1.41 KB)

Ok. So your sketch moves through a number of states.

0 - waiting for more kepresses. This finished when the most recent 4 (or six) keypresses match the code

1 - waiting for the touch sensor. This finishes when the touch sensor is acivated (go to state 2), OR when there is a timeout (go back to state 0).

2 - keeping the door unlocked. This finishes when … I dunno. Where you going to keep the door unlocked for a set amount of time? Were you going to sense when the door was opened. and go back into 'locked' mode when it closes again?

enum State {
  ENTERING_CODE = 0,
  WAITING_TOUCH_SENSOR = 1,
  DOOR_UNLOCKED = 2
}
state = ENTERING_CODE;

void loop() {
  switch(state) {
  case ENTERING_CODE:
    read_the_keypad();
    if(valid_code_entered()) {
      start_blinking_the_led();
      start_touch_sensor_timeout();
      state = WAITING_TOUCH_SENSOR;
    }
    break;

  case WAITING_TOUCH_SENSOR
    if(touch_sensor_timeout_exceeded()) {
      stop_blinking_the_led();
      reset_the_keypad();
      state = ENTERING_CODE;
    }
    else if(touch_sensor_sensed()) {
      stop_blinking_the_led();
      unlock_the_door();
      start_unlock_timeout();
      state = DOOR_UNLOCKED;
    }
    else {
      make_the_led_blink();
    }
    break;

  case DOOR_UNLOCKED:
    if(door_unlock_timeout_exceeded()) {
      lock_the_door();
      reset_the_keypad();
      state = ENTERING_CODE;
    }
    break;
  }
}

Of course, you have to write the various functions that this main function invokes.