/** Voice detecting respirator mask code by RenderMan see www.renderlab.net/projects/goggles-v2 for build details This code is rough, many nights at ENTS.ca, the Edmonton hackerspace drinking beer and hanging out I'm just learning, it's mangled and copy and pasted from a bunch of different sources, mostly the default ardiono examples It's ugly but it works. Feel free to use, fold spindle and mutilate. This code is creative commons if anyone cares **/ const int analogInPin = A0; // Analog input pin that the audio is on const int redPin = 11; //Digital pin for the Red leds const int greenPin = 10; //Digital pin for the Red leds int sound[4]; int soundav; int sensorValue = 0; int potValue = 0; // value read from the pot for ambient noise compensation int outputValue = 0; // value output to the PWM (analog out) int threshold = 0; void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); //Set pins pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); digitalWrite(greenPin, HIGH); digitalWrite(redPin, LOW); delay(100); pinMode(9,OUTPUT); pinMode(8,OUTPUT); //digitalWrite(9, HIGH); //digitalWrite(8, HIGH); delay(200); digitalWrite(9, LOW); digitalWrite(8, LOW); } void loop() { potValue = (analogRead(A1)/10); //read the pot value and divide by 10 to give me some granularity // threshold = map(potValue, 0, 1023, 0, 55); // threshold = (threshold / 10); sensorValue = analogRead(analogInPin); // read the analog in value from the mic: outputValue = map(sensorValue, 0, 1023, 0, 255); // map the 1024 possible mic values to the 255 range of our variable outputValue = ((outputValue - 128)); // after the mapping, the mics baseline is 128, so subtracting 128 will establish a new baseline of zero if (outputValue > (potValue)|| outputValue < (potValue -(2 * (potValue)))) { //if the output value (our variable for the mic output) is greater than the pot value (our adjustment for when to trigger the led to change) //The second part of the above line was conpensation for the arduino sampling on the low side of the wave form (a negative value), so boost the pot value to a positive and do an 'OR' operation which decides things digitalWrite(redPin, HIGH); digitalWrite(greenPin, LOW); } else{ // if we turn the green off and red on (voice trigger), or the reverse which is the 'default' of green on and red off (silence, or below our threshold) digitalWrite(redPin, LOW); digitalWrite(greenPin, HIGH); } // print the results to the serial monitor: Serial.print("soundav = " ); Serial.print(soundav); Serial.print("\t output = "); Serial.print(outputValue); Serial.print("\t potvalue = "); Serial.println(potValue); // wait 10 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(10); } //}