Advertisement
BurningWreck

FastLED MSGEQ7

Apr 1st, 2025
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.64 KB | Software | 0 0
  1.  
  2. #include <FastLED.h>
  3.  
  4. // --- Neopixel Matrix Settings ---
  5. #define LED_PIN        2       // Data pin for Neopixel matrix
  6. #define MATRIX_WIDTH   8
  7. #define MATRIX_HEIGHT  8
  8. #define NUM_LEDS       (MATRIX_WIDTH * MATRIX_HEIGHT) // 64 LEDs for 8x8
  9. #define LED_TYPE       WS2812B // Or WS2811, SK6812, etc.
  10. #define COLOR_ORDER    GRB     // Common for WS2812B
  11. #define BRIGHTNESS     64      // Set max brightness (0-255) - START LOW!
  12.  
  13. CRGB leds[NUM_LEDS];
  14.  
  15. // --- MSGEQ7 Settings ---
  16. #define MSGEQ7_ANALOG_PIN A1   // Analog input from MSGEQ7 'out'
  17. #define MSGEQ7_STROBE_PIN A3    // Digital output to MSGEQ7 'strobe'
  18. #define MSGEQ7_RESET_PIN  A2    // Digital output to MSGEQ7 'reset'
  19. #define NUM_BANDS         7    // MSGEQ7 has 7 frequency bands
  20.  
  21. int spectrumValue[NUM_BANDS]; // Raw values from MSGEQ7 (0-1023)
  22. int displayValue[MATRIX_WIDTH]; // Mapped values for display height (0-MATRIX_HEIGHT)
  23.  
  24. // --- Noise Filtering & Scaling ---
  25. // Adjust these values based on your audio input level and noise floor
  26. const int NOISE_THRESHOLD = 60;  // Ignore readings below this (adjust to filter noise) Orig 50
  27. const int MAX_EXPECTED_VALUE = 500; // Max expected reading (adjust for sensitivity) - rarely reaches 1023  Orig 800
  28.  
  29. // --- Setup Function ---
  30. void setup() {
  31.   Serial.begin(115200); // For debugging output
  32.   Serial.println("MSGEQ7 Neopixel Spectrum Analyzer");
  33.  
  34.   // Configure MSGEQ7 control pins
  35.   pinMode(MSGEQ7_ANALOG_PIN, INPUT);
  36.   pinMode(MSGEQ7_STROBE_PIN, OUTPUT);
  37.   pinMode(MSGEQ7_RESET_PIN, OUTPUT);
  38.  
  39.   // Initialize MSGEQ7 control pins
  40.   digitalWrite(MSGEQ7_RESET_PIN, LOW);
  41.   digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
  42.  
  43.   // Initialize FastLED
  44.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  45.   FastLED.setBrightness(BRIGHTNESS);
  46.   FastLED.clear(); // Start with LEDs off
  47.   FastLED.show();
  48.  
  49.   // Initial reset for MSGEQ7
  50.   digitalWrite(MSGEQ7_RESET_PIN, HIGH);
  51.   delayMicroseconds(1);
  52.   digitalWrite(MSGEQ7_RESET_PIN, LOW);
  53.   delayMicroseconds(75); // Wait for filter settling time
  54. }
  55.  
  56. // --- Main Loop ---
  57. void loop() {
  58.   readMSGEQ7();
  59.   mapMSGEQ7ToDisplay();
  60.   updateDisplay();
  61.   FastLED.show();
  62.   delay(10); // Small delay for stability / frame rate control
  63. }
  64.  
  65. // --- Read values from MSGEQ7 ---
  66. void readMSGEQ7() {
  67.   // Reset MSGEQ7 to start reading from the first band (63Hz)
  68.   digitalWrite(MSGEQ7_RESET_PIN, HIGH);
  69.   delayMicroseconds(1); // Reset pulse width
  70.   digitalWrite(MSGEQ7_RESET_PIN, LOW);
  71.   delayMicroseconds(75); // Wait for output settling time (tOS)
  72.  
  73.   // Read all 7 bands
  74.   for (int i = 0; i < NUM_BANDS; i++) {
  75.     digitalWrite(MSGEQ7_STROBE_PIN, LOW);
  76.     delayMicroseconds(30); // Strobe pulse width (tWH) + Output settling time (tOS)
  77.  
  78.     spectrumValue[i] = analogRead(MSGEQ7_ANALOG_PIN);
  79.  
  80.     // Filter out noise - optional clamping
  81.     if (spectrumValue[i] < NOISE_THRESHOLD) {
  82.          spectrumValue[i] = 0;
  83.     }
  84.     // Optional: You might want to clamp the max value if your scaling assumes it
  85.     // spectrumValue[i] = constrain(spectrumValue[i], 0, MAX_EXPECTED_VALUE);
  86.  
  87.     digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
  88.     delayMicroseconds(50); // Wait before next strobe (tSH)
  89.   }
  90.  
  91.   // Optional: Print raw values for debugging/tuning
  92.  
  93.   for (int i = 0; i < NUM_BANDS; i++) {
  94.     Serial.print(spectrumValue[i]);
  95.     Serial.print(" ");
  96.   }
  97.   Serial.println();
  98.  
  99. }
  100.  
  101. // --- Map MSGEQ7 values to LED column heights ---
  102. void mapMSGEQ7ToDisplay() {
  103.   for (int i = 0; i < NUM_BANDS; i++) {
  104.     // Map the filtered value (0 to MAX_EXPECTED_VALUE) to the matrix height (0 to MATRIX_HEIGHT)
  105.     // Using map() function: map(value, fromLow, fromHigh, toLow, toHigh)
  106.     int mappedValue = map(spectrumValue[i], NOISE_THRESHOLD, MAX_EXPECTED_VALUE, 0, MATRIX_HEIGHT);
  107.  
  108.     // Constrain ensures the value doesn't exceed matrix height, especially if spectrumValue > MAX_EXPECTED_VALUE
  109.     displayValue[i] = constrain(mappedValue, 0, MATRIX_HEIGHT);
  110.   }
  111.   // Handle the 8th column if needed (e.g., leave blank, mirror another column, average?)
  112.   // Here, we'll just leave it blank (its value remains 0 if not explicitly set)
  113.    if (MATRIX_WIDTH > NUM_BANDS) {
  114.        displayValue[NUM_BANDS] = 0; // Explicitly clear the 8th column (index 7)
  115.    }
  116.  
  117.   // Optional: Print mapped heights for debugging
  118.  
  119.   for(int i=0; i < MATRIX_WIDTH; i++) {
  120.       Serial.print(displayValue[i]); Serial.print(" ");
  121.   }
  122.   Serial.println();
  123.  
  124. }
  125.  
  126. // --- Update the Neopixel Matrix Display ---
  127. void updateDisplay() {
  128.   FastLED.clear(); // Clear the display buffer
  129.  
  130.   for (int x = 0; x < NUM_BANDS; x++) { // Iterate through the first 7 columns (bands)
  131.     for (int y = 0; y < displayValue[x]; y++) { // Light up LEDs from bottom (y=0) up to the calculated height
  132.       leds[XY(x, y)] = CRGB::Red; // Set the pixel to Red
  133.     }
  134.   }
  135.   // The 8th column (x=7) remains off because displayValue[7] was set to 0 (or wasn't touched)
  136. }
  137.  
  138. // --- XY Mapping Function (for Serpentine Layout) ---
  139. // Converts matrix coordinates (x, y) to a single LED index.
  140. // Assumes serpentine layout: starts top-left (0,0), goes right,
  141. // snakes back left on the next row, etc.
  142. // **ADJUST THIS FUNCTION IF YOUR MATRIX IS WIRED DIFFERENTLY**
  143. uint16_t XY(uint8_t x, uint8_t y) {
  144.   uint16_t i;
  145.  
  146.   if (y % 2 == 0) {
  147.     // Even rows (0, 2, 4, ...): left to right
  148.     i = (y * MATRIX_WIDTH) + x;
  149.   } else {
  150.     // Odd rows (1, 3, 5, ...): right to left
  151.     i = (y * MATRIX_WIDTH) + (MATRIX_WIDTH - 1 - x);
  152.   }
  153.   // Basic bounds check (optional but good practice)
  154.   if (x >= MATRIX_WIDTH || y >= MATRIX_HEIGHT) {
  155.      return NUM_LEDS; // Return an invalid index if out of bounds
  156.   }
  157.  
  158.   return i;
  159. }
Tags: FastLED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement