Détection de volume sonore

 

recherches :

http://www.jaycollett.com/2009/08/sound-detection-with-the-arduino/

http://arduino.cc/forum/index.php/topic,2534.0.html

http://www.youtube.com/watch?v=fOlStxYiVqs

http://www.youtube.com/watch?v=h8Bu7fh-B6s&feature=endscreen&NR=1

http://blog.makezine.com/2008/03/24/explody-easter-peeps-high/

http://circuitdiagram.net/simple-audio-pre-amplifier.html

http://www.youtube.com/watch?v=ic_yEUV7Y3c

http://electronics.stackexchange.com/questions/36795/using-a-microphone-with-an-arduino

 

Besoin:

Créer une veilleuse de nuit asservie au volume sonore de la pieces.

 

Principe :

utiliser un Arduino pour détecter le niveau sonore d'une piece

 

Composants nécessaires:

1 arduino

1 micro electret

 

Il est nécessaire d'utiliser un circuit d'amplification du genre :

 

 

 

Code source

  1.  /* Détection de volume Audio
  2.   Le circuit :
  3.   http://design.ensa-nancy.net/detection-de-son
  4.   http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
  5.   */
  6.  
  7.  const int ledPin = 9; // no du connecteur de led
  8.  const int ledPin_1 = 2; // no du connecteur de led
  9.  const int ledPin_2 = 3; // no du connecteur de led
  10.  const int ledPin_3 = 4; // no du connecteur de led
  11.  const int ledPin_4 = 5; // no du connecteur de led
  12.  const int sensorPin = A0;
  13.  
  14.  long previousMillis = 0;
  15.  int volume = 0;
  16.  int volume_min = 1023;
  17.  int volume_max = 0;
  18.  int volume_interval = 2;
  19.  boolean led_etat = false;
  20.  int led_value = 0;
  21.  long led_timer = 10000;
  22.  long led_delay = 10;
  23.  long prev_delay = 0;
  24.  
  25.  void setup() {
  26.   Serial.begin(9600);
  27.   pinMode(ledPin_1, OUTPUT);
  28.   pinMode(ledPin_2, OUTPUT);
  29.   pinMode(ledPin_3, OUTPUT);
  30.   pinMode(ledPin_4, OUTPUT);
  31.   digitalWrite(13, HIGH);
  32.  
  33.   // calibrate during the first five seconds
  34.   while (millis() < 5000) {
  35.   volume = analogRead(sensorPin);
  36.  
  37.   // record the maximum sensor value
  38.   if (volume > volume_max) {
  39.   volume_max = volume;
  40.   }
  41.  
  42.   // record the minimum sensor value
  43.   if (volume < volume_min) {
  44.   volume_min = volume;
  45.   }
  46.   }
  47.   // signal the end of the calibration period
  48.   digitalWrite(13, LOW);
  49.   volume_interval = (volume_max - volume_min)/4;
  50.   prev_delay = millis();
  51.   Serial.println(volume_min);
  52.   Serial.println(volume_max);
  53.   Serial.println(volume_interval);
  54.  }
  55.  
  56.  void loop() {
  57.   volume = analogRead(sensorPin);
  58.  
  59.   if (volume > volume_min + volume_interval ) digitalWrite(ledPin_1, HIGH); else digitalWrite(ledPin_1, LOW);
  60.   if (volume > volume_min + volume_interval * 2 ) digitalWrite(ledPin_2, HIGH); else digitalWrite(ledPin_2, LOW);
  61.   if (volume > volume_min + volume_interval * 3) digitalWrite(ledPin_3, HIGH); else digitalWrite(ledPin_3, LOW);
  62.   if (volume > volume_min + volume_interval * 4) digitalWrite(ledPin_4, HIGH); else digitalWrite(ledPin_4, LOW);
  63.  
  64.   //int sensorValue = map(volume, volume_min, volume_max, 0, 255);
  65.   //sensorValue = constrain(sensorValue, 0, 255);
  66.   // fade the LED using the calibrated value:
  67.   //analogWrite(ledPin, sensorValue);
  68.  
  69.   if ( volume >= volume_max){
  70.   if (led_etat == false){
  71.   led_etat = true;
  72.   previousMillis = millis();
  73.   prev_delay = millis();
  74.   Serial.println(led_etat);
  75.   }
  76.   } else if (millis() - previousMillis > led_timer){
  77.   if ( led_etat == true){
  78.   led_etat = false;
  79.   previousMillis = millis();
  80.   prev_delay = millis();
  81.   Serial.println(led_etat);
  82.   }
  83.   }
  84.   if (millis() - prev_delay >= led_delay){
  85.   Serial.println(led_value);
  86.   if (led_etat == true) {
  87.   if (led_value < 255) led_value++;
  88.   analogWrite(ledPin, led_value);
  89.   } else {
  90.   if (led_value > 0) led_value--;
  91.   analogWrite(ledPin, led_value);
  92.   }
  93.   prev_delay = millis();
  94.   }
  95.  }