I'm making a soundboard that plays a file via the onboard DACs, but WAV playback from the SD card is behaving very strangely.
I have full code below, edited down to a simple test of playing one file. When I run the code, a portion of the file is played followed by a section static on channel 0. A second attempt to play the same file does nothing. A third attempt is not reached.
I've tried two different Teensy 3.6 units with the same results.
The only thing I can think of is a corrupted WAV file with garbage appended and the firmware tries to read it endlessly. How could I verify?
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Bounce.h>
AudioPlaySdWav playWav1;
// Use one of these 3 output types: Digital I2S, Digital S/PDIF, or Analog DAC
//AudioOutputI2S audioOutput;
//AudioOutputSPDIF audioOutput;
AudioOutputAnalog audioOutput;
//On Teensy LC, use this for the Teensy Audio Shield:
//AudioOutputI2Sslave audioOutput;
AudioConnection patchCord1(playWav1, 0, audioOutput, 0);
AudioConnection patchCord2(playWav1, 1, audioOutput, 1);
//AudioControlSGTL5000 sgtl5000_1;
// Use these with the Teensy Audio Shield
//#define SDCARD_CS_PIN 10
//#define SDCARD_MOSI_PIN 7
//#define SDCARD_SCK_PIN 14
// Use these with the Teensy 3.5 & 3.6 SD card
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define SDCARD_MOSI_PIN 11 // not actually used
#define SDCARD_SCK_PIN 13 // not actually used
// Use these for the SD+Wiz820 or other adaptors
//#define SDCARD_CS_PIN 4
//#define SDCARD_MOSI_PIN 11
//#define SDCARD_SCK_PIN 13
//Button pins
int button_Pins[]={24,25,26,27,28,29,30,31,34};
void setup() {
Serial.begin(9600);
// Audio connections require memory to work. For more
// detailed information, see the MemoryAndCpuUsage example
AudioMemory(8);
// Comment these out if not using the audio adaptor board.
// This may wait forever if the SDA & SCL pins lack
// pullup resistors
//sgtl5000_1.enable();
//sgtl5000_1.volume(0.5);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
// stop here, but print a message repetitively
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
for(int i;i<9;i++){
pinMode(i,INPUT_PULLUP);
}
playFile("BUTTON0.WAV");
delay(2000);
playFile("BUTTON0.WAV");
delay(2000);
playFile("BUTTON0.WAV");
/*
attachInterrupt(digitalPinToInterrupt(button_Pins[0]),BUTTON0_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[1]),BUTTON1_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[2]),BUTTON2_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[3]),BUTTON3_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[4]),BUTTON4_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[5]),BUTTON5_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[6]),BUTTON6_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[7]),BUTTON7_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[8]),BUTTON8_ISR,FALLING);
*/
Serial.println("Setup Complete");
}
void playFile(const char *filename)
{
Serial.print("Playing file: ");
Serial.println(filename);
// Start playing the file. This sketch continues to
// run while the file plays.
Serial.println(playWav1.play(filename));
// A brief delay for the library read WAV info
// Simply wait for the file to finish playing.
while (playWav1.isPlaying()) {
// uncomment these lines if you audio shield
// has the optional volume pot soldered
//float vol = analogRead(15);
//vol = vol / 1024;
// sgtl5000_1.volume(vol);
}
Serial.println("Done playing...");
}
void BUTTON0_ISR(){
noInterrupts();
playFile("BUTTON0.WAV");
Serial.println("PLAY");
interrupts()
delay(2500);
}
void BUTTON1_ISR(){
noInterrupts()
playFile("BUTTON1.WAV");
Serial.println("Button 1");
interrupts()
}
void BUTTON2_ISR(){
noInterrupts()
playFile("BUTTON2.WAV");
Serial.println("Button 2");
interrupts()
}
void BUTTON3_ISR(){
noInterrupts()
playFile("BUTTON3.WAV");
Serial.println("Button 3");
interrupts()
}
void BUTTON4_ISR(){
noInterrupts()
playFile("BUTTON4.WAV");
Serial.println("Button 4");
interrupts()
}
void BUTTON5_ISR(){
noInterrupts()
playFile("BUTTON5.WAV");
Serial.println("Button 5");
interrupts()
}
void BUTTON6_ISR(){
noInterrupts()
playFile("BUTTON6.WAV");
Serial.println("Button 6");
interrupts()
}
void BUTTON7_ISR(){
noInterrupts()
playFile("BUTTON7.WAV");
Serial.println("Button 7");
interrupts()
}
void BUTTON8_ISR(){
noInterrupts()
Serial.println("Button 8");
playFile("BUTTON8.WAV");
interrupts()
}
void loop() {
}
I have full code below, edited down to a simple test of playing one file. When I run the code, a portion of the file is played followed by a section static on channel 0. A second attempt to play the same file does nothing. A third attempt is not reached.
I've tried two different Teensy 3.6 units with the same results.
The only thing I can think of is a corrupted WAV file with garbage appended and the firmware tries to read it endlessly. How could I verify?
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Bounce.h>
AudioPlaySdWav playWav1;
// Use one of these 3 output types: Digital I2S, Digital S/PDIF, or Analog DAC
//AudioOutputI2S audioOutput;
//AudioOutputSPDIF audioOutput;
AudioOutputAnalog audioOutput;
//On Teensy LC, use this for the Teensy Audio Shield:
//AudioOutputI2Sslave audioOutput;
AudioConnection patchCord1(playWav1, 0, audioOutput, 0);
AudioConnection patchCord2(playWav1, 1, audioOutput, 1);
//AudioControlSGTL5000 sgtl5000_1;
// Use these with the Teensy Audio Shield
//#define SDCARD_CS_PIN 10
//#define SDCARD_MOSI_PIN 7
//#define SDCARD_SCK_PIN 14
// Use these with the Teensy 3.5 & 3.6 SD card
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define SDCARD_MOSI_PIN 11 // not actually used
#define SDCARD_SCK_PIN 13 // not actually used
// Use these for the SD+Wiz820 or other adaptors
//#define SDCARD_CS_PIN 4
//#define SDCARD_MOSI_PIN 11
//#define SDCARD_SCK_PIN 13
//Button pins
int button_Pins[]={24,25,26,27,28,29,30,31,34};
void setup() {
Serial.begin(9600);
// Audio connections require memory to work. For more
// detailed information, see the MemoryAndCpuUsage example
AudioMemory(8);
// Comment these out if not using the audio adaptor board.
// This may wait forever if the SDA & SCL pins lack
// pullup resistors
//sgtl5000_1.enable();
//sgtl5000_1.volume(0.5);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
// stop here, but print a message repetitively
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
for(int i;i<9;i++){
pinMode(i,INPUT_PULLUP);
}
playFile("BUTTON0.WAV");
delay(2000);
playFile("BUTTON0.WAV");
delay(2000);
playFile("BUTTON0.WAV");
/*
attachInterrupt(digitalPinToInterrupt(button_Pins[0]),BUTTON0_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[1]),BUTTON1_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[2]),BUTTON2_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[3]),BUTTON3_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[4]),BUTTON4_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[5]),BUTTON5_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[6]),BUTTON6_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[7]),BUTTON7_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(button_Pins[8]),BUTTON8_ISR,FALLING);
*/
Serial.println("Setup Complete");
}
void playFile(const char *filename)
{
Serial.print("Playing file: ");
Serial.println(filename);
// Start playing the file. This sketch continues to
// run while the file plays.
Serial.println(playWav1.play(filename));
// A brief delay for the library read WAV info
// Simply wait for the file to finish playing.
while (playWav1.isPlaying()) {
// uncomment these lines if you audio shield
// has the optional volume pot soldered
//float vol = analogRead(15);
//vol = vol / 1024;
// sgtl5000_1.volume(vol);
}
Serial.println("Done playing...");
}
void BUTTON0_ISR(){
noInterrupts();
playFile("BUTTON0.WAV");
Serial.println("PLAY");
interrupts()
delay(2500);
}
void BUTTON1_ISR(){
noInterrupts()
playFile("BUTTON1.WAV");
Serial.println("Button 1");
interrupts()
}
void BUTTON2_ISR(){
noInterrupts()
playFile("BUTTON2.WAV");
Serial.println("Button 2");
interrupts()
}
void BUTTON3_ISR(){
noInterrupts()
playFile("BUTTON3.WAV");
Serial.println("Button 3");
interrupts()
}
void BUTTON4_ISR(){
noInterrupts()
playFile("BUTTON4.WAV");
Serial.println("Button 4");
interrupts()
}
void BUTTON5_ISR(){
noInterrupts()
playFile("BUTTON5.WAV");
Serial.println("Button 5");
interrupts()
}
void BUTTON6_ISR(){
noInterrupts()
playFile("BUTTON6.WAV");
Serial.println("Button 6");
interrupts()
}
void BUTTON7_ISR(){
noInterrupts()
playFile("BUTTON7.WAV");
Serial.println("Button 7");
interrupts()
}
void BUTTON8_ISR(){
noInterrupts()
Serial.println("Button 8");
playFile("BUTTON8.WAV");
interrupts()
}
void loop() {
}