Issue playing .wav from SD while checking button bounce

Status
Not open for further replies.

Felipevdds

New member
So I've been hitting my head against a wall all night to get the most simplest thing to work: play an audio file from SD by the push of a button.

I'm using a Teensy 4.0 With audio shield rev D and a simple pushbutton from ground to pin 19. Arduino IDE 1.8.16 and Teensyduino 1.55. A 2GB sandisk SD, but no idea of the quality and i dont suspect it being an SD card issue tbh.

When I comment out all the audio parts the button function work fine to toggle the builtin led and when I comment out all the button bouncing parts the audio just loops fine. But together it really doesn't work. I think the audio stops the bounce2 library from checking the button, I have no idea how to get around this.

#include <Bounce2.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioPlaySdWav playSdWav1; //xy=351,481
AudioMixer4 mixer1; //xy=577,567
AudioAnalyzeRMS rms1; //xy=902,593
AudioOutputI2S i2s2; //xy=920,485
AudioConnection patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection patchCord2(playSdWav1, 0, i2s2, 0);
AudioConnection patchCord3(playSdWav1, 1, mixer1, 1);
AudioConnection patchCord4(playSdWav1, 1, i2s2, 1);
AudioConnection patchCord5(mixer1, rms1);
AudioControlSGTL5000 sgtl5000_1; //xy=1006,788
// GUItool: end automatically generated code
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 11
#define SDCARD_SCK_PIN 13

#define BUTTON_PIN 19
#define LED_PIN LED_BUILTIN

Bounce2::Button button = Bounce2::Button();

int ledState = LOW;

void setup() {
sgtl5000_1.enable();
sgtl5000_1.volume(0.8);

AudioMemory(15);

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);
}
}
AudioInterrupts();
button.attach( BUTTON_PIN, INPUT ); // USE EXTERNAL PULL-UP
button.interval(5);
button.setPressedState(LOW);

// LED SETUP
pinMode(LED_PIN,OUTPUT);
digitalWrite(LED_PIN,ledState);
}

void loop() {
button.update();
if ( button.pressed() ) {
Serial.println("pressed");
playSdWav1.play("SOUND2.WAV");
// delay(3000);


// TOGGLE THE LED STATE :
ledState = !ledState; // SET ledState TO THE OPPOSITE OF ledState
digitalWrite(LED_PIN,ledState); // WRITE THE NEW ledState
}
}
 
Hi,

try to increase debounce time from 5ms to maybe 20ms
or use Intervalltimer for function button.update() every 2 ms

I asume a timing problem
 
You probably want to check button.fallingEdge(), so you only start playing the WAV file once when the button changes from not pressed to pressed.
 
Not needed here - pressed() takes care of that. It returns true if the state changed.
It's just the wrong pin.
 
Status
Not open for further replies.
Back
Top