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

Thread: Beginner - Button does not do what I want - play next file and restart

  1. #1
    Junior Member
    Join Date
    Oct 2016

    Beginner - Button does not do what I want - play next file and restart


    I'm a very infrequent coder, so I'm looking for a fresh look on what I have frankenscripted together.

    ---> What I want the code to do:
    I have one button.
    - When Teensy turns on, "nothing" audible happens
    - When button0 is pressed, the first track plays
    - When button0 is pressed again, the first track stops (in a future version it should crossfade) and the second track plays
    - When every track of const char * filelist[4] has been played, when button0 is pressed again, it reverts to the first track
    --> The button I use when pressed is LOW (it's actually a proximity switch : )

    My failed attempt to try and get this result:
    // Advanced Microcontroller-based Audio Workshop
    // Adapted sketch: Part 1-5: Respond to Pushbuttons & Volume Knob
    // When button0 is pressed, SDTEST1.WAV plays
    // When that same button is pressed, SDTEST2.WAV plays
    // Pressing the same button makes the next file play
    // The soundfile plays until its end and then you have silence
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Bounce.h>
    AudioPlaySdWav           playSdWav1;
    AudioOutputI2S           i2s1;
    AudioConnection          patchCord1(playSdWav1, 0, i2s1, 0);
    AudioConnection          patchCord2(playSdWav1, 1, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;
    Bounce button0 = Bounce(0, 15);
    byte previousState0 = HIGH;
    // Use these with the Teensy Audio Shield
    #define SDCARD_CS_PIN    10
    #define SDCARD_MOSI_PIN  7
    #define SDCARD_SCK_PIN   14
    void setup() {
      if (!(SD.begin(SDCARD_CS_PIN))) {
        while (1) {
          Serial.println("Unable to access the SD card");
      pinMode(0, INPUT);
    int filenumber = 0;  // while file to play
    const char * filelist[4] = {
    void loop() {
      byte buttonState0 = digitalRead(0);
      if (buttonState0 != previousState0) {
        Serial.println("Button0 pressed!!!");
        const char *filename = filelist[filenumber];
        filenumber = filenumber + 1;
        if (filenumber >= 4) filenumber = 0;
        Serial.print("Start playing ");
        delay(10); // wait for library to parse WAV info
      // read pushbuttons
      if (button0.fallingEdge()) {
    What the code does now:::
    ===> Teensy plays only the 4th file as it runs through the first three very very fast (not audible). When I press once, I get this from the Serial Monitor.
    Button0 pressed!!!
    Start playing SDTEST1.WAV
    Button0 pressed!!!
    Start playing SDTEST2.WAV
    Button0 pressed!!!
    Start playing SDTEST3.WAV
    Button0 pressed!!!
    Start playing SDTEST4.WAV

    I've stitched these two sketches together:

    Can some *fresh* eyes give these very tired ones a hand ?

    In the end I want 4 buttons, each with their own sounds.

    Last edited by dywen; 04-01-2021 at 09:29 PM. Reason: Clarify

Posting Permissions

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