First time posting on here. So my goal is to have 3 seperate audio files play when the button is pressed i.e button press audio file 1 plays when its done it stops playing. When the button is pressed again it plays audio file 2, all the way up to 3 times on the fourth i dont need an audio file so this will be just a blank spot for a future part of the project. But then repeat the cycle after that. Right now this is the code i have (posted below) I'm new to the serial function. Right now how its setup in the code is that im testing with just the first audio file but it plays continuously and i have even pressed a button yet. My two boards are the adafruit feather m4 express and the mini fx sound board ogg/wav trigger 16mb with wires connected from sound to micro rx-tx, tx-rx Gnd-Gnd, audio pin 1- digital pin 13 both boards are powered seperately as well. The button is a tactile button switch
#include <Adafruit_Soundboard.h> // Include the Adafruit Audio FX Sound Board library
#define BUTTON_PIN 6 // Pin connected to the button
#define SOUNDBOARD_PIN 1 // Pin connected to the Adafruit Audio FX Sound Board trigger input
// Initialize Serial1 for communication with Soundboard
Uart soundboardSerial(&sercom1, 1, 0, SERCOM_RX_PAD_1, UART_TX_PAD_0); // Serial1 for Feather M4 Express
Adafruit_Soundboard sfx(&Serial, &soundboardSerial, SOUNDBOARD_PIN);
int buttonState = HIGH; // Variable to track button state
int audioTrack = 1; // Variable to track current audio track
bool audioPlayed = false; // Flag to track if audio has been played
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP); // Use INPUT_PULLUP to enable internal pull-up resistor
pinMode(SOUNDBOARD_PIN, OUTPUT); // Set the sound board trigger pin as output
// Initialize the hardware serial port for communication with the Soundboard
Serial.begin(9600); // Adjust baud rate as needed
soundboardSerial.begin(9600);
// Allow time for Soundboard to boot up
delay(1000);
// No need for sfx.begin(); as Adafruit Soundboard does not have a begin() function
}
void loop() {
int currentButtonState = digitalRead(BUTTON_PIN);
// Check if button is pressed (LOW) and was not pressed before
if (currentButtonState == LOW && buttonState == HIGH) {
audioPlayed = false; // Reset audio flag
buttonState = LOW; // Update button state
PlayNextAudioTrack(); // Play the next audio track
} else if (currentButtonState == HIGH && buttonState == LOW) {
buttonState = HIGH; // Update button state when released
}
}
void PlayNextAudioTrack() {
if (!audioPlayed) {
sfx.stopTrack(); // Stop any currently playing track
sfx.playTrack(audioTrack); // Play the current audio track
audioPlayed = true; // Set the flag after audio is played
audioTrack++; // Increment to the next audio track
if (audioTrack > 3) {
audioTrack = 1; // Cycle back to the first track after the third track
}
}
}