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

Status
Not open for further replies.

dywen

Member
Hi,

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 : http://etextile-summercamp.org/swatch-exchange/sewable-proximity-switch-board-1/ )

My failed attempt to try and get this result:
Code:
// Advanced Microcontroller-based Audio Workshop
//
// http://www.pjrc.com/store/audio_tutorial_kit.html
// https://hackaday.io/project/8292-microcontroller-audio-workshop-had-supercon-2015
// 
// 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() {
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }

  pinMode(0, INPUT);
  delay(1000);
}

int filenumber = 0;  // while file to play

const char * filelist[4] = {
  "SDTEST1.WAV", "SDTEST2.WAV", "SDTEST3.WAV", "SDTEST4.WAV"
};


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 ");
    Serial.println(filename);
    playSdWav1.play(filename);
    delay(10); // wait for library to parse WAV info
  }
  
  
  // read pushbuttons
  button0.update();
  if (button0.fallingEdge()) {
    playSdWav1.stop();
  }
  

}

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:
https://github.com/PaulStoffregen/A...e_Playing/Part_1_05_Do_More_While_Playing.ino
&
https://www.pjrc.com/teensy/td_libs_Bounce.html

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

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

dywen
 
Last edited:
Status
Not open for further replies.
Back
Top