Hey guys
I'v put together some code to trigger a sound based on a distance using an arduino, wave shield, and sonar distance sensor. I am having a problem in that it will only play 6 sound files. If I had a 7th to the code, the whole thing stops working. I am at a loss for why this is happening.
#include "SdCard.h"
#include "FatReader.h"
#include "WaveHC.h"
#include <avr/pgmspace.h>
#include "WaveUtil.h"
SdCard card;
FatVolume vol;
FatReader root;
FatReader f;
WaveHC wave; // only one!
#define head1 6
#define head2 7
#define head3 8 //bright led
#define head4 9
#define head5 18
#define distSignal 19
void printName(dir_t &dir)
{
for (uint8_t i = 0; i < 11; i++) {
if (dir.name[i] == ' ')continue;
if (i == 8) Serial.print('.');
Serial.print(dir.name[i]);
}
if (DIR_IS_SUBDIR(dir)) Serial.print('/');
}
int randNum;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(head1, OUTPUT);
pinMode(head2, OUTPUT);
pinMode(head3, OUTPUT);
pinMode(head4, OUTPUT);
pinMode(head5, OUTPUT);
pinMode(distSignal,INPUT);
randomSeed(analogRead(0));
//digitalWrite(head1, LOW);
//digitalWrite(head2, LOW);
//digitalWrite(head3, LOW);
//digitalWrite(head4, LOW);
//digitalWrite(head5, LOW);
if (!card.init()) {
putstring_nl("Card init. failed!");
return;
}
if (!vol.init(card)) {
putstring_nl("No partition!");
return;
}
if (!root.openRoot(vol)) {
putstring_nl("Couldn't open dir");
return;
}
putstring_nl("Files found:");
dir_t d;
root.rewind();
while (root.readDir(d) > 0) {
printName(d);
Serial.println();
}
}
int pumpkinstate=0;
int val=1, timeCount=0, loopCount = 0;
float distValue = 0;
void loop() {
delay(60);
timeCount = 0;
val = digitalRead(distSignal);
while(val == LOW)
{ // Loop until pin reads a high value
val = digitalRead(distSignal);
}
while(val == HIGH)
{ // Loop until pin reads a high value
val = digitalRead(distSignal);
timeCount = timeCount +1; // Count echo pulse time
}
distValue = distValue + timeCount;
loopCount = loopCount +1;
if (loopCount > 2)
{
distValue = distValue/27.5/12.0/3.0;
loopCount = 0;
if (distValue > 11)
{
pumpkinstate = 1;
}
if ((distValue > 6) && (distValue < 10) && (pumpkinstate == 1))
{
pumpkinstate = 2;
randNum = random(1,4);
switch (randNum)
{
case 1:
playcomplete("SPIRIT.WAV");
break;
case 2:
playcomplete("CLOSER.WAV");
break;
case 3:
playcomplete("WOODZIE.WAV");
break;
case 4:
playcomplete("DICKLE.WAV");
break;
default:
break;
}
}
if ((distValue > 1) && (distValue < 5) && (pumpkinstate == 2))
{
randNum = random(1,4);
pumpkinstate = 0;
switch (randNum)
{
case 1:
playcomplete("CHRIS.WAV");
break;
case 2:
playcomplete("GREG.WAV");
break;
/*case 3:
playcomplete("WOOD.WAV");
break; */
default:
break;
}
}
distValue = 0;
} //end loopCount
} // end loop
void playcomplete(char *name) {
int volume=0;
f.open(root, name);
wave.create(f);
wave.play();
while (wave.isplaying){
volume = analogRead(1)/4;
if (volume > 125) {
digitalWrite(head5, HIGH);
digitalWrite(head4, HIGH);
digitalWrite(head3, HIGH);
digitalWrite(head2, HIGH);
digitalWrite(head1, HIGH);
}
else
if (volume > 118) {
digitalWrite(head2, HIGH);
digitalWrite(head1, HIGH);
digitalWrite(head3, HIGH);
digitalWrite(head4, HIGH);
digitalWrite(head5, LOW);
}
else
if (volume > 110) {
digitalWrite(head1, HIGH);
digitalWrite(head2, HIGH);
digitalWrite(head3, LOW);
digitalWrite(head4, LOW);
digitalWrite(head5, LOW);
}
else {
digitalWrite(head1, HIGH);
digitalWrite(head2, LOW);
digitalWrite(head3, LOW);
digitalWrite(head4, LOW);
digitalWrite(head5, LOW);
}
volume = 0;
delay(2);
}
digitalWrite(head1, LOW);
digitalWrite(head2, LOW);
digitalWrite(head3, LOW);
digitalWrite(head4, LOW);
digitalWrite(head5, LOW);
}