Guest User

LEDs Strip Project Code

a guest
Apr 26th, 2026
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | Source Code | 0 0
  1. #include <Arduino.h>
  2. #include <FastLED.h>
  3.  
  4.  
  5. #define NUM_LEDS 600 // the number of LEDs
  6. CRGB leds[NUM_LEDS];
  7.  
  8.  
  9. #define DATA_PIN 4 // where the D1 port of the LEDs plug in the Arduino
  10. #define delayLEDS 5
  11. #define sensorPin A0 // where the SIG port of the Sound Sensor plug in the Arduino
  12. #define LED_TYPE WS2811
  13. #define COLOR_ORDER CRGB
  14.  
  15.  
  16. void FilterSignal(float sensorSignal);
  17. void CompareSignalFiltered(float filteredSignal);
  18. void MainFunction();
  19.  
  20.  
  21. float sensorValue = 0, filteredSignal = 0,
  22. filteredSignalValues[] = {3.4, 3.1, 2.7, 2.4, 2.1, 1.7, 1.3, 0.9, 0.4};
  23.  
  24.  
  25. void setup () {
  26.  
  27.  
  28. delay(1);
  29. FastLED.addLeds<WS2811, DATA_PIN>(leds, NUM_LEDS);
  30. FastLED.setBrightness(250);
  31. Serial.begin (9600);
  32.  
  33.  
  34. }
  35.  
  36.  
  37.  
  38. void loop () {
  39.  
  40.  
  41. MainFunction();
  42. delay(delayLEDS);
  43.  
  44.  
  45. }
  46.  
  47.  
  48. void MainFunction() {
  49.  
  50.  
  51. sensorValue = (float) analogRead(sensorPin) * (5.0 / 1024.0);
  52.  
  53.  
  54. FilterSignal(sensorValue);
  55.  
  56.  
  57. Serial.print(sensorValue);
  58. Serial.print(" ");
  59. Serial.println(filteredSignal);
  60.  
  61.  
  62. CompareSignalFiltered(filteredSignal);
  63.  
  64.  
  65. }
  66.  
  67.  
  68. void FilterSignal(float sensorSignal) {
  69.  
  70.  
  71. filteredSignal = (0.945 * filteredSignal) + (0.0549 * sensorSignal);
  72.  
  73.  
  74. }
  75.  
  76.  
  77. void CompareSignalFiltered(float filteredSignal) {
  78.  
  79.  
  80. // l'ordre de sensibilité est décroissante, c'est une fourchette de 0 à 9
  81. if (filteredSignal > filteredSignalValues[0]) { // from too much noise to harmful to the ears
  82. fill_solid( leds, NUM_LEDS, CRGB::Red );
  83. FastLED.show();
  84. } else if (filteredSignal > filteredSignalValues[5]) { // from a bit of noise to a lot of noise
  85. fill_solid( leds, NUM_LEDS, CRGB::Orange );
  86. FastLED.show();
  87. } else if (filteredSignal > filteredSignalValues[8]) { // from almost silence to pretty calm
  88. fill_solid( leds, NUM_LEDS, CRGB::Green );
  89. FastLED.show();
  90. } else { // No sound detected
  91. fill_solid( leds, NUM_LEDS, CRGB::Green );
  92. FastLED.show();
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment