Created
May 18, 2016 01:39
-
-
Save takawo/4f0ea16e7be045e059758375626d2258 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #include <WaveHC.h> | |
| #include <WaveUtil.h> | |
| #define FSRPin 0 // Pin Num for FSR | |
| #define HS1Pin 1 // Pin Num for HS1 | |
| #define HS2Pin 2 // Pin Num for HS2 | |
| #define PMPin 3 // Pin Num for PM | |
| #define ledFSR 11 // Pin for LED detect FSR status | |
| #define ledHS1 12 // Pin for LED detect HS1 status | |
| #define ledHS2 13 // Pin for LED detect HS2 status | |
| #define MaxSoundFiles 6 // max number of Sound files | |
| #define error(msg) error_P(PSTR(msg)) | |
| 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 | |
| // put pi in flash memory | |
| const char sound_file[] PROGMEM = "12345789"; | |
| uint16_t HS_threshold = 1000; | |
| uint16_t FSR_ref = 0; //Holl Sensors' refernce | |
| unsigned digit = 0; | |
| void setup() { | |
| // put your setup code here, to run once: | |
| Serial.begin(9600); | |
| // setup Digital Output | |
| pinMode(ledFSR, OUTPUT); | |
| pinMode(ledHS1, OUTPUT); | |
| pinMode(ledHS2, OUTPUT); | |
| 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(); | |
| } | |
| void loop() { | |
| uint16_t FSR_value = analogRead(0); | |
| uint16_t HS1_value = analogRead(1); | |
| uint16_t HS2_value = analogRead(2); | |
| uint16_t PM_value = analogRead(3); | |
| boolean FSR_flag = false; | |
| if (FSR_value >= PM_value) { | |
| FSR_flag = true; | |
| } else { | |
| FSR_flag = false; | |
| } | |
| boolean HS1_flag = false; | |
| if (HS1_flag >= HS_threshold) { | |
| HS1_flag = true; | |
| } else { | |
| HS1_flag = false; | |
| } | |
| boolean HS2_flag = false; | |
| if (HS2_flag >= HS_threshold) { | |
| HS2_flag = true; | |
| } else { | |
| HS2_flag = false; | |
| } | |
| boolean HS_flag = false; | |
| if (HS1_flag == true || HS2_flag == true ) { | |
| HS_flag = true; | |
| } else { | |
| HS_flag = false; | |
| } | |
| if (HS_flag == true && FSR_flag == true) { | |
| Serial.println("SOUND TRIGGER"); | |
| } | |
| delay(10); | |
| } | |
| void playsound(void) { | |
| uint8_t soundFileNum = random(MaxSoundFiles); // randomize sound file's Number | |
| while (soundFileNum == FSR_ref) { | |
| soundFileNum = random(MaxSoundFiles); | |
| } | |
| FSR_ref = soundFileNum; | |
| char c = pgm_read_byte(&sound_file[soundFileNum]); | |
| if (c == 0) { | |
| digit = 0; | |
| Serial.println(); | |
| return; | |
| } | |
| Serial.print("\n\a"); | |
| Serial.write("playing "); | |
| Serial.write(c); | |
| Serial.write(".wav"); | |
| Serial.print("\n\a"); | |
| speakfile(c); | |
| } | |
| /////////////////////////////////// HELPERS | |
| char filename[13]; | |
| void speakfile(char c) { | |
| uint8_t i = 0; | |
| // copy flash string for 'period' to filename | |
| strcpy_P(filename, PSTR("0.WAV")); | |
| if ('0' <= c && c <= '9') { | |
| // digit - change 'P' to digit | |
| filename[0] = c; | |
| i = 1; | |
| } | |
| playcomplete(filename); | |
| } | |
| /* | |
| 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