Code
----
[quote]<pre>[<span style="color=: #7E7E7E];">//Ian Petrov VIS147A Final[Ian Petrov VIS147A Final</color]span>[<span style="color=: #7E7E7E];">//code edited from Adafruit Wave Shield Site[code edited from Adafruit Wave Shield Site</color]span>#include include <FatReader.h>#include include <SdReader.h>#include include <avr/pgmspace.h>#include [include <span style="color=: #006699];">"WaveUtil.h"[</color]span>#include [include <span style="color=: #006699];">"WaveHC.h"[</color]span>
SdReader cardSdReader [card; <span style="color=: #7E7E7E];">// This object holds the information for the card[</color]span>FatVolume volFatVolume [vol; <span style="color=: #7E7E7E];">// This holds the information for the partition on the card[</color]span>FatReader rootFatReader root; [ <span style="color=: #7E7E7E];">// This holds the information for the filesystem on the card[</color]span>FatReader fFatReader [f; <span style="color=: #7E7E7E];">// This holds the information for the file we're play[</color]span>
WaveHC waveWaveHC [wave; <span style="color=: #7E7E7E];">// This is the only wave (audio) object, since we will only play one at a time[</color]span>
#define DEBOUNCE 100 [define DEBOUNCE 100 <span style="color=: #7E7E7E];">// button debouncer[</color]span>
[<span style="color=: #7E7E7E];">// this handy function will return the number of bytes currently free in RAM this handy function will return the number of bytes currently free in RAM, great for debugging great for debugging! [ </color]span>[<span style="color=: #CC6600];">int[</color] span> freeRam([<span style="color=: #CC6600];">void[</color]span>)
{
extern [ extern <span style="color=: #CC6600];">int[</color] span> __bss_end; extern [ extern <span style="color=: #CC6600];">int[</color] span> *__brkval; [ <span style="color=: #CC6600];">int[</color] span> free_memory; [ <span style="color=: #CC6600];">if[</color]span>(([<span style="color=: #CC6600];">int[</color]span>)__brkval == 0) { free_memory free_memory = (([<span style="color=: #CC6600];">int[</color]span>)&free_memory) - (([<span style="color=: #CC6600];">int[</color]span>)&__bss_end); } [ <span style="color=: #CC6600];">else[</color] span> { free_memory free_memory = (([<span style="color=: #CC6600];">int[</color]span>)&free_memory) - (([<span style="color=: #CC6600];">int[</color]span>)__brkval); } [ <span style="color=: #CC6600];">return[</color] span> free_memory; }
[<span style="color=: #CC6600];">void[</color] span> sdErrorCheck([<span style="color=: #CC6600];">void[</color]span>)
{
[ <span style="color=: #CC6600];">if[</color] span> (!card.errorCode()) [<span style="color=: #CC6600];">return[</color]span>; putstring putstring([<span style="color=: #006699];">"\n\rSD I/O error: "[</color]span>); [ <span style="color=: #CC6600][;"><b]>Serial[</b][></color]span>.[<span style="color=: #CC6600];">print[</color]span>(card.errorCode(), [<span style="color=: #006699];">HEX[</color]span>); putstring putstring([<span style="color=: #006699];">", "[</color]span>); [ <span style="color=: #CC6600][;"><b]>Serial[</b][></color]span>.[<span style="color=: #CC6600];">println[</color]span>(card.errorData(), [<span style="color=: #006699];">HEX[</color]span>); [ <span style="color=: #CC6600];">while[</color]span>(1);
}
[<span style="color=: #CC6600];">void[</span> <span style="color] [color=: #CC6600][;"><b]>setup[</b][></color]span>() { [ <span style="color=: #7E7E7E];">// set up serial port[</color]span> [ <span style="color=: #CC6600][;"><b]>Serial[</b][></color]span>.[<span style="color=: #CC6600];">begin[</color]span>(9600); putstring_nl putstring_nl([<span style="color=: #006699];">"WaveHC with 6 buttons"[</color]span>); putstring putstring([<span style="color=: #006699];">"Free RAM: "[</color]span>); [<span style="color=: #7E7E7E];">// This can help with debugging, running out of RAM is bad[</color]span> [ <span style="color=: #CC6600][;"><b]>Serial[</b][></color]span>.[<span style="color=: #CC6600];">println[</color]span>(freeRam()); [<span style="color=: #7E7E7E];">// if this is under 150 bytes it may spell trouble![</color]span> [ <span style="color=: #7E7E7E];">// Set the output pins for the DAC control. This pins are defined in the library[</color]span> [ <span style="color=: #CC6600];">pinMode[</color]span>(2, [<span style="color=: #006699];">OUTPUT[</color]span>); [ <span style="color=: #CC6600];">pinMode[</color]span>(3, [<span style="color=: #006699];">OUTPUT[</color]span>); [ <span style="color=: #CC6600];">pinMode[</color]span>(4, [<span style="color=: #006699];">OUTPUT[</color]span>); [ <span style="color=: #CC6600];">pinMode[</color]span>(5, [<span style="color=: #006699];">OUTPUT[</color]span>); [ <span style="color=: #7E7E7E];">// pin13 LED[</color]span> [ <span style="color=: #CC6600];">pinMode[</color]span>(13, [<span style="color=: #006699];">OUTPUT[</color]span>); [ <span style="color=: #7E7E7E];">// enable pull-up resistors on switch pins (analog inputs)[</color]span> [ <span style="color=: #CC6600];">digitalWrite[</color]span>(14, [<span style="color=: #006699];">HIGH[</color]span>); [ <span style="color=: #CC6600];">digitalWrite[</color]span>(15, [<span style="color=: #006699];">HIGH[</color]span>); [ <span style="color=: #CC6600];">digitalWrite[</color]span>(16, [<span style="color=: #006699];">HIGH[</color]span>); [ <span style="color=: #CC6600];">digitalWrite[</color]span>(17, [<span style="color=: #006699];">HIGH[</color]span>); [ <span style="color=: #CC6600];">digitalWrite[</color]span>(18, [<span style="color=: #006699];">HIGH[</color]span>); [ <span style="color=: #CC6600];">digitalWrite[</color]span>(19, [<span style="color=: #006699];">HIGH[</color]span>); [ <span style="color=: #7E7E7E];">// if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you[</color]span> [ <span style="color=: #CC6600];">if[</color] span> (!card.init()) { [<span style="color=: #7E7E7E];">//play with 8 MHz spi (default faster!) [</color]span> putstring_nl putstring_nl([<span style="color=: #006699];">"Card init. failed!"[</color]span>); [<span style="color=: #7E7E7E];">// Something went wrong, lets print out why[</color]span> sdErrorCheck sdErrorCheck(); [ <span style="color=: #CC6600];">while[</color]span>(1); [<span style="color=: #7E7E7E];">// then 'halt' - do nothing![</color]span> } [ <span style="color=: #7E7E7E];">// enable optimize read - some cards may timeout. Disable if you're having problems[</color]span> card card.partialBlockRead([<span style="color=: #CC6600];">true[</color]span>); [<span style="color=: #7E7E7E];">// Now we will look for a FAT partition Now we will look for a FAT partition![</color]span> uint8_t part uint8_t part; [ <span style="color=: #CC6600];">for[</color] span> (part = 0; part < 5; part++) { [<span style="color=: #7E7E7E];">// we have up to 5 slots to look in[</color]span> [ <span style="color=: #CC6600];">if[</color] span> (vol.init(card, part)) [ <span style="color=: #CC6600];">break[</color]span>; [<span style="color=: #7E7E7E];">// we found one, lets bail[</color]span> } [ <span style="color=: #CC6600];">if[</color] span> (part == 5) { [<span style="color=: #7E7E7E];">// if we ended up not finding one :([</color]span> putstring_nl putstring_nl([<span style="color=: #006699];">"No valid FAT partition!"[</color]span>); sdErrorCheck sdErrorCheck(); [ <span style="color=: #7E7E7E];">// Something went wrong, lets print out why[</color]span> [ <span style="color=: #CC6600];">while[</color]span>(1); [<span style="color=: #7E7E7E];">// then 'halt' - do nothing![</color]span> } [ <span style="color=: #7E7E7E];">// Lets tell the user about what we found[</color]span> putstring putstring([<span style="color=: #006699];">"Using partition "[</color]span>); [ <span style="color=: #CC6600][;"><b]>Serial[</b][></color]span>.[<span style="color=: #CC6600];">print[</color]span>(part, [<span style="color=: #006699];">DEC[</color]span>); putstring putstring([<span style="color=: #006699];">", type is FAT"[</color]span>); [ <span style="color=: #CC6600][;"><b]>Serial[</b][></color]span>.[<span style="color=: #CC6600];">println[</color]span>(vol.fatType(),[<span style="color=: #006699];">DEC[</color]span>); [<span style="color=: #7E7E7E];">// FAT16 or FAT32?[</color]span> [ <span style="color=: #7E7E7E];">// Try to open the root directory[</color]span> [ <span style="color=: #CC6600];">if[</color] span> (!root.openRoot(vol)) { putstring_nl putstring_nl([<span style="color=: #006699];">"Can't open root dir!"[</color]span>); [<span style="color=: #7E7E7E];">// Something went wrong,[</color]span> [ <span style="color=: #CC6600];">while[</color]span>(1); [<span style="color=: #7E7E7E];">// then 'halt' - do nothing![</color]span> } [ <span style="color=: #7E7E7E];">// Whew! We got past the tough parts.[</color]span> putstring_nl putstring_nl([<span style="color=: #006699];">"Ready!"[</color]span>);
}
[<span style="color=: #CC6600];">void[</color] span> playcomplete([<span style="color=: #CC6600];">char[</color] span> good) { [ <span style="color=: #7E7E7E];">// call our helper to find and play this name[</color]span> playfile playfile([<span style="color=: #006699];">"GOOD.WAV"[</color]span>); [ <span style="color=: #CC6600];">while[</color] span> (wave.isplaying) { [ <span style="color=: #7E7E7E];">// do nothing while its playing[</color]span> } [ <span style="color=: #7E7E7E];">// now its done playing[</color]span>
}
[<span style="color=: #CC6600];">void[</span> <span style="color] [color=: #CC6600][;"><b]>loop[</b][></color]span>() { [ <span style="color=: #7E7E7E];">//putstring("."); // uncomment this to see if the loop isnt running [</color]span> [ <span style="color=: #CC6600];">switch[</color] span> (check_switches()) { [<span style="color=: #7E7E7E];">//this is the loop which cycles the arnold sounds based[</color]span> [ <span style="color=: #CC6600];">case[</color] span> 1: [<span style="color=: #7E7E7E];">//on which switch you connect to.[</color]span> playcomplete playcomplete([<span style="color=: #006699];">"STOP.WAV"[</color]span>); [ <span style="color=: #CC6600];">break[</color]span>; [ <span style="color=: #CC6600];">case[</color] span> 2: playcomplete playcomplete([<span style="color=: #006699];">"SOB.WAV"[</color]span>); [ <span style="color=: #CC6600];">break[</color]span>; [ <span style="color=: #CC6600];">case[</color] span> 3: playcomplete playcomplete([<span style="color=: #006699];">"DONT.WAV"[</color]span>); [ <span style="color=: #CC6600];">break[</color]span>; [ <span style="color=: #CC6600];">case[</color] span> 4: playcomplete playcomplete([<span style="color=: #006699];">"LACK.WAV"[</color]span>); [ <span style="color=: #CC6600];">break[</color]span>; [ <span style="color=: #CC6600];">case[</color] span> 5: playcomplete playcomplete([<span style="color=: #006699];">"FIST.WAV"[</color]span>); }
}
[<span style="color=: #CC6600];">byte[</color] span> check_switches()
{
[ <span style="color=: #CC6600];">static[</span> <span style="color] [color=: #CC6600];">byte[</color] span> previous[6]; [ <span style="color=: #CC6600];">static[</span> <span style="color] [color=: #CC6600];">long[</color] span> time[6]; [ <span style="color=: #CC6600];">byte[</color] span> reading; [ <span style="color=: #CC6600];">byte[</color] span> pressed; [ <span style="color=: #CC6600];">byte[</color] span> index; pressed pressed = 0 0;
[ <span style="color=: #CC6600];">for[</color] span> ([<span style="color=: #CC6600];">byte[</color] span> index = 0; index < 6; ++index) { reading reading = [ <span style="color=: #CC6600];">digitalRead[</color]span>(14 + index); [ <span style="color=: #CC6600];">if[</color] span> (reading == [<span style="color=: #006699];">LOW[</color] span> && previous[index] == [<span style="color=: #006699];">HIGH[</color] span> && [<span style="color=: #CC6600];">millis[</color]span>() - time[index] > DEBOUNCE) { [ <span style="color=: #7E7E7E];">// switch pressed[</color]span> time time[index] = [ <span style="color=: #CC6600];">millis[</color]span>(); pressed pressed = index index + 1 1; [ <span style="color=: #CC6600];">break[</color]span>; } previous previous[index] = reading reading; } [ <span style="color=: #7E7E7E];">// return switch number (1 - 6)[</color]span> [ <span style="color=: #CC6600];">return[</color] span> (pressed);
}
[<span style="color=: #7E7E7E];">// Plays a full file from beginning to end with no pause Plays a full file from beginning to end with no pause.[</color]span>[<span style="color=: #CC6600];">void[</color] span> playcomplete([<span style="color=: #CC6600];">char[</color] span> *name) { [ <span style="color=: #7E7E7E];">// call our helper to find and play this name[</color]span> playfile playfile(name); [ <span style="color=: #CC6600];">while[</color] span> (wave.isplaying) { [ <span style="color=: #7E7E7E];">// do nothing while its playing[</color]span> } [ <span style="color=: #7E7E7E];">// now its done playing[</color]span>
}
[<span style="color=: #CC6600];">void[</color] span> playfile([<span style="color=: #CC6600];">char[</color] span> *name) { [ <span style="color=: #7E7E7E];">// see if the wave object is currently doing something[</color]span> [ <span style="color=: #CC6600];">if[</color] span> (wave.isplaying) {[<span style="color=: #7E7E7E];">// already playing something, so stop it![</color]span> wave wave.[<span style="color=: #CC6600];">stop[</color]span>(); [<span style="color=: #7E7E7E];">// stop it[</color]span> } [ <span style="color=: #7E7E7E];">// look in the root directory and open the file[</color]span> [ <span style="color=: #CC6600];">if[</color] span> (!f.open(root, name)) { putstring putstring([<span style="color=: #006699];">"Couldn't open file "[</color]span>); [<span style="color=: #CC6600][;"><b]>Serial[</b][></color]span>.[<span style="color=: #CC6600];">print[</color]span>(name); [<span style="color=: #CC6600];">return[</color]span>; } [ <span style="color=: #7E7E7E];">// OK read the file and turn it into a wave object[</color]span> [ <span style="color=: #CC6600];">if[</color] span> (!wave.create(f)) { putstring_nl putstring_nl([<span style="color=: #006699];">"Not a valid WAV"[</color]span>); [<span style="color=: #CC6600];">return[</color]span>; } [ <span style="color=: #7E7E7E];">// ok time to play! start playback[</color]span> wave wave.play();
}
[</quote]pre>