I'm looking for anyone with expertise in getting any of these SD card music playing libraries to work. I have this beautiful custom shield all built up for my freetronics usb droid r3 but all my tests and google results have not been able to get a music output. Many people on instructables and youtube have apparently got this working with a simple understanding and a low amount of hardware + software. That's pretty frustrating since i'm sure i've followed all instructions and manuals available.
First, I will start with my knowledge of SD card interfacing. An SD card must be formatted with the official formatting software in place of operating system formatters. The card must not be formatted more than, say, 5 times quick format and I have experienced the destruction of a card due to. My second card was quick formatted to FAT32 I have heard that cards below 2GB must be FAT16 but design but it's just something to tick off. The files on board are 8bit, 8-32Khz sample rate, mono wav files with the 8.3 naming convention as per the limits of the tmrpcm library.
I did find that the output for SD.begin() was false, indicating that the card could not be initialized. It's my only lead since i've tested all my sensor logic with different outputs such as writing to serial or just a plain analogWrite(127). That's all working so everything points to either the tmrpcm library or the SD card. Google had a lot of helpless souls with uninitialized SD cards but none of the solutions there seem to help. Where to from here?
#include <pcmConfig.h>
#include <pcmRF.h>
#include <TMRpcm.h>
#include <SD.h>
#include <SPI.h> //due to a problem with the SD library, research suggest this must be included to access that library.
#define SDChipSelectPin 10 //we use this pin as a sensor to detect if sd card is present.
TMRpcm tmrpcm; //creates an object pointing at the TMRpcm library for use in this sketch.
void setup()
{
Serial.begin(9600); // allows communication with the computer at 9600 bits/s.
if (!SD.begin(SDChipSelectPin))
{
Serial.println("Can't detect sd card."); //check the sd card is working.
return;
}
else
{
Serial.println("SD card detected.");
}
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT); //chip select pin for spi must be a constant output.
digitalWrite(10, HIGH); //this line is just a google solution; it doesnt seem to change much.
if (!SD.begin()) {
Serial.println("Failed!");
while (true); //end program at the error.
}
Serial.println("SD card initialized.");
if (!SD.open("sound1.wav"))
{
Serial.println("could not open file.");
while (true); //end program at the error.
}
pinMode(14,INPUT); //pin A0.
pinMode(15,INPUT); //pin A1.
pinMode(16,INPUT); //pin A2.
pinMode(17,INPUT); //pin A3.
pinMode(18,INPUT); //pin A4.
pinMode(19,INPUT); //pin A5.
pinMode(6,OUTPUT); //PWM pin D6. //pulse width modulated output with constant voltage of vcc.
tmrpcm.speakerPin = 6;
tmrpcm.volume(1);
tmrpcm.setVolume(4);
}
void loop()
{
float pinA0 = analogRead(14) * (5.0 / 1023.0); //this equation converts the analogue read value (0-255) into a float voltage value similar to input.
float pinA1 = analogRead(15) * (5.0 / 1023.0);
float pinA2 = analogRead(16) * (5.0 / 1023.0);
float pinA3 = analogRead(17) * (5.0 / 1023.0);
float pinA4 = analogRead(18) * (5.0 / 1023.0);
float pinA5 = analogRead(19) * (5.0 / 1023.0);
if (pinA0 > 3)
{
Serial.println("Button 1 pressed.");
tmrpcm.stopPlayback();
tmrpcm.play("sound1.wav"); //the maximum values played are 32KHz sampling, mono 8 bit unsigned WAV.
delay(200);
}
if (pinA1 > 3)
{
Serial.println("Button 2 pressed.");
tmrpcm.stopPlayback();
tmrpcm.play("sound2.wav");
delay(200);
}
if (pinA2 > 3)
{
Serial.println("Button 3 pressed.");
tmrpcm.stopPlayback();
tmrpcm.play("sound3.wav");
delay(200);
}
if (pinA3 > 3)
{
Serial.println("Button 4 pressed.");
tmrpcm.stopPlayback();
tmrpcm.play("sound4.wav");
delay(200);
}
if (pinA4 > 3)
{
Serial.println("Button 5 pressed.");
tmrpcm.stopPlayback();
tmrpcm.play("sound5.wav");
delay(200);
}
if (pinA5 > 3)
{
Serial.println("Button 6 pressed.");
tmrpcm.stopPlayback();
tmrpcm.play("sound6.wav");
delay(200);
}
Serial.print(pinA0);
Serial.print(" ");
Serial.print(pinA1);
Serial.print(" ");
Serial.print(pinA2);
Serial.print(" ");
Serial.print(pinA3);
Serial.print(" ");
Serial.print(pinA4);
Serial.print(" ");
Serial.println(pinA5);
}
