1

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;
}
2
  • Do you want to keep playing the other audio if it is past 4 seconds? or just once?
    – Fahad
    Apr 28, 2021 at 22:36
  • Hi, if the 3rd audio file could play once, that would be great. Thanks
    – Jason8899
    Apr 28, 2021 at 22:50

1 Answer 1

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:

  1. The button is pressed
  2. and is pressed for more than said time (BUTTON_LOW_TIME_MAX)
  3. 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;
}
7
  • Great, I really appreciate it. Thanks
    – Jason8899
    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
    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
    Apr 29, 2021 at 2:54
  • Unfortunately it still has the same problem, works a few times as intended, then breaks. Thanks Fahad
    – Jason8899
    Apr 29, 2021 at 14:48
  • I changed static int buttonLowTime to static long buttonLowTime and it seems to be working fine now.
    – Jason8899
    Apr 29, 2021 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.