Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!




Gradient with movin...
 
Share:
Notifications
Clear all

Gradient with moving ball

Page 3 / 3

 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2018
 

Hmm, not what I expected, but we seem to be getting closer.

So my first thought is that you're using colors in the gradient that are already hitting 255 values (for red, green and/or blue). Which may limit how much brighter the color can get?

Additionally you're using an overal brightness of 15 in this line:

FastLED.setBrightness(15);

 

Maybe setBrightness should be 255 (back to normal).
Now the gradient will be brighter than you'd like, so you'll have to tune the color selection in the gradient build.

Either remove the setBrightness line of set it to 255:

FastLED.setBrightness(255);

 

Now we have to adjust your gradient, which was:

fill_gradient_RGB(GradientLeds, GRADIENT, CRGB(255,50,0), CRGB(255,70,0), CRGB(0,170,30), CRGB(0,255,255));

 

to (I'm just guessing the color numbers by multiplying them with 0.06, since overall brightness was 15, so 15/255 = 0.06).
The values may or may not be as expected, so some finetuning may be needed.

fill_gradient_RGB(GradientLeds, GRADIENT, CRGB(15,3,0), CRGB(15,4,0), CRGB(0,10,2), CRGB(0,15,15));

 

When using maximizeBrightness now (without a parameter), the brightness of the meteor may become brighter than before?
I'm just guessing here since I still cannot test the code.


ReplyQuote
(@trace)
Estimable Member
Joined: 2 years ago
Posts: 115
Topic starter  

@hans

Yeah, I already thought of FastLED.setBrightness as an overall value could cause the problem. But this is a nice and easy way to adjust the brightness.

With your solution now, it works. Adjusting the color values to get the wanted brightness is genius. And not putting any parameter in the maximizeBrightness makes the meteor as bright as 255 (maximize ;) ). Using a value adjusts the brightness to what the maximization (does this workd exist?) can go. Thats nice.

But there is also a little downside. It needs calculation and a bit of trial and error just to change the brightness of the gradient. Im sure there is a solution for this, right? Like expressing the values mathematically? If not, well, it can´t be always easy :D


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2018
 

Cool! We're making good progress!!! 😁 

As for the calculations,... yes you can put them straight in the colors, for example CRGB(255*0.6, 50*0.6, 0) but I don't think that is what you're asking. Or is it?
You could even define a constant for that. Or ...

We could maybe use fadeToBlack (nscale8() could be a better alternative since it will never fade to fully black) to make all LEDs in the Gradient array darker:

fill_gradient_RGB(GradientLeds, GRADIENT, CRGB(255,50,0), CRGB(255,70,0), CRGB(0,170,30), CRGB(0,255,255));

for(int i=0; i<NUM_LEDS;i++) {
  GradientLeds[i].fadeToBlackBy( 64 );
  // alternative:
  // GradientLeds[i].nscale8( 64 );
}

 

 Is this what you were thinking of? 😁 

 


ReplyQuote
(@trace)
Estimable Member
Joined: 2 years ago
Posts: 115
Topic starter  

@hans

In both cases, yes....thats what I was thinking about. Didn´t know you can express the values mathematically "inside" the value itself, good to know.

I thought fadeToBlackBy is only used for fading. And it works great, but the value is of course "inverted". Thats why nscale8 is the better solution. It is just more intuitive to express the brightness from 0-255 when 0 is dark and 255 the brightest.

Thanks for all of your work and Im sure the Gradient-Meteor effect will be usefull for others too. And whith this code it is also possible to set the brightness values to have a bright gradient with a darker meteor, nice!


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2018
 

Awesome! 😊 👍 

Would you mind sharing the final code? (not a requirement, but maybe other users may like it)


ReplyQuote


(@trace)
Estimable Member
Joined: 2 years ago
Posts: 115
Topic starter  

@hans

Yes of course. Im also very happy if someone posts a final working code instead of snippets.

Here it is. Including LEDs outside the Gradient which light up by pushing a button (not very smart coding but it works).

#include <FastLED.h>

#define NUM_LEDS  70
#define LED_PIN   5
#define GRADIENT 64

#define buttonPin1 11
#define buttonPin2 10
#define buttonPin3 9
#define buttonPin4 8
#define buttonPin5 7



int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;



CRGB leds[NUM_LEDS];
CRGB GradientLeds[GRADIENT];



void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);

  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);
  pinMode(buttonPin5, INPUT_PULLUP);
  
  makeGradient();
}


void loop() 
{

  meteorRain(0x33,0xff,0x00, 10 ,60 ,true, 40);

} 

void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) 
{  

  for(int i = 0; i < GRADIENT+GRADIENT; i++) 
  {
    // fade color to background color for all LEDs
    for(int j=0; j < GRADIENT; j++) {
      if( (!meteorRandomDecay) || (random(10) > 1) ) {
        leds[j] = fadeTowardColor(leds[j], GradientLeds[j], meteorTrailDecay ); 
      }
    }

    // draw meteor
 
    for(int j = 0; j < meteorSize; j++) {
      if( ( i-j < GRADIENT) && (i-j >= 0) ) {
        leds[i-j] = GradientLeds[i - j];
        leds[i-j].maximizeBrightness(50);
      }
    }


      buttonState1 = digitalRead(buttonPin1);       // read the state of the pushbutton value
  if (buttonState1 == LOW) { 
    fill_solid(leds,GRADIENT, CRGB(0, 0, 0));
    
  }

   FastLED.show();


leds[66] = CRGB(0, 0, 0);
      buttonState2 = digitalRead(buttonPin2);       // read the state of the pushbutton value
  if (buttonState2 == LOW) { 
    leds[66] = CRGB(0, 0, 255);
  }

    leds[67] = CRGB(0, 0, 0);
      buttonState3 = digitalRead(buttonPin3);       // read the state of the pushbutton value
  if (buttonState3 == LOW) { 
    leds[67] = CRGB(255, 0, 255);
  }

      leds[68] = CRGB(0, 0, 0);
      buttonState4 = digitalRead(buttonPin4);       // read the state of the pushbutton value
  if (buttonState4 == LOW) { 
    leds[68] = CRGB(255, 255, 0);
  }

      leds[69] = CRGB(0, 0, 0);
      buttonState5 = digitalRead(buttonPin5);       // read the state of the pushbutton value
  if (buttonState5 == LOW) { 
    leds[69] = CRGB(0, 255, 255);
  }
    
    delay(SpeedDelay);
  }
}

// Functions from Kriegsman example
CRGB fadeTowardColor( CRGB& cur, const CRGB& target, uint8_t amount)
{
  nblendU8TowardU8( cur.red,   target.red,   amount);
  nblendU8TowardU8( cur.green, target.green, amount);
  nblendU8TowardU8( cur.blue,  target.blue,  amount);
  return cur;
}

// function used by "fadeTowardColor"
void nblendU8TowardU8( uint8_t& cur, const uint8_t target, uint8_t amount)
{
  if( cur == target) return;
  
  if( cur < target ) {
    uint8_t delta = target - cur;
    delta = scale8_video( delta, amount);
    cur += delta;
  } else {
    uint8_t delta = cur - target;
    delta = scale8_video( delta, amount);
    cur -= delta;
  }
}

void makeGradient() {


  // make a gradient (can take up to 4 colors, just using 2 for this example)
fill_gradient_RGB(GradientLeds, GRADIENT, CRGB(255,50,0), CRGB(255,70,0), CRGB(0,170,30), CRGB(0,255,255));
  
  // Copy the gradient to the actual leds and show it.
  for(int i=0; i<GRADIENT; i++) {
    GradientLeds[i].nscale8( 15 );
  }
 
  FastLED.show();
  }

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2018
 

Awesome! Thanks Trace 😊 


ReplyQuote
Page 3 / 3

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: