/***************************************************** * * * SPOOKY WHISPERING PRANK * * instructables.com/id/Spooky-Whispering-Prank/ * * * *****************************************************/ //Add libraries to the Arduino sketch. #include #include #include // Set constant pins for the sound FX board: // Transmit, Receive and Reset #define SFX_RX 8 #define SFX_TX 9 #define AUDIO_RESET 10 //Start a software serial connection for the Audio board. SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX); //Start an instance of the soundboard. This is the name of the board. Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, AUDIO_RESET); //Establish a charachter array for the audio filenames //If you decide to use .ogg files change WAV to OGG char filename[12] = " WAV"; // This is where you specify the actual filenames of your audio // Each name must have characters no matter what. // In other words, if the filename is 6 characters long, then // add two blank spaces between the quotes to make it 8 characters long. static const char PROGMEM bigStringTable[] = // play() index "00SCARE " "01SCARE " "02SCARE " "03SCARE " // 1-4 "04SCARE " "05SCARE " "06SCARE " "07SCARE " // 5-8 "08SCARE " "09SCARE " "10SCARE " "11SCARE " // 9-12 "12SCARE " "13SCARE " "14SCARE " "15SCARE "; // 13-16 // The threshold for the light sensor int threshold = 100; // The threshold for the audio sensor float soundCompare = 0.3; // A variable for measuring the peak-to-peak volts of the audio signal double volts; // The sample window (frequency) for reading the audio sensor (50 mS = 20Hz) const int sampleWindow = 50; unsigned int sample; // Setup the sketch by engaing the Software Serial and Hardware Serial connections void setup(void) { ss.begin(9600); Serial.begin(115200); } //The body of the sketch void loop(void) { // Read the light and the sound sensors int sensorValue = analogRead(A0); soundReading(); // If the light sensor is less than the comparison threshold if(sensorValue < threshold){ // And if the sensor hears something loud if (volts > soundCompare){ // Play some audio play(random(16)); delay(1000); //While ten seconds pass, if light goes on stop the sound and reset for (int i=0; i <= 100; i++){ sensorValue = analogRead(A0); delay(100); if(sensorValue > threshold){ sfx.stop(); break; } } } } } // Function for playing audio void play(uint16_t i) { memcpy_P(filename, &bigStringTable[i * 8], 8); //changed 8 to 14 // PROGMEM -> RAM Serial.println(filename); sfx.playTrack(filename); } //Function for reading the audio sensor void soundReading(){ unsigned long startMillis= millis(); // Start of sample window unsigned int peakToPeak = 0; // peak-to-peak level unsigned int signalMax = 0; unsigned int signalMin = 1024; // collect data for 50 mS while (millis() - startMillis < sampleWindow) { sample = analogRead(A1); if (sample < 1024) // toss out spurious readings { if (sample > signalMax) { signalMax = sample; // save just the max levels } else if (sample < signalMin) { signalMin = sample; // save just the min levels } } } peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude volts = (peakToPeak * 5.0) / 1024; // convert to volts //Serial.println(volts); }