It might be helpful for you to post the entire program, plus the exact crash information. (E.g., does the println of the data just before the crash look ok?)
the program prints the following two lines before crashing:
1
885
the first number is the position of the swtich and the second number is the reading from the potentiometer.
here's my code (some explanation below):
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
SdReader card;
FatVolume vol;
FatReader root;
FatReader f;
uint8_t dirLevel; // indent level for file/dir names
dir_t dirBuf; // buffer for directory reads
WaveHC wave; // only one!
int ledPin = 13; // LED
int inputPin = 14; // on/off switch
int val = 0; // variable for reading the pin status
// debounce stuff
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
// potentiometer
int intPin = 3;
int intVal = 0;
// play recursively - possible stack overflow if subdirectories too nested
void play(FatReader &dir)
{
FatReader file;
while (dir.readDir(dirBuf) > 0) {
// skip . and .. directories
if (dirBuf.name[0] == '.') continue;
Serial.println();
for (uint8_t i = 0; i < dirLevel; i++) Serial.print(' ');
if (!file.open(vol, dirBuf)) {
Serial.println("file.open failed");
while(1);
}
if (file.isDir()) {
putstring("Subdir: ");
dirLevel += 2;
// play files in subdirectory
play(file);
dirLevel -= 2;
}
else {
putstring("Playing ");
if (!wave.create(file)) {
putstring(" Not a valid WAV");
}
else {
Serial.println();
wave.play();
while (wave.isplaying) {
putstring(".");
delay(100);
}
}
}
}
}
void setup() {
// set up serial port
Serial.begin(9600);
// set up waveshield pins
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare switch as input
digitalWrite(inputPin, HIGH);
if (!card.init()) {//play with 8 MHz spi
putstring_nl("Card init. failed!");
while(1);
}
// enable optimize read - some cards may timeout
card.partialBlockRead(true);
uint8_t part;
for (part = 0; part < 5; part++) {
if (vol.init(card, part)) break;
}
if (part == 5) {
putstring_nl("No valid FAT partition!");
while(1);
}
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!");
while(1);
}
dirLevel = 0;
}
void test() {
intVal = analogRead(intPin);
Serial.println(intVal);
if (intVal <= 300) {
playcomplete("SOUND1.WAV");
}
else if (intVal > 300 && intVal <= 700) {
playcomplete("SOUND2.WAV");
}
else if (intVal > 700) {
playcomplete("SOUND3.WAV");
}
}
void loop(){
reading = digitalRead(inputPin);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
// ... invert the output
if (state == HIGH)
state = LOW;
else
state = HIGH;
// ... and remember when the last button press was
time = millis();
}
Serial.println(reading);
if (previous != reading) {
digitalWrite(ledPin, HIGH); // turn LED ON
test();
}
previous = reading;
}
void playcomplete(char *name) {
playfile(name);
while (wave.isplaying);
}
void playfile(char *name) {
if (wave.isplaying) {// already playing something, so stop it!
wave.stop(); // stop it
}
if (!f.open(root, name)) {
putstring("Couldn't open file ");
Serial.print(name);
return;
}
if (!wave.create(f)) {
putstring_nl("Not a valid WAV");
return;
}
// ok time to play!
wave.play();
}
this code is for lady ada's wave shield using the WaveHC library. i've been coaxing this code into doing new things for me, step-by-step, so i'm pretty sure the error is of my own doing.
in case it helps, or anyone is interested here is the original code to generate a sound when a button is pressed.
what i'm trying to do is generate a sound when a switch is flipped, the sound will be different depending on the value of the potentiometer.
specifically, the software problem seems to be with the test() function, however, i also had problems when using a switch statement instead of conditionals, so if there's nothing really wrong with my code, maybe it's to do with the heaviness of the wav files?