Audio or LEDs, but not both. Please help

Status
Not open for further replies.
So, here's a stripped down version of the essentials of this code. Basically, I can get audio to play or LEDs to work, but if both are active in the code, only the first one called works.
I tried doing an SPI transaction around the LED lines, but that doesn't seem to affect it. Any ideas?

Code:
#include <FastLED.h>

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

AudioPlaySerialflashRaw  playRawHUM;  //xy=149,388
AudioPlaySerialflashRaw  playRawBlade;  //xy=149,388
AudioMixer4              mixer1;         //xy=445,386
AudioOutputAnalog        dac1;           //xy=591,379
AudioConnection          patchCord2(playRawBlade, 0, mixer1, 0);
//AudioConnection          patchCord1(playRawHUM, 1, mixer1, 1);
AudioConnection          patchCord3(mixer1, dac1);

#define LED_PIN     7

#define COLOR_ORDER BGR
#define CHIPSET     APA102
#define NUM_LEDS    300

#define BRIGHTNESS  100
#define FRAMES_PER_SECOND 70

#define PROP_AMP_ENABLE    5
#define FLASH_CHIP_SELECT 6

CRGB leds[NUM_LEDS];
int potVal = 0;
int outputValue = 0;
int presetColors[] = {CRGB::Red,CRGB::Blue,CRGB::ForestGreen,CRGB::White, CRGB::Orange};
  
  
void setup() {
  delay(1000); // sanity delay
  //FastLED.addLeds<APA102, BGR>(leds, NUM_LEDS);
  FastLED.addLeds<CHIPSET, COLOR_ORDER>(leds, NUM_LEDS);//.setCorrection( TypicalLEDStrip );
  FastLED.setBrightness( BRIGHTNESS );
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, HIGH);

  Serial.begin(9600);

  // wait up to 3 seconds for the Serial device to become available
  long unsigned debug_start = millis ();
  while (!Serial && ((millis () - debug_start) <= 3000))
    ;

  Serial.println ("Start prop shield RAW player");
  pinMode(PROP_AMP_ENABLE, OUTPUT);
  digitalWrite(PROP_AMP_ENABLE, HIGH);
  
  AudioMemory(20);
  dac1.analogReference(EXTERNAL); // much louder!
  delay(50);             // time for DAC voltage stable  

  mixer1.gain(0, 1.5f);
  // Start SerialFlash
  if (!SerialFlash.begin(FLASH_CHIP_SELECT)) {
    while (1)
      {
  Serial.println ("Cannot access SPI Flash chip");
  delay (3000);
      }
    }
    
}



void loop()
{
  //This audio or the LEDs work, but only whichever is first in the code
for( int j = 0; j < 50; j++) {
      leds[j] = CRGB::Red;
    }
SPI.beginTransaction(SPISettings(24000000, MSBFIRST, SPI_MODE0));
    digitalWrite(7, HIGH);  // enable access to LEDs
    FastLED.show();
    digitalWrite(7, LOW);
SPI.endTransaction();   // allow other libs to use SPI again  

//  playRawBlade.play("BLADEUP.RAW");
//  delay(2000); 
  
FastLED.delay(1000 / FRAMES_PER_SECOND);
  
     
}
 
The SPI speed might be too fast for the APA102s. If I recall correctly, Paul suggested 10MHz or 12 MHz would work better. This solved a flicker problem for me. I was driving 96 APA102 pixels with FastLED at 4.5v.
 
I seem to recall having issues with FastLED (driving WS2812B's) in conjunction with the audio library due to interrupts whilst servicing audio. Solution was to uncomment a #define, but I don't remember if that was in FastLED or the Audio library.
 
The store page says this:
When other SPI communication is used, pin 7 must be HIGH to allow LED access and LOW to prevent other SPI chips from interfering with the LEDs. The SPI transactions functions should also be used. You will need to include SPI.h to have access to these SPI functions.

With the Audio library it using the SPI for data while you are using the LED's - perhaps that is what the SPI.begin() was for?

What happens if you do:

Code:
    playRawBlade.stop();
    digitalWrite(7, HIGH);  // enable access to LEDs
    FastLED.show();
    digitalWrite(7, LOW);
    playRawBlade.play("BLADEUP.RAW");
    delay(100);
 
Yes, the idea was that the SPI transaction would compartmentalize the two actions (I thought, anyway).
I tested the above and it didn't seem to have any affect.

Also, I changed the SPI speed that Pictographer mentioned, and it did help with flicker a little but didn't change the actual issue at all.
 
I tried that, and thought it worked, but apparently not. I had multiple files open and was testing against the wrong one.

Still no go.
 
Last edited:
Still trying the code in msg #1, or some newer version? Any chance you could post BLADEUP.RAW, so I can give it a try here?
 
I had some trouble getting the audio library and fastLED 3.1 to work. I'm pretty sure that it had something to do with DMA buffers, but, I reverted to fastLED 3.0.3 and my problems went away, so I didn't investigate fully.

Edit: I was using WS2812's though. Your mileage may vary.
 
Last edited:
Status
Not open for further replies.
Back
Top