Hi, 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.
You can't stop loop(). But you can put more conditions on the "if" statement. Perhaps adding a boolean being tested to see if this code has been run once already, do run it again.
Hi
I modified your code to use millis() instead of delay().
See if it works as you want.
Your code is missing the DFPlayer library.
#define ACTIVATED LOW
int btndoor = 7;
int btnwelcome = 6;
unsigned long time_door = 0; // Time control for door
unsigned long time_weelCome = 0; // Time control for wellcome
bool doorFlag = false; // Flag for run door play
bool wellFlag = false; // Flag for run wellcome play
int counter = 0; // Play 5 time only
//--------------------------------------------------
void setup()
{
pinMode(btndoor, INPUT_PULLUP);
// digitalWrite(btnpuerta, HIGH); ???? btnpuerta
pinMode(btnwelcome, INPUT_PULLUP);
time_door = millis(); // Inicialize time_door control
time_weelCome = millis(); // Inicialize time_weelCome control
}
//--------------------------------------------------
void loop() {
if (digitalRead(btndoor) == ACTIVATED)
{
if (doorFlag == false) // if not pass 2500 mseg
{
myDFPlayer.play(1); // Play
doorFlag = true; // Block repeat until 2500 mseg
time_door = millis(); // Reinicialize time_door control
}
if (millis() - time_door >= 2500) // If pass 2500 ms
{
doorFlag = false; // Enable play
}
}
if (digitalRead(btnwelcome) == ACTIVATED)
{
if (counter < 5) // Play 5 times
{
if (wellFlag == false) // if not pass 2000 mseg
{
myDFPlayer.play(5); // Play
counter++; // Icrement play counter
wellFlag = true; // Block repeat until 2000 mseg
time_weelCome = millis(); // Reinicialize time_weelCome control
}
if (millis() - time_weelCome >= 2000) // If pass 2000 ms
{
wellFlag = false; // Enable play
}
}
else // If play more then 5 times
{
myDFPlayer.stop (); // Play off
}
}
else // if btnwelcome is not ACTIVATED
{
counter = 0; // Reset counte of play
}
}