WS2818B LED strip changing brightness

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I am using a digital RGB strip to produce colour palettes. I have used code from github to do this, however I am wanting to increase the overall intensity/brightness by pressing a button.

I have tried to use the generic BRIGHTNESS term to set the brightness however it seems that I can not change the BRIGHTNESS value after the code has been uploaded. I have tried to put a if statement in to do this but it seems to not change the value of BRIGHTNESS. I have attached the code below, it can also be found at https://gist.github.com/kriegsma....

This code is more challenging for me to understand, hence I am asking this question. If you guys have any advice on how to change this so that I can adjust the level of brightness that would be much appreciated.

Cheers

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'll let others help with the code, but I have a thought or two.

 

Can you reliably read the press of the push button switch?

Are you familiar with "debouncing" PB switches?

Verifying you have a reliable input signal to trigger the rest of your project would seem to be step one.

 

Next know that the WS LEDs intensity is dependent upon the value sent to each of the three LEDs within each packaged WS LED.

For example, if you send the RED LED 0 it is off, send it 255 if is full on, send it 128 it is 1/2 on, send it 64 it is 1/4 on, etc.

Send all 3 colors 255 and you have full on, high intensity white.

Send all 3 colors 128 hand you have 1/2 intensity white.

 

Know that in the real world the LED's output isn't exactly linear to its drive level if the WS chip's driver has an analog current control inside the chip, although it will be pretty darn good if the driver just uses PWM.

Also know that your eye's perception of the intensity isn't linear to the drive signal you send to the LEDs, either.

(This is a much bigger factor than the LED's hardware driver.)

But the above is the overall concept.

 

Finally, The library you are using has several features above and beyond the simple turn the LEDs on / off with whatever color.

Perhaps you could start with a simpler library, get it working as you desire, and then move on to the more involved library if needed.

 

Those LEDs are soooo cool to play with!

 

JC

 

Edit: Typo

Last Edited: Sun. Feb 2, 2020 - 12:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please post code inline:

#include "FastLED.h"

// ColorWavesWithPalettes
// Animated shifting color waves, with several cross-fading color palettes.
// by Mark Kriegsman, August 2015
//
// Color palettes courtesy of cpt-city and its contributors:
//   http://soliton.vm.bytemark.co.uk/pub/cpt-city/
//
// Color palettes converted for FastLED using "PaletteKnife" v1:
//   http://fastled.io/tools/paletteknife/
//

#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif

#define DATA_PIN    4
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB
#define NUM_LEDS    15
//#define BRIGHTNESS  0

CRGB leds[NUM_LEDS];
const int buttonPin=6;
int buttonstate =0;
int BRIGHTNESS  =100;

// ten seconds per color palette makes a good demo
// 20-120 is better for deployment
#define SECONDS_PER_PALETTE 10


void setup() {
  delay(3000); // 3 second delay for recovery
  pinMode(buttonPin, INPUT);
  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS)
    //.setCorrection(TypicalLEDStrip) // cpt-city palettes have different color balance
    .setDither(BRIGHTNESS < 255);

  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);
}

// Forward declarations of an array of cpt-city gradient palettes, and 
// a count of how many there are.  The actual color palette definitions
// are at the bottom of this file.
extern const TProgmemRGBGradientPalettePtr gGradientPalettes[];
extern const uint8_t gGradientPaletteCount;

// Current palette number from the 'playlist' of color palettes
uint8_t gCurrentPaletteNumber = 0;

CRGBPalette16 gCurrentPalette( CRGB::Black);
CRGBPalette16 gTargetPalette( gGradientPalettes[0] );


void loop()
{
  EVERY_N_SECONDS( SECONDS_PER_PALETTE ) {
    gCurrentPaletteNumber = addmod8( gCurrentPaletteNumber, 1, gGradientPaletteCount);
    gTargetPalette = gGradientPalettes[ gCurrentPaletteNumber ];
  }

  EVERY_N_MILLISECONDS(40) {
    nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 16);
  }
  
  colorwaves( leds, NUM_LEDS, gCurrentPalette);

  FastLED.show();
  FastLED.delay(20);
   buttonstate=digitalRead(buttonPin);
    if (buttonstate ==HIGH){
    BRIGHTNESS = BRIGHTNESS+10; 
    delay(10);
    FastLED.setBrightness(BRIGHTNESS);
  }
  if(BRIGHTNESS>=255){
    BRIGHTNESS=0;
  }
}


// This function draws color waves with an ever-changing,
// widely-varying set of parameters, using a color palette.
void colorwaves( CRGB* ledarray, uint16_t numleds, CRGBPalette16& palette) 
{
  static uint16_t sPseudotime = 0;
  static uint16_t sLastMillis = 0;
  static uint16_t sHue16 = 0;
 
  uint8_t sat8 = beatsin88( 87, 220, 250);
  uint8_t brightdepth = beatsin88( 341, 96, 224);
  uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
  uint8_t msmultiplier = beatsin88(147, 23, 60);

  uint16_t hue16 = sHue16;//gHue * 256;
  uint16_t hueinc16 = beatsin88(113, 300, 1500);
  
  uint16_t ms = millis();
  uint16_t deltams = ms - sLastMillis ;
  sLastMillis  = ms;
  sPseudotime += deltams * msmultiplier;
  sHue16 += deltams * beatsin88( 400, 5,9);
  uint16_t brightnesstheta16 = sPseudotime;
  
  for( uint16_t i = 0 ; i < numleds; i++) {
    hue16 += hueinc16;
    uint8_t hue8 = hue16 / 256;
    uint16_t h16_128 = hue16 >> 7;
    if( h16_128 & 0x100) {
      hue8 = 255 - (h16_128 >> 1);
    } else {
      hue8 = h16_128 >> 1;
    }

    brightnesstheta16  += brightnessthetainc16;
    uint16_t b16 = sin16( brightnesstheta16  ) + 32768;

    uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
    uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
    bri8 += (255 - brightdepth);
    
    uint8_t index = hue8;
    //index = triwave8( index);
    index = scale8( index, 240);

    CRGB newcolor = ColorFromPalette( palette, index, bri8);

    uint16_t pixelnumber = i;
    pixelnumber = (numleds-1) - pixelnumber;
    
    nblend( ledarray[pixelnumber], newcolor, 128);
  }
}

// Alternate rendering function just scrolls the current palette 
// across the defined LED strip.
void palettetest( CRGB* ledarray, uint16_t numleds, const CRGBPalette16& gCurrentPalette)
{
  static uint8_t startindex = 0;
  startindex--;
  fill_palette( ledarray, numleds, startindex, (256 / NUM_LEDS) + 1, gCurrentPalette, 255, LINEARBLEND);
}
// Gradient palette "temperature_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/arendal/tn/temperature.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 144 bytes of program space.
// Gradient palette "arctic_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/arendal/tn/arctic.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 452 bytes of program space.

//DEFINE_GRADIENT_PALETTE( Blue_Cyan_Yellow_gp ) { //Like
//    0,   0,  0,255,
//   63,   0, 55,255,
//  127,   0,255,255,
//  191,  42,255, 45,
//  255, 255,255,  0};

  // Gradient palette "bhw1_27_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw1/tn/bhw1_27.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 20 bytes of program space.

DEFINE_GRADIENT_PALETTE( bhw1_27_gp ) {
    0,  16,  2, 50,
   61,  87, 31,180,
  114, 255,255,255,
  196,  87, 31,180,
  255, 220, 27,112};


  

// Single array of defined cpt-city color palettes.
// This will let us programmatically choose one based on
// a number, rather than having to activate each explicitly 
// by name every time.
// Since it is const, this array could also be moved 
// into PROGMEM to save SRAM, but for simplicity of illustration
// we'll keep it in a regular SRAM array.
//
// This list of color palettes acts as a "playlist"; you can
// add or delete, or re-arrange as you wish.

const TProgmemRGBGradientPalettePtr gGradientPalettes[] = {
//Blue_Cyan_Yellow_gp, //Like
bhw1_27_gp
 };


// Count of how many cpt-city gradients are defined:
const uint8_t gGradientPaletteCount = 
  sizeof( gGradientPalettes) / sizeof( TProgmemRGBGradientPalettePtr );

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]