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; }

Jason8899
  • 23
  • 4

1 Answers1

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(); &lt;-----------
    timeoutAudioPlayed = false; &lt;---------
  }
}

}

// The logic for timeout audio <------------ if((buttonState == LOW) && (millis() - buttonLowTime) > BUTTON_LOW_TIME_MAX && timeoutAudioPlayed == false) { mp3_play (3); timeoutAudioPlayed = true; }

lastButtonState = reading; }

Fahad
  • 544
  • 2
  • 10
  • Great, I really appreciate it. Thanks – Jason8899 Apr 29 '21 at 00: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 '21 at 01: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 '21 at 02:54
  • Unfortunately it still has the same problem, works a few times as intended, then breaks. Thanks Fahad – Jason8899 Apr 29 '21 at 14:48
  • I changed static int buttonLowTime to static long buttonLowTime and it seems to be working fine now. – Jason8899 Apr 29 '21 at 21:14
  • Oh, my bad. It should have been long, and NOT int. Good catch! – Fahad Apr 30 '21 at 01:04
  • thanks again... – Jason8899 Apr 30 '21 at 02:30