Created
October 16, 2014 18:56
-
-
Save nasser/bbd35a98f453d02a091f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Text-to-speech example to speak the first n digits of pi. | |
* The number is stored in flash, each digit is spoken one at a time. | |
*/ | |
#include <WaveHC.h> | |
#include <WaveUtil.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 volumes root directory | |
FatReader file; // This object represent the WAV file for a pi digit or period | |
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time | |
/* | |
* Define macro to put error messages in flash memory | |
*/ | |
#define error(msg) error_P(PSTR(msg)) | |
//////////////////////////////////// SETUP | |
void setup() { | |
// set up Serial library at 9600 bps | |
Serial.begin(9600); | |
PgmPrintln("Pi speaker"); | |
if (!card.init()) { | |
error("Card init. failed!"); | |
} | |
if (!vol.init(card)) { | |
error("No partition!"); | |
} | |
if (!root.openRoot(vol)) { | |
error("Couldn't open dir"); | |
} | |
PgmPrintln("Files found:"); | |
root.ls(); | |
} | |
/////////////////////////////////// LOOP | |
unsigned digit = 0; | |
// change this if motion sensor on different pin | |
int motionSensorPin = 8; | |
// change this to match the filename you want to play | |
char* filename = "SOUND.WAV"; | |
void loop() { | |
// get next character from flash memory | |
if(digitlRead(motionSensorPin)) { | |
playcomplete(filename); | |
} | |
delay(10); | |
} | |
/* | |
* print error message and halt | |
*/ | |
void error_P(const char *str) { | |
PgmPrint("Error: "); | |
SerialPrint_P(str); | |
sdErrorCheck(); | |
while(1); | |
} | |
/* | |
* print error message and halt if SD I/O error | |
*/ | |
void sdErrorCheck(void) { | |
if (!card.errorCode()) return; | |
PgmPrint("\r\nSD I/O error: "); | |
Serial.print(card.errorCode(), HEX); | |
PgmPrint(", "); | |
Serial.println(card.errorData(), HEX); | |
while(1); | |
} | |
/* | |
* Play a file and wait for it to complete | |
*/ | |
void playcomplete(char *name) { | |
playfile(name); | |
while (wave.isplaying); | |
// see if an error occurred while playing | |
sdErrorCheck(); | |
} | |
/* | |
* Open and start playing a WAV file | |
*/ | |
void playfile(char *name) { | |
if (wave.isplaying) {// already playing something, so stop it! | |
wave.stop(); // stop it | |
} | |
if (!file.open(root, name)) { | |
PgmPrint("Couldn't open file "); | |
Serial.print(name); | |
return; | |
} | |
if (!wave.create(file)) { | |
PgmPrintln("Not a valid WAV"); | |
return; | |
} | |
// ok time to play! | |
wave.play(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment