Last active
May 16, 2016 05:25
-
-
Save takawo/9a8a358a96c40410a31b338c336fbffe 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
| /* | |
| * HELL_STAMP SOUND PLAY | |
| * | |
| */ | |
| #include <WaveHC.h> | |
| #include <WaveUtil.h> | |
| // put pi in flash memory | |
| const char sound_file[] PROGMEM = "12345789"; | |
| 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 FSRPin 0 // Pin Num for FSR | |
| #define FSR_threshold 1000 // threshold for FSR | |
| #define HollSens_threshold 1000 // threshold for FSR | |
| #define MaxSoundFiles 6 // max number of Sound files | |
| uint8_t FSR_ref = 0; // sound number reference | |
| boolean FSR_flag = 0; //FSR's flag | |
| #define HollSensPin1 1 // Pin Num for HolleSensor1 | |
| #define HollSensPin2 2 // Pin Num for HolleSensor2 | |
| #define ledPin 13 // Pin for LED | |
| boolean Holl_flag = 0; //Holl Sensors' flag | |
| boolean Holl_ref = 0; //Holl Sensors' refernce | |
| /* | |
| * Define macro to put error messages in flash memory | |
| */ | |
| #define error(msg) error_P(PSTR(msg)) | |
| //////////////////////////////////// SETUP | |
| void setup() { | |
| //set up LED pin to output | |
| pinMode(ledPin, OUTPUT); | |
| // 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; | |
| void loop() { | |
| //FSR Read | |
| uint32_t FSR_value = analogRead(FSRPin); | |
| if (FSR_value <= FSR_threshold) { | |
| FSR_flag = 1; | |
| } | |
| else { | |
| FSR_flag = 0; | |
| } | |
| //Hall Sensor Read | |
| uint16_t HS1_value = analogRead(HollSensPin1); | |
| uint16_t HS2_value = analogRead(HollSensPin2); | |
| //Hall Sensors Judge | |
| if (HS1_value > HollSens_threshold || HS2_value > HollSens_threshold) { | |
| Holl_flag = 1; | |
| digitalWrite(ledPin, HIGH); | |
| } | |
| else { | |
| Holl_flag = 0; | |
| digitalWrite(ledPin, LOW); | |
| } | |
| // Holl_flag debuging | |
| if (Holl_ref != Holl_flag){ | |
| if(Holl_flag == 1){ | |
| Serial.println("Holl_flag = HIGH"); | |
| } | |
| else { | |
| Serial.println("Holl_flag = LOW"); | |
| } | |
| } | |
| Holl_ref = Holl_flag; | |
| //Triger of playing a sound file | |
| if (FSR_flag == 1 && Holl_flag == 1) { | |
| playsound(); | |
| } | |
| 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