Home > Blockchain >  How stop a loop, if the signal is still active
How stop a loop, if the signal is still active

Time:03-12

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?

CodePudding user response:

if (digitalRead(btnpuerta) == ACTIVATED) {
    myDFPlayer.play(1);
    delay(2500);
    }

   if (digitalRead(btnwelcome) == ACTIVATED) {
       if (doneFlag == false) // fragmento para repetir el comando una sola vez
      {
          myDFPlayer.play (5);
          delay (2000);
          myDFPlayer.stop ();
          doneFlag = true;
      }
     
    }else{
      doneFlag = false;
    }

CodePudding user response:

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
  • Related