Simple wav player code help?

Status
Not open for further replies.

cristo1

New member
Hi everyone :) I'm having some trouble making my wav player project work correctly. Please let me explain that I do not know how to write code properly. But I have managed to get it to work ( somewhat ).
I'm using a teensy 3.6 and the audio board to play wav files. I have managed to get the teensy to play,stop, and replay the wav files when I push an external pushbutton connected to pin 24. Which is exactly what I want it to do.
The part that I'm having trouble with is that I need to have the onboard led light when the wav file is playing ( when I push the button ); then have the led turn off when the wav file stops playing ( or when I push the button to stop it ).
I'm fairly certain this can be done. In fact, with trial and error I did get the led to light when the wav file wasn't playing and go off when it was playing ( so opposite of what I want it to do )? I've been trying for a few hours to rearrange the code to get what I want. But it's not happening :( And like I said: I don't know how to code properly. I've been copying and pasting and just doing the trial and error thing.

This is the code I'm working with:

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Bounce.h>

AudioPlaySdWav           playSdWav1;
AudioOutputI2S           audioOutput;
AudioConnection          patchCord1(playSdWav1, 0, audioOutput, 0);
AudioConnection          patchCord2(playSdWav1, 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
#define BUTTON 24
#define LED 13

int ledValue = HIGH;
Bounce bouncer = Bounce( BUTTON, 5 ); 

void setup() {
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.45);
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  SD.begin(SDCARD_CS_PIN);
  pinMode(BUTTON,INPUT);
  pinMode(LED,OUTPUT);
}

void loop() {

   if ( bouncer.update() ) {
     if ( bouncer.read() == LOW) {
        if ((playSdWav1.isPlaying() == false) || (ledValue == HIGH) ) {
          playSdWav1.play("test1.WAV");
          delay(500);    
          ledValue = LOW; 
             
       } 
       else if ((playSdWav1.isPlaying() == false) || (ledValue == LOW) ) {        
         playSdWav1.stop();
         delay(500);
         ledValue = LOW;
        
       }
       digitalWrite(LED,ledValue);
     }
   }
 }


Please, any help, code correction, or guidance would be much appreciated :)

BTW - I can appreciate if someone tells me to go buy an mp3 player. I bought the teensy 3.6 a few years ago for a different iteration of the same project which was scrapped. So it's just been sitting in my parts bin ever since. I would really like to use it.
I'm working with ELF sine waves and had been using an old iphone to play files @ 7 hz ( which is why I need the led ). That works somewhat. I tested the teensy and audio board and it works much better for my project.

Thanks in advance :)
 
The part that I'm having trouble with is that I need to have the onboard led light when the wav file is playing ( when I push the button ); then have the led turn off when the wav file stops playing ( or when I push the button to stop it ).

Probably the simplest way would be to just repetitively check in loop() whether it actually is or is not playing, and change the LED accordingly. Maybe like this.

Code:
  playSdWav1.isPlaying()) {
    digitalWrite(LED, HIGH);
  } else {
    digitalWrite(LED, LOW);
  }

There are many other possible ways, most of them more efficient. But this is probably the simplest and easiest, so try it first.
 
Status
Not open for further replies.
Back
Top