Can i stop tonemelody with button?

my problem lies in the fact that I can not stop the "void loop" function and more accurately "StatInDoor". I have to make sure that the "ButtonOpenDoor" works independently of "ButtonCall". If someone presses the "ButtonCall" button then the "ButtonOpenDoor" button must work in progress, and if the "ButtonCall" button is pressed, the "ButtonOpenDoor" button must stop playing

My idea for this this:

int StatOutDoor = digitalRead(ButtonCall && ButtonOpenDoor);

or:

if(StatOutDoor == LOW & StatInDoor = LOW){
              PlayMelody();    
        }

but is not work :frowning:

full code:

#include "pitches.h"

int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};


int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

int ButtonCall = 4;
int buzzer = 8;
int ButtonOpenDoor = 7;
int RelayModule = 12;


void PlayMelody() {

  for (int thisNote = 0; thisNote < 8; thisNote++) {

    int noteDuration = 1000 / noteDurations[thisNote];
    tone(buzzer, melody[thisNote], noteDuration);

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    noTone(buzzer);
  }

}

void setup(){
      pinMode(ButtonCall, INPUT);
      pinMode(buzzer, OUTPUT);
      pinMode(ButtonOpenDoor,INPUT);
      pinMode(RelayModule,OUTPUT);
}
void loop() {

 int StatOutDoor = digitalRead(ButtonCall); 

    if(StatOutDoor == LOW){
          PlayMelody();    
    }  

 int StatInDoor = digitalRead(ButtonOpenDoor);

  if (StatInDoor == HIGH) {
    digitalWrite(RelayModule, HIGH);
  } 

  else {
    digitalWrite(RelayModule, LOW);
    delay(3300);
  }
}

Someone can help with it? (sorry for bad English)

what would you expect this to do?int StatOutDoor = digitalRead(ButtonCall && ButtonOpenDoor);digitalRead goes check if a PIN is HIGH or LOW and returns that value...
which pin do you think is ButtonCall && ButtonOpenDoor

your problem is that PlayMelody() is blocking until it's finished...

you should check the blink without delay example and do something similar with timing for the notes instead of blinking the led. then when it's not time to do something else, you are not stuck in a delay() and can check if buttons are pressed

ohh... sure. How to transform it?

As I said - explore the blink without delay idea

J-M-L:
As I said - explore the blink without delay idea

the solution to this problem is not trivial for a beginner and simply reading through the examples is not likely to get OP close to a solution on his/her own. It requires a pretty thorough understanding of polling, millis() timers and arrays at a bare minimum.

an object oriented approach would be the easiest and best solution but the OP (since (s)he's struggling with blocking code) may not be at that level.

Isn't the goal here learning?
I'd love to see an attempt at managing duration of each note with the blink without delay approach then we can always help

Sure, but getting things to do what you want is fun.... like lcd's, rtc's, etc...

More of an on the shoulders of giants kind-of-thing