WS2812fx coding adding black lights

Hi guys,

I’ve not yet played around with ws2812fx effects much but already like what under the hood!

My question is I am running the FX_MODE_MULTI_DYNAMIC which displays a mixture of colour randomly though the pixel strip much like Xmas lights. Is it a sample task to add a few random black lights (off)? Is this done via the segment function?

The code is

#include <arduino.h>
#include <WS2812FX.h>
#include <Keypad.h>
#define COLOUR_ORDER NEO_RGB //standard strips normally GRB - Double check Reds and Greens
#define COMS_FEQ NEO_KHZ800
#define CHIPSET WS2812

#define DATA_PIN 6  // digital pin used to drive the LED strip
#define NUM_LEDS 80  // number of LEDs on the strip

#define MIC_PIN A0 // Microphone
#define MIN_BRIGHTNESS   25
#define MAX_BRIGHTNESS 180
#define THRESHOLD        5

#define N 100 // Number of samples to take each time readSamples is called
#define fadeDelay 20 // delay time for each fade amount
#define noiseLevel 2 // if too sensitive raise number to 2 and so on

#define VOLTS 5 //5v strip
#define MAX_AMPS 500 //milliamps 60mA PER PIXEL (4.8Amp max for 80x)
#define BRIGHTNESS (1) //0-255

//***********below is for creating 4 sections*******************

#define NUM_SECTIONS 4
#define SECTION_LENGTH (NUM_LEDS / NUM_SECTIONS)

//********************************************

//set up for Switches or Keypad

const byte ROWS = 2; //final panel many need to be expanded upto 4 Rows 
const byte COLS = 2; //final panel many need to be expanded upto 3 Cols
char keys [ROWS] [COLS] = {
  {'1','2'},
  {'3','4'}
};
byte rowPins [ROWS] = {2, 3};
byte colPins [COLS] = {4, 5};
Keypad keypad = Keypad (makeKeymap(keys), rowPins, colPins, ROWS, COLS); 

char key;

//********************************************

WS2812FX ws2812fx = WS2812FX(NUM_LEDS, DATA_PIN, COLOUR_ORDER + COMS_FEQ);

uint16_t quietLevel = 0;
uint16_t maxSample = 0;
uint16_t maxSampleEver = 0;
unsigned long timer = 0;

void setup() {
  Serial.begin(115200);

  
  for(int i=0; i<20; i++) { // take some initial audio measurements to establish the "quiet" level
    quietLevel += analogRead(0); // 0-1023
    delay(25);
  }
  quietLevel /= 20;
  Serial.print("\nquietLevel is "); Serial.println(quietLevel);
  
  ws2812fx.init();
  ws2812fx.setBrightness(64);

  ws2812fx.setSegment(0,  0,  NUM_LEDS-1, FX_MODE_MULTI_DYNAMIC, 8000); // parameters: index, start, stop, mode, color, speed, reverse

  ws2812fx.start();
}

void loop() {
  
  uint16_t audioSample = abs(analogRead(0) - quietLevel); // take an audio sample
  if(audioSample > maxSample) maxSample = audioSample;

  if(millis() > timer) { // if the timer has expired, use the sampled audio to recalculate the LED brightness
    if(maxSample > THRESHOLD) { // ensure the audio is above the threshold to reduce LED flickering
      if(maxSample > maxSampleEver) maxSampleEver = maxSample;

      uint8_t newBrightness = map(maxSample, THRESHOLD, maxSampleEver, MIN_BRIGHTNESS, MAX_BRIGHTNESS);  //calculate a new brightness, properly scaled to the sampled audio
      ws2812fx.setBrightness(newBrightness);
    } else {
      ws2812fx.setBrightness(MIN_BRIGHTNESS);
    }

    maxSample = 0;
    timer = millis() + 100; // recalc brightness every 100ms
  }

  ws2812fx.service();
}

Thanks
Duncan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.