I am a tinkerer trying to put together a retirement gift for a friend of mine that plays a train whistle when a button is pushed. I want to simply press a button, play the whole MP3 then stop. If they push the button again I want it to play the full MP3 once more and so on.
My problem is, I have to hold down the button for the MP3 to play. If I let go of the button it stops playing. If I press and hold again it continues where you let go of the button until it starts over.
How can I make this code play the full MP3 with a single button press then stop, but if you press the button again it plays the whole MP3 again?
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "AudioFileSourceSPIFFS.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2S.h"
#define switchPin D6
AudioGeneratorMP3 *mp3;
AudioFileSourceSPIFFS *file;
AudioOutputI2S *out;
void setup()
{
WiFi.forceSleepBegin();
Serial.begin(115200);
delay(1000);
SPIFFS.begin();
file = new AudioFileSourceSPIFFS("/train.mp3");
out = new AudioOutputI2S();
mp3 = new AudioGeneratorMP3();
mp3->begin(file, out);
pinMode(switchPin, INPUT_PULLUP);
}
void loop(){
int btn_Status = HIGH;
btn_Status = digitalRead (switchPin);
if (btn_Status == LOW) {
if (mp3->isRunning()) {
if (!mp3->loop()) mp3->stop();
} else {
Serial.printf("MP3 done\n");
delay(1000);
{ file->close(); delete file; delete mp3; }
}
}
}
I have a few other version that I have played around with but still cannot get it to do what I want.
Again I am trying to press a button, play the whole MP3 then stop. If the button gets pressed again, play the whole MP3 then stop.
Thank you all in advance. I am just a tinkerer self-taught trying to do something nice for a friend. I get it, I get it. I should go to school and learn the fundamentals etc. etc.. that I have seen in these forums. Well, I don't have time or Money to devote to becoming an Expert. I am just trying to learn as I go. Thanks