Run two function in the same loop, with delay in one of the functions!

Hello :slight_smile:

Im working on my home alarm and im stuck on the last part.
I need to be able to run a function with a delay in it (the 30 sec countdown for the alarm) and have another function that runes continuously without beeing interupted by the other function.
Right now only the timer function runs wich results in the other function not working. (Aka running)

This is my code:

  void loop() 
  {
      if(alarmActivated == true) 
      {   
        enterPassword();
      } 
      if(alarmTrigger == true)
      {
        alarm();
      }
      if(resetPassword == true)
      {
        resetPass();
      }
      if(activateAlarm == true)
      {
        actAlarm();
      }
      if(loggedIn == true)
      {
        choseOption();
      }
      if(tempBool == true)
      {
        activateAlarmLarm();
      }

      
      if(trueFalse == true) {
        alarmTrigger = false;
        alarmActivated = false;
    noTone(buzzer); // Send 1KHz sound signal...
      } else {
        hallState = digitalRead ( hallPin ) ;                           // reading from the sensor and storing the state of the hall effect sensor :
        if ( hallState == LOW ) {                                                // Checking whether the state of the module is high or low
           Serial.println ( " The state of the analog hall module is high " ) ;
           alarmActivated = false;
           alarmTrigger = false;
        } else {
          alarmActivated = true;
          tempBool = true;
          Serial.println ( " The state of the analog hall module is low " ) ;
       }
        
      }

So when the hallState == HIGH i have my tempBool and alarmActivated true. But beacuse the tempBool has delay in it the alarmActivated does not work wich results in me not beeing able to type in my password.

Heres the rest of the code:

 ///////////////////////////////////////
  //                                   //
  //     DEACTIVATE ALARM FUNCTION     //
  //                                   //
  ///////////////////////////////////////


  void enterPassword() {


  int key = kpd.getKey();
    
       if (key == '1') {
          lengthCode ++;
          lcd.print('*');
          tempPassword += 1;
  }  if (key == '2') {
          lengthCode ++;
          lcd.print('2');
          tempPassword += 2;
  }  if (key == '3') {
          lengthCode ++;
          lcd.print('3');
          tempPassword += 3;
  }  if (key == '4') {
          lengthCode ++;
          lcd.print('4');
          tempPassword += 4;
  }  if (key == '5') {
          lengthCode ++;
          lcd.print('5');
          tempPassword += 5;
  }  if (key == '6') {
          lengthCode ++;
          lcd.print('6');
          tempPassword += 6;
  }  if (key == '7') {
          lengthCode ++;
          lcd.print('7');
          tempPassword += 7;
  }  if (key == '8') {
          lengthCode ++;
          lcd.print('8');
          tempPassword += 8;
  }  if (key == '9') {
          lengthCode ++;
          lcd.print('9');
          tempPassword += 9;
  }  if (key == '0') {
          lengthCode ++;
          lcd.print('0');
          tempPassword += 0;
  }  
  
  if (key == '*') {
        if(trys > 1)
        {
           if(tempPassword == password){
                 trueFalse = true;
                 alarmTrigger = false;
                 loggedIn = true;
                 tempPassword = "";
                 lengthCode = 0;
                 lcd.clear();
                 lcd.print("Success!");
                 alarmActivated = false;
                 delay(2000);
                 lcd.clear();
                 lcd.print("Welcome home");
                 
            } else if (tempPassword != password) {
     
                lcd.clear();
                lcd.print("Wrong password:");
                tempPassword = "";
                trys -= 1;
                lcd.setCursor(0,1);
                lcd.print(trys);
                lcd.print(" trys left");
                delay(1500);
                lcd.clear();
                lcd.setCursor(0,0);
                lcd.print("Enter password:");
                lcd.setCursor(0,1);
                
             }
          } else {
          lcd.clear();
          lcd.print("Run you fools");
          alarmTrigger = true;
        }
  }
  if (key == '#')
  {
    if(alarmActivated == true)
    {
    lcd.clear();
    tempPassword = "";
    lcd.setCursor(0,0);
    lcd.print("Enter password:");
    lcd.setCursor(0,1);
    } 
  }
}
  ///////////////////////////////////////
  //                                   //
  //       ACTIVATE ALARM LARM         //
  //                                   //
  ///////////////////////////////////////

  void activateAlarmLarm() {
    for(int i=0; i < 3; i++) {
      delay(1000);
       digitalWrite(18, HIGH); 
    tone(buzzer, 1000); // Send 1KHz sound signal...
        delay(1000);
        digitalWrite(18, LOW); 
    noTone(buzzer); // Send 1KHz sound signal...
    }
  }

If you want a responsive program then don't use delay(). Use millis() to manage your timing without blocking as illustrated in the demo Several Things at a Time

And please post a complete program as a single piece. If it is too long (hopefully not) you can add your .ino file as an attachment.

...R

Thanks :slight_smile:

Moved as requested.

if(trueFalse == true) {

What a dumb name. Completely meaningless.