Stop loop button when the user stay pressed, to play loop once time

Hello, I am new to Arduino, how can I stop the infinite loop, when the user keeps the button pressed, the idea is that if he presses it, the loop repeats only once.

My code:


# define ACTIVATED LOW
int btnwelcome = 6;

void setup(){
  pinMode(btnwelcome, INPUT);
  digitalWrite(btnwelcome,HIGH);
}

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

    }
}

This is contradictory.

Do you mean if the user keeps the button pressed, the loop is not executed, but if it is pressed and released, then the loop is executed?

The idea is that if they press the button, it plays only once, but there are people who leave the button pressed, and it repeats continuously, that is, for each time the button is pressed, the sound is played only once, like this they keep pressing him

OK, you need state detection (detect a change in button state). This is a simple way that looks like it will work.

void loop()
{
   // Wait for button press
   while (digitalRead(btnwelcome) != ACTIVATED);
   // Debounce
   delay(50);

   // Wait for button release
   while (digitalRead(btnwelcome) == ACTIVATED);

   myDFPlayer.play (5);
   delay (2000);
   myDFPlayer.stop ();
}

Making more than one thing run at once is the specialty of the house here!
During delay() the button can't be detected so some other way to wait is required.
The delays gotta go!

There's a technique using the system clock and unsigned integers to time intervals that always works up to a maximum interval. Arduino millis() in unsigned long variable can time almost 50 days of wait.

elapsed time = end time - start time // every time even across rollover

Arduino millis() returns the number of ms +/-1 since startup, micros() returns usecs to the last 4, the low 2 bits always 0 but it's SOLID to that ms/250.

if ( millis() - savedTime >= desired interval ) // if wait is over, do.

Nick Gammon's intro to non-blocking code.

This has absolutely nothing to do with the question that was asked!

I use the code this way:

   while (digitalRead(btnwelcome) != ACTIVATED);

   myDFPlayer.play (5);
   delay (2000);
   myDFPlayer.stop ();
   

   // Wait for button release
   while (digitalRead(btnwelcome) == ACTIVATED);
   // Debounce
   delay(50);

Very thnks :slight_smile: for u help me

It does when he wants the button to stop the song, be active all the time.

@saagcs

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.

It will help you get the best out of the forum in the future.

The DFPlayer sends serial telling when playback is finished and if any errors occur.
You don't have to read it.

I have DFPlayer Minis.

Great! And when he needs that feature he can adapt the code to add it. But this premature optimization of avoiding the delay() function just because "they might need to do something different in the future" really has to stop.

Im sorry, i have another problem in the same code, now not work my second button, that play different sound, fail when insert the new code


  if (digitalRead(btnpuerta) == ACTIVATED) { //not work this fragment
    myDFPlayer.play(1);
    delay(2500);
    }

 while (digitalRead(btnwelcome) != ACTIVATED);

   myDFPlayer.play (5);
   delay (2000);
   myDFPlayer.stop ();
   

   // Wait for button release
   while (digitalRead(btnwelcome) == ACTIVATED);
   // Debounce
   delay(50);

  if (digitalRead(btnreload) == ACTIVATED) {
    doneFlag == true;
    }

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.

Hos i can make this?

The code:

# define ACTIVATED LOW

int btnpuerta = 7;
int btnwelcome = 6;


void setup()
{

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

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

void loop(){

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

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

I am noob in this with arduino.

very thnks for u help.

You need to play the welcome sound when the engine is turned on not while it is turned on. The following tutorial should help:

StateChangeDetection

Cross-posts merged.

Due to their repeated violations, @saagcs has received a two day suspension from the forum.

@saagcs, I hope that when you return to the forum you'll be more respectful of our community.

Thanks in advance.
Per

That's just it, he can't. He is coming along fine with code but why stay in the kiddie pool?

With beginner level demo code and tutorials he can have always-active buttons/sensors at beginner level, the Hello World of Real Time code on Arduino.

@saagcs -- you can simpllify the job to independent pieces that loop() runs, interacting through variables, not loads of nested structure that leads to spaghetti code.
So the buttons have their own "driver" that updates a status byte each.
A function that sends the DFPlayer a command.
A function that reads button 1 status and run the DFPlayer function accordingly.
A function that reads button 2 status and ditto but different.

Boken to pieces, each one short and testable in a side sketch, debug goes quicker.

When you want to code smooth real time instead of jerky turn-based. If people present you-can-do-that-with-delays just remember that while your buttons and sounds code may take 300 to 400 cpu cycles, delay(1) wastes 16000 cycles that could be paying attention to the pins.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.