Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 14 of 14

Thread: Audio Memory/Output Causing glitches

  1. #1

    Audio Memory/Output Causing glitches

    I have noticed a glitch in my code. It cycles through different LED settings some using FFT and some not. The settings that don't use FFT glitch out. I have found that when removing both audio memory in setup and AudioOutputI2S the glitch goes away. When adding either one of those pieces the glitch is back.

    I have simplified the problem in this code below. Its just a simple fade function but when running it will glitch out. It also does not need to be receiving any audio signal to perform this visual glitch. If I remove the two Audio pieces it runs just fine.

    Any Ideas on how to fix this?

    Code:
    #include <FastLED.h>
    #define NUM_LEDS 6
    #define DATA_PIN 2
    #define BUTTON_PIN 8
    #include <avr/power.h>
    
    
    byte buttonChangeClick = 1;
    byte buttonChangeHold = 1;
    byte buttonChange3 = 1;
    byte prevButtonChangeClick = 1;
    byte prevButtonChangeHold = 1;
    byte prevButtonChange3 = 1;
    
    unsigned long millisTime;
    static unsigned last_time = 0;
    CRGB leds[NUM_LEDS];
    
    int ledLevelPin = 0;
    int ledLevelVal = 0;
    int ledLevel;
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    AudioControlSGTL5000 audioShield;
      
    byte fadeColor = 50;
    byte fadeCount = 1;
    
    
    AudioInputI2S          audioInput;         // audio shield: mic or line-in
    AudioSynthWaveformSine sinewave;
    AudioAnalyzeFFT1024    myFFT;
    AudioOutputI2S         audioOutput;
    
    AudioConnection patchCord1(audioInput, 0, myFFT, 0);
    
    const int myInput = AUDIO_INPUT_LINEIN;
    
    void setup() { 
           FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
           
            AudioMemory(12);
    
    
      audioShield.enable();
      audioShield.inputSelect(myInput);
      audioShield.volume(0.5);
    
      
      myFFT.windowFunction(AudioWindowHanning1024);
    
      sinewave.amplitude(0.8);
      sinewave.frequency(1034.007);
    }
    
    void loop()
    {
    fadeLoop();
    }
    
    
    
    void fadeLoop()
    {
      millisTime = millis();
       if (millisTime - last_time > 40 && fadeCount == 1)
       {
    
         fadeColor ++;
         leds[0] = CRGB (fadeColor,0,255);
         leds[2] = CRGB (fadeColor,0,255);
         leds[4] = CRGB (fadeColor,0,255);
         
         leds[1] = CRGB (fadeColor,0,0);
         leds[3] = CRGB (fadeColor,0,0);
         leds[5] = CRGB (fadeColor,0,0);
    
      FastLED.show();
      last_time = millisTime;
      if (fadeColor >= 254) fadeCount =2;
       }
    
       if (millisTime - last_time > 40 && fadeCount == 2)
       {
    
         fadeColor --;
         leds[0] = CRGB (fadeColor,0,255);
         leds[2] = CRGB (fadeColor,0,255);
         leds[4] = CRGB (fadeColor,0,255);
         
         leds[1] = CRGB (fadeColor,0,0);
         leds[3] = CRGB (fadeColor,0,0);
         leds[5] = CRGB (fadeColor,0,0);
      FastLED.show();
      last_time = millisTime;
        if (fadeColor <= 50) fadeCount =1;
       }
     }

  2. #2
    Try Increasing the value of AudioMemory. Let me know if it works.

  3. #3
    Alfa66, Thank you for responding to my post. I incremented the memory by 12 until reaching 72. Each increment appeared to have the same exact glitch.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,920
    What does "glitch out" mean?

    I get that the LEDs are supposed to slowly increase and then decrease in brightness. But what are you actually seeing? Obviously something different, but I'm not getting much idea of what it actually is.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,920
    As a quick test, can you try surrounding those two FastLED.show() with the audio interrupt disable code. Like this:

    Code:
      AudioNoInterrupts();
      FastLED.show();
      AudioInterrupts();
    Make sure you get both of them.

    Does this help, or make things even worse?

  6. #6
    Glitching out is the best way I can think to describe it. What happens is random pixels will flash randomly in a glitchy fashion. So as its fading smoothly, a few pixels flash around. Then continues fading and FLASH while fading FLASH. Maybe two maybe three at once. Sometimes its just one pixel.

    I imagine if it had a sound of its own it would be similar to this. https://www.youtube.com/watch?v=SuLCQanvrC8

  7. #7
    That does not seem to change the effect I am seeing.

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,920
    Maybe try this?

    Code:
      noInterrupts();
      FastLED.show();
      interrupts();

  9. #9
    That also did not fix it.

  10. #10
    The only thing that does seem to fix it is commenting out the audio memory in the setup and the AudioOutputI2s. Adding either one of these elements brings back the "glitch".

  11. #11
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,920
    Quote Originally Posted by rexhex View Post
    That also did not fix it.
    That's very surprising to me. Usually when we see Neopixel and other libs conflict, it's due to interrupt usage conflicts.

    Quote Originally Posted by rexhex View Post
    The only thing that does seem to fix it is commenting out the audio memory in the setup and the AudioOutputI2s. Adding either one of these elements brings back the "glitch".
    Well, why don't you just leave off AudioOutputI2S? You haven't connected any audio stream to it.

    Obviously nothing in the audio library runs if you don't allocate any memory for it to use.

  12. #12
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,920
    Ok, I ran it here, and I managed to reproduce the problem.

    Click image for larger version. 

Name:	fastled.jpg 
Views:	99 
Size:	76.9 KB 
ID:	6504

    It is indeed an interrupt conflict. But FastLED ignores those ways I suggested. The solution is defining FASTLED_ALLOW_INTERRUPTS to 0.

    This works:

    Code:
    #define FASTLED_ALLOW_INTERRUPTS 0
    #include <FastLED.h>
    #define NUM_LEDS 6
    #define DATA_PIN 2
    #define BUTTON_PIN 8
    #include <avr/power.h>
    
    byte buttonChangeClick = 1;
    byte buttonChangeHold = 1;
    byte buttonChange3 = 1;
    byte prevButtonChangeClick = 1;
    byte prevButtonChangeHold = 1;
    byte prevButtonChange3 = 1;
    
    unsigned long millisTime;
    static unsigned last_time = 0;
    CRGB leds[NUM_LEDS];
    
    int ledLevelPin = 0;
    int ledLevelVal = 0;
    int ledLevel;
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    AudioControlSGTL5000 audioShield;
    
    byte fadeColor = 50;
    byte fadeCount = 1;
    
    AudioInputI2S          audioInput;         // audio shield: mic or line-in
    AudioSynthWaveformSine sinewave;
    AudioAnalyzeFFT1024    myFFT;
    AudioOutputI2S         audioOutput;
    
    AudioConnection patchCord1(audioInput, 0, myFFT, 0);
    
    const int myInput = AUDIO_INPUT_LINEIN;
    
    void setup() {
      FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
      AudioMemory(12);
    
      audioShield.enable();
      audioShield.inputSelect(myInput);
      audioShield.volume(0.5);
    
      myFFT.windowFunction(AudioWindowHanning1024);
    
      sinewave.amplitude(0.8);
      sinewave.frequency(1034.007);
    }
    
    void fadeLoop();
    
    void loop()
    {
      fadeLoop();
    }
    
    
    
    void fadeLoop()
    {
      millisTime = millis();
      if (millisTime - last_time > 40 && fadeCount == 1)
      {
    
        fadeColor ++;
        leds[0] = CRGB (fadeColor, 0, 255);
        leds[2] = CRGB (fadeColor, 0, 255);
        leds[4] = CRGB (fadeColor, 0, 255);
        leds[1] = CRGB (fadeColor, 0, 0);
        leds[3] = CRGB (fadeColor, 0, 0);
        leds[5] = CRGB (fadeColor, 0, 0);
        FastLED.show();
        last_time = millisTime;
        if (fadeColor >= 254) fadeCount = 2;
      }
    
      if (millisTime - last_time > 40 && fadeCount == 2)
      {
        fadeColor --;
        leds[0] = CRGB (fadeColor, 0, 255);
        leds[2] = CRGB (fadeColor, 0, 255);
        leds[4] = CRGB (fadeColor, 0, 255);
        leds[1] = CRGB (fadeColor, 0, 0);
        leds[3] = CRGB (fadeColor, 0, 0);
        leds[5] = CRGB (fadeColor, 0, 0);
        FastLED.show();
        last_time = millisTime;
        if (fadeColor <= 50) fadeCount = 1;
      }
    }

  13. #13
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,920
    However, if you use a large number of Neopixel LEDs, FastLED will disable interrupts for too long, causing the audio to stutter. For a long LED strip, you'll need either the OctoWS2811 driver, or a 4-wire LED type like APA102.

  14. #14
    I am planning on using the AudioOutput later on so I wanted to keep it in the sketch. Plus the issue still occurred with just the audio memory in the setup.

    So it was an interrupt issue after all... What is it that made you think it was an interrupt issue right away? Im asking because I would like to be able to identify interrupt issues as well.

    Thank you for the support Mr. Stoffregen! I have it working over here on my end as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •