Audio Shield Wave Player with Teensy 3.1 and Fastled --> flickering

walle

Member
Hello, I am building a project with teensy and I want to play a wav file from sd card and control some leds via Fastled. I have downloaded yesterday from all software the newest version(Arduino, teensyduino, audio library, fastled) When I use in the example code from wave player for num_leds 3 everything works fine, but when i use 40 up to 200, the leds have flickering. I use Teensy 3.1 96 Mhz with fastled 3.1 Audio Shield and the Audio Wave player code and I have no idea why it makes these problems! Interesting is, that when the music doesnt play at the beginning the effect is slower than when the music plays...


Code:
#include <FastLED.h>

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#define NUM_LEDS 200
CRGB leds[NUM_LEDS];

// GUItool: begin automatically generated code
AudioPlaySdWav           playWav1;       //xy=154,78
AudioOutputI2S           i2s1;           //xy=334,89
AudioConnection          patchCord1(playWav1, 0, i2s1, 0);
AudioConnection          patchCord2(playWav1, 1, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=240,153
// GUItool: end automatically generated code

void setup() {
    Serial.begin(9600);
     FastLED.addLeds<TM1809, 5, RGB>(leds, NUM_LEDS);
     FastLED.setBrightness(20);
    
    AudioMemory(5);
    sgtl5000_1.enable();
    sgtl5000_1.volume(0.5);
    SPI.setMOSI(7);
    SPI.setSCK(14);
    if (!(SD.begin(10))) {
        // stop here, but print a message repetitively
        while (1) {
            Serial.println("Unable to access the SD card");
            delay(500);
        }
    }
    
    
}

void playFile(const char *filename)
{
    playWav1.play(filename);
    delay(5);
}

uint8_t a = 0;
void loop() {
    leds[0] = CHSV(a, 255, 255);
    a++;
    leds[1] = CHSV(0, 255, a);
    FastLED.show();
    FastLED.delay(1);
    
    EVERY_N_SECONDS(20)
    {
        playFile("SDTEST2.WAV");
    }

}
 
These types of LEDs required FastLED to create the waveform with tight timing in code. When the audio library needs CPU time, it will disrupt FastLED. You can use AudioNoInterrupts() before FastLED.show() to prevent the audio library from interfering, and AudioInterrupts() after. But if FastLED takes too long, the sound might glitch. Ultimately, you might need to break the LEDs into multiple shorter strips, or switch to clock + data types that don't require tight timing.
 
Back
Top