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
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)