Trying to figure out how to do this in millis. So right now the code works just fine, plays mp3_play(2) while a push button is closed then plays mp3_play(1) when its open. Lets say I want mp3_play(2) to play while the push button closed but after 4 seconds if the button is still closed another audio file will be triggered to play. Cant figure that one out.
thanks Jason
void fire(){
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
mp3_play (1); //power down sound
}
if (buttonState == LOW) {
mp3_play (2); // fire sound
}
}
}
lastButtonState = reading;
}
-
Do you want to keep playing the other audio if it is past 4 seconds? or just once?Fahad– Fahad2021年04月28日 22:36:04 +00:00Commented Apr 28, 2021 at 22:36
-
Hi, if the 3rd audio file could play once, that would be great. ThanksJason8899– Jason88992021年04月28日 22:50:55 +00:00Commented Apr 28, 2021 at 22:50
1 Answer 1
You have to keep track of time once a button is pressed (LOW). So buttonLowTime
is initialized every time you press the button. In the if
logic, you first check if the buttonstate
is LOW
and if the said time has been passed.
But you also mentioned you want to play the audio once. So you need to keep track if you already played the audio. So, a flag, timeoutAudioPlayed
has been created to keep track of that. So, now, the 3rd audio will only play if:
- The button is pressed
- and is pressed for more than said time (
BUTTON_LOW_TIME_MAX
) - and if the audio hasn't been played.
void fire()
{
static int buttonLowTime = 0; <-------------------
static bool timeoutAudioPlayed = true; <---------
int reading = digitalRead(buttonPin);
if (reading != lastButtonState)
{
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
if (reading != buttonState)
{
buttonState = reading;
if (buttonState == HIGH)
{
mp3_play (1); //power down sound
}
if (buttonState == LOW)
{
mp3_play (2); // fire sound
buttonLowTime = millis(); <-----------
timeoutAudioPlayed = false; <---------
}
}
}
// The logic for timeout audio <------------
if((buttonState == LOW) && (millis() - buttonLowTime) > BUTTON_LOW_TIME_MAX && timeoutAudioPlayed == false)
{
mp3_play (3);
timeoutAudioPlayed = true;
}
lastButtonState = reading;
}
-
Great, I really appreciate it. ThanksJason8899– Jason88992021年04月29日 00:00:36 +00:00Commented Apr 29, 2021 at 0:00
-
Thanks for the help Fahad. One little issue after trying the code, its works great for a few times, then it only plays the (3) file with a button press and not the (2) anymore.Jason8899– Jason88992021年04月29日 01:40:09 +00:00Commented Apr 29, 2021 at 1:40
-
I can just speculate the reason. I changed the timeout logic, from lastButtonState to buttonState. Because lastButtonState is true even without going through the debounce check. I updated the code above. Let me know if that works.Fahad– Fahad2021年04月29日 02:54:13 +00:00Commented Apr 29, 2021 at 2:54
-
Unfortunately it still has the same problem, works a few times as intended, then breaks. Thanks FahadJason8899– Jason88992021年04月29日 14:48:26 +00:00Commented Apr 29, 2021 at 14:48
-
I changed
static int buttonLowTime
tostatic long buttonLowTime
and it seems to be working fine now.Jason8899– Jason88992021年04月29日 21:14:07 +00:00Commented Apr 29, 2021 at 21:14