Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Issue playing .wav from SD while checking button bounce

  1. #1
    Junior Member
    Join Date
    Oct 2021
    Posts
    1

    Issue playing .wav from SD while checking button bounce

    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
    }
    }

  2. #2
    Senior Member
    Join Date
    May 2017
    Posts
    123
    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

  3. #3
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    9,424
    PIN 19 is used for I2C which is needed for the Audio Shield.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    25,223
    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.

  5. #5
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    9,424
    Not needed here - pressed() takes care of that. It returns true if the state changed.
    It's just the wrong pin.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •