# Gradient with moving ball – Page 3 – Arduino – Forum

Page 1 of 1

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!

Share:
Clear all

# [Solved] Gradient with moving ball

37 Posts
2 Users
0 Reactions
10.9 K Views
(@hans)
Joined: 11 years ago
Posts: 2724

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);`

`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.

(@trace)
Estimable Member
Joined: 5 years ago
Posts: 170
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

(@hans)
Joined: 11 years ago
Posts: 2724

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++) {
// alternative:
}```

Is this what you were thinking of? 😁

(@trace)
Estimable Member
Joined: 5 years ago
Posts: 170
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!

(@hans)
Joined: 11 years ago
Posts: 2724

Awesome! 😊 👍

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

(@trace)
Estimable Member
Joined: 5 years ago
Posts: 170
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 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];

void setup() {

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

}

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)
{

{
// fade color to background color for all LEDs
for(int j=0; j < GRADIENT; j++) {
if( (!meteorRandomDecay) || (random(10) > 1) ) {
}
}

// draw meteor

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

if (buttonState1 == LOW) {

}

FastLED.show();

leds[66] = CRGB(0, 0, 0);
if (buttonState2 == LOW) {
leds[66] = CRGB(0, 0, 255);
}

leds[67] = CRGB(0, 0, 0);
if (buttonState3 == LOW) {
leds[67] = CRGB(255, 0, 255);
}

leds[68] = CRGB(0, 0, 0);
if (buttonState4 == LOW) {
leds[68] = CRGB(255, 255, 0);
}

leds[69] = CRGB(0, 0, 0);
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;
}

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;
}
}

// make a gradient (can take up to 4 colors, just using 2 for this example)

// Copy the gradient to the actual leds and show it.
}

FastLED.show();
}```

(@hans)