1

I'm trying to control an adafruit soundboard with my elegoo uno R3. I'm a beginner programer and I'm trying to get the soundboard to play a sound file by name when I press a button. I have confirmed the soundboard works because it works when I run the menu program that I got from the tutorial I followed, but when I try to simplify that and transfer it to my own code something goes wrong. Here is my code:

#include <Adafruit_Soundboard.h>
#include <SoftwareSerial.h>

#define SFX_TX 10
#define SFX_RX 11
#define SFX_RST 12

int pin_a = 8;
int pin_b = 7;
int pin_c = 4;
long randNumber;
long randNumberCheck;
const int buttonPin = 2;

int buttonState = 0;
int lightLoopCount = 0;

#define NUM_SOUNDS 2
char *soundName[NUM_SOUNDS] = {
  "T00     OGG",
  "T00     OGG",
};

SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);

void setup() {
  Serial.begin(115200);
  ss.begin(9600);

  if (!sfx.reset()) {
    Serial.println("Not found");
    while (1);
  }
  
  pinMode(pin_a, OUTPUT);
  pinMode(pin_b, OUTPUT);
  pinMode(pin_c, OUTPUT);
  
  pinMode(buttonPin, INPUT);

  randomSeed(analogRead(0));
}

void loop() {
  analogWrite(pin_a, 0);
  analogWrite(pin_b, 0);
  analogWrite(pin_c, 0);
  
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {

    sfx.playTrack(soundName[1]);
    
    if (!sfx.playTrack(soundName[1])) {
      Serial.print("Failed to play");
    }
      
    lightLoopCount = 0;
    while (lightLoopCount < 10) {
      analogWrite(pin_a, 0);
      analogWrite(pin_b, 0);
      analogWrite(pin_c, 0);
      
      // print a random number from 0 to 299
      randNumber = random(3);
      while (randNumber == randNumberCheck) {
        randNumber = random(3);
      }
      
      if (randNumber == 0) {
        analogWrite(pin_a, 255);
      }
      else if (randNumber == 1) {
        analogWrite(pin_b, 255);
      }
      else {
        analogWrite(pin_c, 255);
      }

      randNumberCheck = randNumber;
  
      delay(300);
      lightLoopCount++;
    }
  }
}

Here is the datasheet for the soundboard: https://www.digikey.com/htmldatasheets/production/1800160/0/0/1/audio-fx-sound-board.html

I've tried several different ways to get the board to just play a sound by filename when I press the button. Everything else works when I press the button, I think I'm just having trouble converting the string into a char array that the board can understand. It's saying "Failed to play" when I press the button. Can someone help me convert the string properly?

2
  • please add a link for the soundboard datasheet to your post ... do not add to a comment
    – jsotola
    Nov 23, 2021 at 1:45
  • add debugging code to your sketch ... print out the song filename ... also print the length of the filename just to make sure of any hidden characters
    – jsotola
    Nov 23, 2021 at 1:47

0

Your Answer

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

Browse other questions tagged or ask your own question.