Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <FastLED.h>
- #define NUM_LEDS 600 // the number of LEDs
- CRGB leds[NUM_LEDS];
- #define DATA_PIN 4 // where the D1 port of the LEDs plug in the Arduino
- #define delayLEDS 5
- #define sensorPin A0 // where the SIG port of the Sound Sensor plug in the Arduino
- #define LED_TYPE WS2811
- #define COLOR_ORDER CRGB
- void FilterSignal(float sensorSignal);
- void CompareSignalFiltered(float filteredSignal);
- void MainFunction();
- float sensorValue = 0, filteredSignal = 0,
- filteredSignalValues[] = {3.4, 3.1, 2.7, 2.4, 2.1, 1.7, 1.3, 0.9, 0.4};
- void setup () {
- delay(1);
- FastLED.addLeds<WS2811, DATA_PIN>(leds, NUM_LEDS);
- FastLED.setBrightness(250);
- Serial.begin (9600);
- }
- void loop () {
- MainFunction();
- delay(delayLEDS);
- }
- void MainFunction() {
- sensorValue = (float) analogRead(sensorPin) * (5.0 / 1024.0);
- FilterSignal(sensorValue);
- Serial.print(sensorValue);
- Serial.print(" ");
- Serial.println(filteredSignal);
- CompareSignalFiltered(filteredSignal);
- }
- void FilterSignal(float sensorSignal) {
- filteredSignal = (0.945 * filteredSignal) + (0.0549 * sensorSignal);
- }
- void CompareSignalFiltered(float filteredSignal) {
- // l'ordre de sensibilité est décroissante, c'est une fourchette de 0 à 9
- if (filteredSignal > filteredSignalValues[0]) { // from too much noise to harmful to the ears
- fill_solid( leds, NUM_LEDS, CRGB::Red );
- FastLED.show();
- } else if (filteredSignal > filteredSignalValues[5]) { // from a bit of noise to a lot of noise
- fill_solid( leds, NUM_LEDS, CRGB::Orange );
- FastLED.show();
- } else if (filteredSignal > filteredSignalValues[8]) { // from almost silence to pretty calm
- fill_solid( leds, NUM_LEDS, CRGB::Green );
- FastLED.show();
- } else { // No sound detected
- fill_solid( leds, NUM_LEDS, CRGB::Green );
- FastLED.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment