Problem controlling LEDstrip and sound at the same time with teensy 3.2 + propshield

Euwe

New member
H

I'm having a problem for a while but I am not able to find a solution. The problem is that when I try to animate the led strip (WS2812B) while playing a WAV file at the same time not all LEDs seem to respond to the program I wrote. The first 80 LEDs seem to do just fine while animating the strip, but the last 20 or so seem glitch and all turn on at the same time instead of animating up one by one. The same problem is when animating down, but then the first 20 LEDs seem to turn off at the same time and then the rest of the strip seems to do just what I programmed. I am using a teensy 3.2, teensy's micro SD card adaptor and a propshield where I have connected the LEDs to the LED driver and a 2W 8Ω speaker to the audio amplifier. I have run both codes individually and they seem to work perfectly, but when combined the led strip glitches.

If anyone could help me out that'd be great!

This is the code I used:

Code:
#include <FastLED.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
//AudioPlaySdWav           playSdWavHum;     //xy=270,91.00000381469727// I will add this later
AudioPlaySdWav           playSdWavOnOff;     //xy=272,40.00000190734863
AudioMixer4              mixer1;         //xy=621.0000076293945,98.00000190734863
AudioOutputAnalog        dac1;           //xy=791,95

//AudioConnection          patchCord1(playSdWavHum, 0, mixer1, 1);// I will add this later
AudioConnection          patchCord2(playSdWavOnOff, 0, mixer1, 0);
AudioConnection          patchCord5(mixer1, dac1);
// GUItool: end automatically generated code

const int buttonPin = 17;
const int ledPin = 13; 
const int pressLengthMin = 50;
const int pressLength1 = 500;
const int pressLength2 = 2000; 
unsigned long previousMillis = 0; 

int buttonState = 0;
int millisTimer1;
int millisTimer2;
bool buttonClicked = false;
bool saberOn = false;

#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  11
#define SDCARD_SCK_PIN   13

#define DATA_PIN    20  
#define NUM_LEDS    99 

CRGB leds[NUM_LEDS];

void setup() {
  Serial.begin(9600);
  Serial.println("HELLO THERE!");
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
  FastLED.setBrightness( 50 );
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }
  
  AudioMemory(20);
  dac1.analogReference(INTERNAL); 
  delay(50);             // time for DAC voltage stable
  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH); // turn on the amplifier
  delay(10);             // allow time to wake up

  mixer1.gain(0, 1.0f); //on off sounds
  //mixer1.gain(1, 0.5f); // I will add this later

  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);
}

void loop() {
  buttonTimer();
}

void buttonTimer() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW && buttonClicked == false) {
    millisTimer1 = millis();
    buttonClicked = true;
  } else if (buttonState == HIGH && buttonClicked == true) {
    millisTimer2 = millis();
    buttonClicked = false;
  }
  buttonCall();
}

void buttonCall() {
  if (millisTimer2 - millisTimer1 > pressLengthMin && millisTimer2 - millisTimer1 <= pressLength1 && saberOn == false){
    saberOnCall();
    millisTimer1 = 0;
    millisTimer2 = 0;
  }else if (millisTimer2 - millisTimer1 > pressLengthMin && millisTimer2 - millisTimer1 <= pressLength1 && saberOn == true){
    saberOffCall();
    millisTimer1 = 0;
    millisTimer2 = 0;
  }
}

void saberOnCall() {
  playSdWavOnOff.play("in01.wav");
  for(int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB(255,0,0);
    FastLED.show();
    delay(3);
  }
  saberOn = true;
}

void saberOffCall() {
  saberOn = false;
  playSdWavOnOff.play("in02.wav");
  for (int i=NUM_LEDS; i>=0; i--){
    leds[i]=CRGB(0, 0, 0);
    FastLED.show();
    delay(3);
  }
}
 
Code:
  FastLED.addLeds<[B]WS2812B[/B], DATA_PIN, GRB>(leds, NUM_LEDS);

Use WS2812Serial. FastLED's normal WS2812B driver doesn't work well audio processing.

In Arduino, click File > Examples > WS2812Serial > FastLED_Cylon to get an example of the special syntax required. The order of define and include lines is important. The other key detail, which you'll see in the example, is only certain pins can be used with WS2812Serial. You can't just pick any pin as with the normal WS2812B driver. It's a worthwhile trade-off, because WS2812Serial will give you solid reliable LED update even if the audio processing needs a lot of CPU time.

If you only need simple direct control of the LEDs, just using the WS2812Serial library without FastLED is also an option.
 
Use WS2812Serial. FastLED's normal WS2812B driver doesn't work well audio processing.

Thank you so much! It now runs perfectly!

This is my working code:
Code:
#include <WS2812Serial.h>
#define USE_WS2812SERIAL
#include <FastLED.h>

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

// GUItool: begin automatically generated code
//AudioPlaySdWav           playSdWavHum;     //xy=270,91.00000381469727// I will add this later
AudioPlaySdWav           playSdWavOnOff;     //xy=272,40.00000190734863
AudioMixer4              mixer1;         //xy=621.0000076293945,98.00000190734863
AudioOutputAnalog        dac1;           //xy=791,95

//AudioConnection          patchCord1(playSdWavHum, 0, mixer1, 1);// I will add this later
AudioConnection          patchCord2(playSdWavOnOff, 0, mixer1, 0);
AudioConnection          patchCord5(mixer1, dac1);
// GUItool: end automatically generated code

const int buttonPin = 17;
const int ledPin = 13; 
const int pressLengthMin = 50;
const int pressLength1 = 500;
const int pressLength2 = 2000; 
unsigned long previousMillis = 0; 

int buttonState = 0;
int millisTimer1;
int millisTimer2;
bool buttonClicked = false;
bool saberOn = false;

#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  11
#define SDCARD_SCK_PIN   13

#define DATA_PIN    1  
#define NUM_LEDS    99 


CRGB leds[NUM_LEDS];

void setup() {
  Serial.begin(57600);
  Serial.println("HELLO THERE!");
  LEDS.addLeds<WS2812SERIAL,DATA_PIN,RGB>(leds,NUM_LEDS);
  LEDS.setBrightness(50);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }
  
  AudioMemory(20);
  dac1.analogReference(INTERNAL); 
  delay(50);             // time for DAC voltage stable
  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH); // turn on the amplifier
  delay(10);             // allow time to wake up

  mixer1.gain(0, 0.2f); //on off sounds
  //mixer1.gain(1, 0.3f); // I will add this later

  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);
}

void loop() {
  buttonTimer();
}

void buttonTimer() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW && buttonClicked == false) {
    millisTimer1 = millis();
    buttonClicked = true;
  } else if (buttonState == HIGH && buttonClicked == true) {
    millisTimer2 = millis();
    buttonClicked = false;
  }
  buttonCall();
}

void buttonCall() {
  if (millisTimer2 - millisTimer1 > pressLengthMin && millisTimer2 - millisTimer1 <= pressLength1 && saberOn == false){
    saberOnCall();
    millisTimer1 = 0;
    millisTimer2 = 0;
  }else if (millisTimer2 - millisTimer1 > pressLengthMin && millisTimer2 - millisTimer1 <= pressLength1 && saberOn == true){
    saberOffCall();
    millisTimer1 = 0;
    millisTimer2 = 0;
  }
}

void saberOnCall() {
  playSdWavOnOff.play("in01.wav");
  for(int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB(255,0,0);
    FastLED.show();
    delay(4);
  }
  saberOn = true;
}

void saberOffCall() {
  saberOn = false;
  playSdWavOnOff.play("in02.wav");
  for (int i=NUM_LEDS; i>=0; i--){
    leds[i]=CRGB(0, 0, 0);
    FastLED.show();
    delay(4);
  }
}
 
Hi,

I am still running into problems once I increase the number of LEDs to 130. At 96 MHz the LEDs go dim short after turning them on and at 120 MHz everything seems to work just fine at the start but, the audio only works once. When I press the button again the LEDs work but no audio comes out.

How can I fix this?
 
Back
Top