Tuesday, May 28, 2013

After discovering that we could buy the microphone with working amp all in one package (https://www.sparkfun.com/products/9964) we bought four and hooked them up. They worked immediately. It was joyous. When the


/* 
 Randy
 Starts and you must wait four seconds for it to read the ambient sound in the environment
 and adjust the threshold accordingly.
 */


// these constants won't change:
const int ledPin = 13;      // led connected to digital pin 13
const int electret1 = 0;  // the amplifier output is connected to analog pin 0
const int electret2 = 1;
const int electret3 = 2;
const int electret4 = 3;

// these variables will change:
int sensorReading1 = 0;      // variable to store the value read from the sensor pin
int sensorReading2 = 0;
int sensorReading3 = 0;
int sensorReading4 = 0;
int sensorMax1 = 0;
int sensorMax2 = 0;
int sensorMax3 = 0;
int sensorMax4 = 0;
int sensorMin = 1023;
int threshold1;
int threshold2;
int threshold3;
int threshold4;

void setup() {
  pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
  Serial.begin(57600);       // use the serial port
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  while (millis() < 3000) {
    delay(1000);
    threshold1 = analogRead(electret1);
    threshold2 = analogRead(electret2);
    threshold3 = analogRead(electret3);
    threshold4 = analogRead(electret4);

    // record the maximum sensor value
    if (threshold1 > sensorMax1) {
      sensorMax1 = threshold1;
    }
    
    if (threshold2 > sensorMax2) {
      sensorMax2 = threshold2;
    }
    
    if (threshold3 > sensorMax3) {
      sensorMax3 = threshold3;
    }
    
    if (threshold4 > sensorMax4) {
      sensorMax4 = threshold4;
    }
  }

  // signal the end of the calibration period
  digitalWrite(13, LOW);
  threshold1 = sensorMax1;
  threshold2 = sensorMax2;
  threshold3 = sensorMax3;
  threshold4 = sensorMax4;
  
  // led pins
  pinMode(3, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop() {
  digitalWrite(3, LOW);
  digitalWrite(6, LOW);
  
  // read the sensor and store it in the variable sensorReading:
  sensorReading1 = analogRead(electret1);   
  sensorReading2 = analogRead(electret2); 
  sensorReading3 = analogRead(electret3); 
  sensorReading4 = analogRead(electret4); 

  // if the sensor reading is greater than the threshold:
  if (sensorReading1 >= threshold1) {
    Serial.print("Mic1: ");
    Serial.print(sensorReading1-threshold1);
    Serial.print("    ");
  } else {
    Serial.print("Mic1: 0    ");
  }
  
  if (sensorReading2 >= threshold2) {
    Serial.print("Mic2: ");
    Serial.print(sensorReading2-threshold2);
    Serial.print("    ");
    if (sensorReading2-threshold2 > 10) {
        digitalWrite(3, HIGH);
    }
  } else {
    Serial.print("Mic2: 0    ");
  }
  
  if (sensorReading3 >= threshold3) {
    Serial.print("Mic3: ");
    Serial.print(sensorReading3-threshold3);
    Serial.print("    ");
    if (sensorReading3-threshold3 > 10) {
        digitalWrite(6, HIGH);
    }
  } else {
    Serial.print("Mic3: 0    ");
  }
  
  if (sensorReading4 >= threshold4) {
    Serial.print("Mic4: ");
    Serial.print(sensorReading4-threshold4);
    Serial.print("    ");
    Serial.print("\n");  
  } else {
    Serial.print("Mic4: 0    ");
    Serial.print("\n");  
  }



  delay(2);  // Better for Processing showing data
}

No comments:

Post a Comment