I have made a series of sensors that when triggered should play a specific audio file. Is there any reason that I would only be able to use three different audio files? I am hoping that I can use five but it seems to only be working with three. Code included below.
#include <SD.h>
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're play
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
void setup()
{
Serial.begin(9600); //Setup Serial
pinMode(14, INPUT); //Assigns analog pins 0-4 as sensor inputs
pinMode(15, INPUT);
pinMode(16, INPUT);
pinMode(17, INPUT);
pinMode(18, INPUT);
pinMode(19, INPUT);
pinMode(6, OUTPUT); //Assigns digital pin 6 as output
pinMode(7, OUTPUT); //Assigns analog pin 7 as output for motor
pinMode(8, OUTPUT); //Assigns digital pin 8 as output for LED cluster 1
pinMode(9, OUTPUT); //Assigns digital pin 9 as output for LED cluster 2
// Set the output pins for the DAC control. This pins are defined in the library
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(10, OUTPUT);
if (!card.init()) { //play with 8 MHz spi (default faster!)
putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
sdErrorCheck();
// while(1); // then 'halt' - do nothing!
}
// enable optimize read - some cards may timeout. Disable if you're having problems
// card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // if we ended up not finding one :(
putstring_nl("No valid FAT partition!");
sdErrorCheck(); // Something went wrong, lets print out why
// while(1); // then 'halt' - do nothing!
}
// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
// while(1); // then 'halt' - do nothing!
}
// Whew! We got past the tough parts.
putstring_nl("Ready!");
}
void sdErrorCheck(void)
{
if (!card.errorCode())
putstring("\n\rSD I/O error: ");
Serial.print(card.errorCode(), HEX);
putstring(", ");
Serial.println(card.errorData(), HEX);
// while(1);
}
void playfile(char *name) {
// see if the wave object is currently doing something
if (wave.isplaying) {// already playing something, so stop it!
wave.stop(); // stop it
}
// look in the root directory and open the file
if (!f.open(root, name)) {
putstring("Couldn't open file ");
Serial.print(name);
}
// OK read the file and turn it into a wave object
if (!wave.create(f)) {
putstring_nl("Not a valid WAV");
}
// ok time to play! start playback
wave.play();
}
void ghost()
{
int blinky = 0;
while(blinky < 40)
{
digitalWrite(8,HIGH);
delay(50); //set proper delay here for ghost to go to the end
digitalWrite(8,LOW);
delay(50);
blinky++;
}
delay(1);
}
/*void strobe1()
{
int blinky = 0;
while(blinky<12)
{
digitalWrite(8,HIGH); //Blink LED cluster 1 and 2
digitalWrite(9,HIGH); //for first half of cat scream
delay(40);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(40);
blinky++;
}
}*/
void strobe2()
{
int blinky = 0;
while(blinky < 16)
{
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(80);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(80);
blinky++;
}
}
void strobe4()
{
int blinky = 0;
while(blinky < 80)
{
digitalWrite(9,HIGH);
delay(50);
digitalWrite(9,LOW);
delay(50);
blinky++;
}
delay(1);
}
void strobe5()
{
int blinky = 0;
while(blinky < 40)
{
digitalWrite(9,HIGH);
delay(40);
digitalWrite(9,LOW);
delay(30);
blinky++;
}
delay(1);
}
// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
//call our helper to find and play this name
playfile(name);
while (wave.isplaying) {
// do nothing while its playing
}
// now its done playing
}
void loop()
{
if(digitalRead(14) == HIGH)
{
// blinky=0;
playfile("cat.WAV");
while (wave.isplaying)
{
//strobe1(); //try blinking light without function, you are already in a while loop
digitalWrite(8,HIGH); //Blink LED cluster 1 and 2
digitalWrite(9,HIGH); //for first half of cat scream
delay(40);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(40);
//blinky++;
}
// putstring_nl("finish sensor 1");
}
else if(digitalRead(15)==HIGH)
{
playfile("help.WAV");
while(wave.isplaying)
{
//strobe2();
}
}
else if(digitalRead(16)==HIGH)
{
// digitalWrite(7,HIGH);
playfile("ghost.WAV");
while(wave.isplaying)
{
//ghost();
}
// digitalWrite(7,LOW);
}
else if(digitalRead(17)==HIGH)
{
playfile("help.WAV");
while(wave.isplaying)
{
//strobe4();
}
}
else if(digitalRead(19)==HIGH)
{
playfile("ghost.WAV");
while(wave.isplaying)
{
//strobe5();
}
}
}