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