Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * The Circuit:
- * Connect AUD to analog A4 (as input)
- * Connect LED to analog A5 (as output)
- * Connect GND to GND
- * Connect VCC to 3.3V or 5.0V (3.3V yields the best results)
- * 3.3V -- quiet sensor signal approx 310
- * 5.0V -- quiet sensor signal approx 520
- *
- * To adjust when the LED turns on based on audio input:
- * Open up the serial com port (Top right hand corner of the Arduino IDE)
- * It looks like a magnifying glass. Perform several experiments
- * clapping, snapping, blowing, door slamming, knocking etc and see where the
- * resting noise level is and where the loud noises are. Adjust the if statement
- * according to your findings.
- *
- * You can also adjust how long you take samples for by updating the "SampleWindow"
- *
- * This code has been adapted from the
- * Example Sound Level Sketch for the
- * Adafruit Microphone Amplifier
- */
- const int sampleWindow = 250; //Sample window width in mS (250 mS = 4Hz)
- const float referenceVoltage = 3.28; //Set board operating voltage
- const int threshold = 1.00; //Sound level to trigger LED (in volts)
- int knock; //Variable to store sensor value
- int ledPin = A5; //Define LED pin
- int micPin = A4; //Define Microphone pin
- void setup()
- {
- Serial.begin(9600);
- pinMode(ledPin, OUTPUT);
- Serial.println(F("\nGetting Ready..."));
- delay(3000);
- }
- void loop()
- {
- unsigned long start= millis(); // Start of sample window
- unsigned int peakToPeak = 0; // peak-to-peak level
- unsigned int signalMax = 0; // Define maximum signal as 0 to start
- unsigned int signalMin = 1024; // Define minimum signal as 1024 to start
- //collect audio signal data for 250 miliseconds
- while (millis() - start < sampleWindow)
- {
- knock = analogRead(micPin);
- if (knock > signalMax)
- {
- signalMax = knock; // save just the max levels
- }
- else if (knock < signalMin)
- {
- signalMin = knock; // save just the min levels
- }
- } //end while
- peakToPeak = signalMax - signalMin; // calculate max - min = peak-to-peak amplitude
- float volts = (peakToPeak * referenceVoltage) / 1024; // convert to volts
- Serial.println("Signal: "+String(knock)+" Peak Voltage: "+String(volts));
- if (volts >= threshold)
- {
- //turn on LED for 500ms
- digitalWrite(ledPin, HIGH);
- delay(500);
- Serial.println(F("\n\nKnock Knock"));
- }
- else
- {
- //turn LED off
- digitalWrite(ledPin, LOW);
- }
- }// end loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement