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

Thread: Controlling SK6812 LEDs while using Audio Shield

  1. #1
    Junior Member
    Join Date
    Feb 2020
    Posts
    2

    Controlling SK6812 LEDs while using Audio Shield

    Hi, absolute beginner teensy user here.
    Is it possible to control SK6812 LEDs with a teensy 3.5 with audio shield? I am trying to control three strings, one of 9 leds and one of 5 leds and one with 1 LED. The first LED strip is supposed to flash in a random pattern, while the other lights up with button pushes. I know there are some conflicts due to the audio shield with timing. Is there a way around this? With the code I am using, it doesnt seem to update the LEDs properly. If they do light up, its extremely slow. Here is a code sample:


    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Adafruit_NeoPixel.h>
    #include <Bounce.h>
    
    // GUItool: begin automatically generated code
    AudioPlaySdWav           playSdWav2;     //xy=755,350
    AudioPlaySdWav           playSdWav4;     //xy=756,444
    AudioPlaySdWav           playSdWav3;     //xy=757,397
    AudioPlaySdWav           playSdWav1;     //xy=758,302
    AudioMixer4              mixer2;         //xy=973,395
    AudioMixer4              mixer1;         //xy=975,318
    AudioOutputI2S           i2s1;           //xy=1206,341
    AudioConnection          patchCord1(playSdWav2, 0, mixer1, 1);
    AudioConnection          patchCord2(playSdWav2, 1, mixer2, 1);
    AudioConnection          patchCord3(playSdWav4, 0, mixer1, 3);
    AudioConnection          patchCord4(playSdWav4, 1, mixer2, 3);
    AudioConnection          patchCord5(playSdWav3, 0, mixer1, 2);
    AudioConnection          patchCord6(playSdWav3, 1, mixer2, 2);
    AudioConnection          patchCord7(playSdWav1, 0, mixer1, 0);
    AudioConnection          patchCord8(playSdWav1, 1, mixer2, 0);
    AudioConnection          patchCord9(mixer2, 0, i2s1, 1);
    AudioConnection          patchCord10(mixer1, 0, i2s1, 0);
    AudioControlSGTL5000     sgtl5000_1;     //xy=996,524
    // GUItool: end automatically generated code
    
    // Use these with the Teensy Audio Shield
    #define SDCARD_CS_PIN    10
    #define SDCARD_MOSI_PIN  7
    #define SDCARD_SCK_PIN   14
    
    //Neopixel Chains Defined
    Adafruit_NeoPixel blinkStrip(9, 30, NEO_RGBW + NEO_KHZ800);
    Adafruit_NeoPixel alertStrip(5, 31, NEO_RGBW + NEO_KHZ800);
    Adafruit_NeoPixel commStrip(1, 34, NEO_RGBW + NEO_KHZ800);
    
    //Neopixel Color Variables
    uint32_t white = blinkStrip.Color(0, 0, 0, 255);
    uint32_t off = blinkStrip.Color(0, 0, 0, 0);
    
    //Variables for timed actions
    unsigned long startTime [9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    unsigned long duration [9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 } ;
    unsigned long previousMillisyel = 0;
    unsigned long previousMillisred = 0;
    unsigned long computeStatemillis = 0;
    unsigned long commStatemillis = 0;
    unsigned long prevCommMillis = 0;
    unsigned long notifyMillis = 0;
    unsigned long currentMillis =0;
    long commDelay = 0;
    const byte commDelays [] = {3000, 5000, 4000, 3500, 4500};
    int randNumber;
    const long alertInterval = 1000;
    const int computeStateWaitTime = 3000;
    
    //Button/switch Number and Button State Variables
    
    int lightFlashsw = 37;
    int lightFlash = 0;
    
    int ambSnd = 0;
    
    Bounce ambientSW = Bounce(ambientSoundpin, 15);
    
    
    //random blinking maximum and minimum values
    const int randMax = 8000;
    const int randMin = 2000;
    const int commMax = 15000;
    const int commMin = 8000;
    
    //Bool Statement Variables
    bool bootUp = false;
    bool light1state = false;
    bool light2state = false;
    bool light3state = false;
    bool light4state = false;
    bool light5state = false;
    bool light6state = false;
    bool light7state = false;
    bool light8state = false;
    bool light9state = false;
    
    
    
    
    void setup() {
      
      //Audio Shield Settings
      Serial.begin(9600);
      AudioMemory(8);
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.5);
      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);
        }
      }
      pinMode(13, OUTPUT); // LED on pin 13
      mixer1.gain(0, 0.5);
      mixer1.gain(1, 0.5);
      mixer1.gain(2, 0.5);
      mixer1.gain(3, 0.5);
      mixer2.gain(0, 0.5);
      mixer2.gain(1, 0.5);
      mixer2.gain(2, 0.5);
      mixer2.gain(3, 0.5);
      delay(1000);
      //End Audio Shield Settings
    
      //PinModes for Buttions and Switches
     
      pinMode (lightFlashsw, INPUT);
      digitalWrite (lightFlashsw, HIGH);
     
    
      //initialize Neopixels
      blinkStrip.begin();
      blinkStrip.show(); // Initialize all pixels to 'off'
      alertStrip.begin();
      alertStrip.show(); // Initialize all pixels to 'off'
      commStrip.begin();
      commStrip.show(); // Initialize all pixels to 'off'
    
      //Random Number Generation
      randomSeed(analogRead(0));
    
    }
    
    
    void loop() {
      randNumber = random(1, 10);
      currentMillis = millis();
    
       //Light Sequence for Left Panel. Also see
      //if (lightFlash == HIGH) {
      callLight(randNumber);
      //  } else {
      // blinkStrip.fill (white, 0, 9);
      // blinkStrip.show ();
      //}
    
      //Bridge Ambience Audio and Control
      if (playSdWav1.isPlaying() == false) {
        if (ambientSoundcounter == 0) {
          playSdWav1.play("BRGBK1.WAV");
          Serial.println("Start playing 1");
          delay(10);
        }
        if (ambientSoundcounter == 1) {
          playSdWav1.play("BRGBK2.WAV");
          Serial.println("Start playing 2");
          delay(10);
        }
        if (ambientSoundcounter == 2) {
          playSdWav1.play("BRGBK3.WAV");
          Serial.println("Start playing 3");
          delay(10);
        }
        while (playSdWav1.isPlaying()) {
          ambientSW.update();
          if (ambientSW.fallingEdge()) {
            playSdWav1.stop();
            ambientSoundcounter ++;
          }
          if (ambientSW.risingEdge()) {
            playSdWav1.stop();
            ambientSoundcounter ++;
          }
          if (ambientSoundcounter > 2) {
            ambientSoundcounter = 0;
            Serial.println("Back to 0");
          }
        }
      }
    
     
    }
    
    
    
    //Generate random function calls for random lights
    void callLight(int randNumber) {
    
      if (randNumber == 1)  {
        light1();
      }
      if (randNumber == 2) {
        light2();
      }
      if (randNumber == 3) {
        light3();
      }
      if (randNumber == 4) {
        light4();
      }
      if (randNumber == 5) {
        light5();
      }
      if (randNumber == 6) {
        light6();
      }
      if (randNumber == 7) {
        light7();
      }
      if (randNumber == 8) {
        light8();
      }
      if (randNumber == 9) {
        light9();
      }
    }
    
    // Functions to control random flashing lights, using led matrix and flashing groups of three together
    void light1() {
      if (currentMillis - startTime[1] >= duration[1])
      { startTime[1] = currentMillis;
        duration[1] = random(randMin, randMax);  //smallest and longest ON/OFF times for reandom blinking
        if (light1state == true) {
          blinkStrip.setPixelColor(0, white);
          light1state = false;
        } else {
          blinkStrip.setPixelColor(0, off);
          light1state = true;
        }
      }
      blinkStrip.show();
    }
    
    void light2() {
      if (currentMillis - startTime[2] >= duration[2])
      { startTime[2] = currentMillis;
        duration[2] = random(randMin, randMax);  //smallest and longest ON/OFF times for reandom blinking
        Serial.println("Light 3");
        if (light2state == true) {
          blinkStrip.setPixelColor(1, white);
          light2state = false;
        } else {
          blinkStrip.setPixelColor(1, off);
          light2state = true;
        }
      }
      blinkStrip.show();
    }
    void light3() {
      if (currentMillis - startTime[3] >= duration[3])
      { startTime[3] = currentMillis;
        duration[3] = random(randMin, randMax);  //smallest and longest ON/OFF times for reandom blinking
        Serial.println("Light 4");
        if (light3state == true) {
          blinkStrip.setPixelColor(2, white);
          light3state = false;
        } else {
          blinkStrip.setPixelColor(2, off);
          light3state = true;
        }
      }
      blinkStrip.show();
    }
    void light4() {
      if (currentMillis - startTime[4] >= duration[4])
      { startTime[4] = currentMillis;
        duration[4] = random(randMin, randMax);  //smallest and longest ON/OFF times for reandom blinking
        if (light4state == true) {
          blinkStrip.setPixelColor(3, white);
          light4state = false;
        } else {
          blinkStrip.setPixelColor(3, off);
          light4state = true;
        }
      }
      blinkStrip.show();
    }
    void light5() {
      if (currentMillis - startTime[5] >= duration[5])
      { startTime[5] = currentMillis;
        duration[5] = random(randMin, randMax);  //smallest and longest ON/OFF times for reandom blinking
        if (light5state == true) {
          blinkStrip.setPixelColor(4, white);
          light5state = false;
        } else {
          blinkStrip.setPixelColor(4, off);
          light5state = true;
        }
      }
      blinkStrip.show();
    }
    void light6() {
      if (currentMillis - startTime[6] >= duration[6])
      { startTime[6] = currentMillis;
        duration[6] = random(randMin, randMax);  //smallest and longest ON/OFF times for reandom blinking
        if (light6state == true) {
          blinkStrip.setPixelColor(5, white);
          light6state = false;
        } else {
          blinkStrip.setPixelColor(5, off);
          light6state = true;
        }
      }
      blinkStrip.show();
    }
    void light7() {
      if (currentMillis - startTime[7] >= duration[7])
      { startTime[7] = currentMillis;
        duration[7] = random(randMin, randMax);  //smallest and longest ON/OFF times for reandom blinking
        if (light7state == true) {
    
          blinkStrip.setPixelColor(6, white);
          light7state = false;
        } else {
          blinkStrip.setPixelColor(6, off);
          light7state = true;
        }
      }
      blinkStrip.show();
    }
    void light8() {
      if (currentMillis - startTime[8] >= duration[8])
      { startTime[8] = currentMillis;
        duration[8] = random(randMin, randMax);  //smallest and longest ON/OFF times for reandom blinking
        if (light8state == true) {
          blinkStrip.setPixelColor(7, white);
          light8state = false;
        } else {
          blinkStrip.setPixelColor(7, off);
          light8state = true;
        }
      }
      blinkStrip.show();
    }
    void light9() {
      if (currentMillis - startTime[0] >= duration[0]) //using zero because numbering system is 0-8 rather than 1-9
      { startTime[0] = currentMillis;
        duration[0] = random(randMin, randMax);  //smallest and longest ON/OFF times for reandom blinking
        if (light9state == true) {
          blinkStrip.setPixelColor(8, white);
          light9state = false;
        } else {
    
          blinkStrip.setPixelColor(8, off);
          light9state = true;
        }
      }
      blinkStrip.show();
    }

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,828
    Quote Originally Posted by grogking View Post
    I know there are some conflicts due to the audio shield with timing. Is there a way around this?
    Yes, there is indeed a way. Use the non-blocking WS2812Serial library instead of the blocking Adafruit_NeoPixel.

    However, the copy of WS2812Serial included with Teensyduino does not support SK6812 RGBW LEDs. The good news is RGBW support has been added on github. You'll need to get the latest from github and manually install it.

    https://github.com/PaulStoffregen/WS2812Serial

    The other issue with WS2812Serial is you can't just choose any pin like with Adafruit_NeoPixel. Only certain pins are supported. Scroll down on that page to see the documentation on supported pins. Or open any of its examples, from File > Examples > WS2812Serial.

    When you click that examples menu, you'll know whether you've successfully installed the newest copy, because it will have BasicTest_RGBW. If you see only BasicTest and FastLED_Cylon but no BasicTest_RGBW, that means Arduino is still using the original copy from the Teensyduino installer.

  3. #3
    Junior Member
    Join Date
    Feb 2020
    Posts
    2
    Thanks for the reply. I have the correct library loaded, but when I try to verify the code from the example sketch, it is giving me these errors:
    Code:
    BasicTest_RGBW:22: error: 'WS2812_GRBW' was not declared in this scope
     WS2812Serial leds(numled, displayMemory, drawingMemory, pin, WS2812_GRBW);
    
                                                                  ^
    
    BasicTest_RGBW: In function 'void setup()':
    BasicTest_RGBW:34: error: 'class WS2812Serial' has no member named 'setBrightness'
       leds.setBrightness(200); // 0=off, 255=brightest
    
            ^
    
    Multiple libraries were found for "WS2812Serial.h"
     Used: D:\Program
     Not used: D:\Documents\Arduino\libraries\WS2812Serial-master
    'WS2812_GRBW' was not declared in this scope
    I'm guessing it has something to do with the multiple libraries statement, but not sure how to deal with that.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,828
    Quote Originally Posted by grogking View Post
    I'm guessing it has something to do with the multiple libraries statement, but not sure how to deal with that.
    Yup, looks like it's using the wrong one. Easiest way is to just delete the copy it shouldn't be using.

Posting Permissions

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