Hi all,
For an installation, I want to build a simple system where an audio file is randomly choosed on the SD card after a button (and later, a sensor) is pressed.
I have 10 files on the SD card named SDTEST1.WAV, SDTEST2.WAV, SDTEST3.WAV etc.
The button is connected to Digital In #16 on my Teensy3.2 (+ audio shield).
The idea is to concatenate the file name from a string and start the play function automatically.
When the audio file finishes to play, to system stops, waiting for the button to be pressed again.
After many, many tests, I can't find a correct way to concatenate the file name (in the attached code, the issue appears line 85).
I tried a lot of other solutions but none of them is working so, I think I need your help !
Thanks
Nicolas.
For an installation, I want to build a simple system where an audio file is randomly choosed on the SD card after a button (and later, a sensor) is pressed.
I have 10 files on the SD card named SDTEST1.WAV, SDTEST2.WAV, SDTEST3.WAV etc.
The button is connected to Digital In #16 on my Teensy3.2 (+ audio shield).
The idea is to concatenate the file name from a string and start the play function automatically.
When the audio file finishes to play, to system stops, waiting for the button to be pressed again.
After many, many tests, I can't find a correct way to concatenate the file name (in the attached code, the issue appears line 85).
I tried a lot of other solutions but none of them is working so, I think I need your help !
Thanks
Nicolas.
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
const int buttonPin = 16;
long randNumber;
int buttonState = 0;
AudioPlaySdWav playSdWav1;
AudioOutputI2S i2s1;
AudioConnection patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection patchCord2(playSdWav1, 1, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1;
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
AudioMemory(8);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
SPI.setMOSI(7);
SPI.setSCK(14);
if (!(SD.begin(10))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
delay(1000);
}
void playFile(const char *filename)
{
Serial.print("Playing file: ");
Serial.println(filename);
// Start playing the file. This sketch continues to
// run while the file plays.
playSdWav1.play(filename);
// A brief delay for the library read WAV info
delay(5);
// Simply wait for the file to finish playing.
while (playSdWav1.isPlaying()) {
// uncomment these lines if you audio shield
// has the optional volume pot soldered
//float vol = analogRead(15);
//vol = vol / 1024;
// sgtl5000_1.volume(vol);
}
}
void loop() {
// cf : https://www.arduino.cc/en/Tutorial/Debounce
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is not, the buttonState is LOW:
if (buttonState == LOW) {
(playSdWav1.isPlaying() == true); // inverser true et false ?
Serial.println("Stop playing");
delay(10); // wait for library to parse WAV info
// do nothing while playing...
}
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
else (buttonState == HIGH); {
// defini le nom aleatoire du fichier Audio
randNumber = (random(4) + 4); // genere les valeurs 15 et 16 uniquement
String(myString1) = "SDTEST"; // avec ou sans parentheses autour de myString1 ?
String(myString2) = ".WAV "; // idem myString2 ?
String(myFile) = (myString1 + randNumber + myString2);
(playSdWav1.isPlaying() == false);
Serial.println("Start playing");
playSdWav1.play(String(myFile)); // le pb est bien ici. pourquoi le programme ne reconnait-il
// pas le nom du fichier ?
delay(10); // wait for library to parse WAV info
Serial.println(myFile); // comment stopper la fonction
// le temps de lire le fichier audio ?
}
}