Hi, I have been playing with this code for awhile. I would like to ultimately press a push button to start a wav audio and play it on repeat until I push the same button again. Right now I can press the button and it will play the audio repeatly but I can't stop it. Can someone help me? I'm a beginner. Here is the code:
#include <SD.h>
#include <TMRpcm.h>
TMRpcm tmrpcm;
int wasPlaying = 0;
int finished = 0;
int start = 0;
int buttonPin = 2; //digital pin where the button is
int buttonState = 0;
void setup() {
Serial.begin(9600);
Serial.print("\nInitializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("failed!");
return;
}
Serial.println("done.");
tmrpcm.speakerPin = 9; //digital pin where the speaker is
}
void loop() {
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
while(buttonState == HIGH)
{ //if button is pressed, test.wav is played repeatedly
(start = 1);
playNext();
if(tmrpcm.isPlaying())
while(tmrpcm.isPlaying());
tmrpcm.stopPlayback();
}
}
void playNext() {
if(wasPlaying == 1) {
Serial.println("Completed playback.");
wasPlaying = 0;
finished = 1;
start = 0;
}
tmrpcm.play("test.wav");
wasPlaying = 1;
}