Search code examples
javaarduinoarduino-uno

How stop a loop, if the signal is still active


i have two inputs with wiring, the first, detect doors opens, and play sound 1, this sound turn off, when closed its doors.

The second sound, is welcome sound when turn on the engine car, but i have problem with the second sound, because it plays loop forever with welcome the sound because the engine is turn on.

I need to play the welcome sound every time the engine is beginning to work, but only once, the input signal never cut off, because the engine is turn on, it only cuts off when the engine is off.

The code:

# define ACTIVATED LOW

int btndoor = 7;
int btnwelcome = 6;

void setup()
{
  pinMode(btndoor, INPUT);
  digitalWrite(btnpuerta,HIGH);

  pinMode(btnwelcome, INPUT);
  digitalWrite(btnwelcome,HIGH);

}

void loop(){
  if (digitalRead(btndoor) == ACTIVATED) {
    myDFPlayer.play(1);
    delay(2500);
    }

  if (digitalRead(btnwelcome) == ACTIVATED) {
          myDFPlayer.play (5);
          delay (2000);
          myDFPlayer.stop ();     
     
    }

}

How i can stop loop?


Solution

  • u can use flag as

    if engine is on
     if flag ==  false
       play the sound 
       flag = true
    

    but dont forget to put iot as false when turn the engine off

    turn engine off 
    flag = false