Hi guys.
I am trying to make a "phone" which would play a certain .wav track for a specific button. I have 4x4 keypad, DUE, amp and speaker. I have audio and keypad parts connected and working separately. Only problem is how to make a track play when I press the button.
I have this code:
#include <DAC.h>
#include <SD.h>
#include <SPI.h>
#include <Audio.h>
#include <Key.h>
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char* wavas;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
// debug output at 9600 baud
Serial.begin(9600);
// setup SD-card
SD.begin(10);
// debug output at 9600 baud
Audio.begin(44100, 100);
}
void loop() {
int count = 0;
//pressing the key -> assigning the filename
char customKey = customKeypad.getKey();
if (customKey == '1'){
char* wavas[]={"jonas.wav"};
Serial.println("playing jonas.wav");
}
if (customKey == '2'){
char* wavas[]={"petras.wav"};
Serial.println("playing petras.wav");
}
if (customKey == '3'){
char* wavas[]={"antanas.wav"};
Serial.println("playing antanas.wav");
}
// open wave file from sdcard
File myFile = SD.open(wavas);
const int S = 1024; // Number of samples to read in block
short buffer[S];
// until the file is not finished
while (myFile.available()) {
myFile.read(buffer, sizeof(buffer));
// Prepare samples
int volume = 1024;
Audio.prepare(buffer, S, volume);
Audio.write(buffer, S);
}
myFile.close();
while (true) ;
}
Seemed that it sometimes works if keep pressing the button as soon as I start serial monitor. Serial.println() works fine, it just the .wav is misbehaving. Btw, I don't really even need serial monitor, I use it for debugging.
Any ideas?
Cheers!