Bonjour,
Bonne nouvelle année à tous !
J'ai quasiment terminé mon projet de décibels mètre visuel et sonore pour la salle de classe de ma femme.
En revanche, j'ai encore quelques soucis avec le micro, du coup c'est un peu (beaucoup) embétant pour un sonomètre...
J'ai réalisé un support en bois, ressemblant à un feu tricolore, composé de 3 matrices de 15 leds RGB WS2812B, pilotées par un arduino nano, un micro avec gain, un écran LCD i2C, un potentiomètre pour régler la luminosité et deux buzzers.
Le principe est assez simple, le micro doit mesurer le volume sonore et l'écran affiche en temps réel le niveau sonore en décibels.
Le feu tricolore affiche les couleurs suivantes :
-Vert (matrice du bas), si dB < 60
-Orange (matrice du milieu), si dB > 60 et dB < 75
-Rouge (matrice du haut), si dB > 75 et dB < 90
-Rouge (sur les 3 matrices) + buzzer, si dB > 90
Mon souci est que le micro capte bien des variations quand je souffle dessus, mais pas quand je mets de la musique dans la pièce ; le nombre de décibels reste au minimum.
Jouer sur la vis de réglage du gain du micro ne change rien.
Pourriez-vous me dire où j'ai pu me planter ?
Comme je débute en arduino, je me suis inspiré d'un script trouvé sur le net pour la formule de calcul des dB, que j'ai incorporé à mon script ci-dessous.
Voici le code que j'ai compilé :
#include <FastLED.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define COLOR_ORDER GRB
#define LED_TYPE WS2812B
#define MAX_BRIGHTNESS 250
#define MIN_BRIGHTNESS 20
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD address to 0x27 for a 16 chars and 2 line display
char Buzzer = 2;
const int brightnessInPin = A7; // The Analog input pin that the brightness control potentiometer is attached to.
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
#define NUM_LEDS_PER_STRIP 15
CRGB redLeds[NUM_LEDS_PER_STRIP];
CRGB greenLeds[NUM_LEDS_PER_STRIP];
CRGB orangeLeds[NUM_LEDS_PER_STRIP];
void setup() {
FastLED.addLeds<NEOPIXEL, 8>(greenLeds, NUM_LEDS_PER_STRIP);
FastLED.addLeds<NEOPIXEL, 7>(orangeLeds, NUM_LEDS_PER_STRIP);
FastLED.addLeds<NEOPIXEL, 6>(redLeds, NUM_LEDS_PER_STRIP);
FastLED.setBrightness( MAX_BRIGHTNESS );
Serial.begin(9600);
lcd.init();
}
void loop() {
// read the analog brightness value:
//int brightValue = analogRead(brightnessInPin);
// map it to the range of the FastLED brightness:
int mappedValue = map(analogRead(brightnessInPin), 0, 1023, 0, 255);
//int outputValue = constrain(mappedValue, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
// now we set the brightness of the strip
FastLED.setBrightness(constrain(mappedValue, MIN_BRIGHTNESS, MAX_BRIGHTNESS));
unsigned long startMillis = millis(); // Start of sample window
float peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 10; //minimum value
unsigned int signalMin = 1024; //maximum value
while (millis() - startMillis < sampleWindow) // collect data for 50 mS
{
sample = analogRead(0); //get reading from microphone
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
float dB = map(peakToPeak, 100, 900, 40, 100); //calibrate for deciBels
Serial.println (dB);
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Niveau sonore :");
lcd.setCursor(2, 1);
lcd.print(dB);
lcd.setCursor(4, 1);
lcd.print(" dB");
if (dB < 60)
{
for (int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
redLeds[i] = CRGB::Black;
orangeLeds[i] = CRGB::Black;
greenLeds[i] = CRGB::Green;
FastLED.show();
delay (30);
}
}
else if (dB > 60 && dB < 75)
{
for (int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
redLeds[i] = CRGB::Black;
orangeLeds[i] = CRGB::Orange;
greenLeds[i] = CRGB::Black;
FastLED.show();
delay (30);
}
}
else if (dB > 75 && dB < 90)
{
for (int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
redLeds[i] = CRGB::Red;
orangeLeds[i] = CRGB::Black;
greenLeds[i] = CRGB::Black;
FastLED.show();
delay (30);
}
}
else if (dB > 90)
{
for (int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
redLeds[i] = CRGB::Red;
orangeLeds[i] = CRGB::Red;
greenLeds[i] = CRGB::Red;
FastLED.show();
delay (30);
}
tone(Buzzer, 2960, 1000);
delay (30);
}
}