Controlling LED Strip Brightness with Proximity sensor

Hello all,

I am trying to evolve the code that I have been working on. I am controlling the brightness and color palette of my LED strip using a proximity sensor. I would like the brightness of the strip to have a smoother flow/fade between values. Ideally, I would like it to respond similar to how I have coded the color palettes to blend from one into the next.

The issue here is that the code that I am using uses a "current palette" and a "target palette" in the transition process between palettes. I tried to use the same code with brightness instead of color, but I haven't figured out how to/of you ever can set a current brightness, target brightness and approach solving the problem from this angle.

The code uses the functions:

CRGBPalette16 currentPalette( CRGB::White);
CRGBPalette16 targetPalette( CRGB::Black );

and

for ( int i = 0; i < NUM_LEDS; i++) {
leds = ColorFromPalette( currentPalette, colorIndex + sin8(i * 16), brightness);
colorIndex += 3;
}
to establish the colors that are displayed on the strip. Unfortunately, I havent been able to figure out how to substitute "CRGBPalette16" and "ColorFromPalette" with brightness functions.
Hmm... suggestions?
The way that it is currently coded produces a change in brightness based on proximity, but the result generates a glitchy/binary brightness effect and is not as smooth as I would like.
```
*#include <FastLED.h>
#include <NewPing.h>
#define DATA_PIN     7
#define NUM_LEDS    94
#define LED_TYPE    WS2813
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
CRGBPalette16 currentPalette( CRGB::White);
CRGBPalette16 targetPalette( CRGB::Black );
const  int numReadings =  20 ;
int readings [ numReadings ] ;
int readIndex = 0;
int total = 0;
int average = 0;
int newValue = 0;
int lastpixel = 0;
TBlendType    currentBlending;

#define UPDATES_PER_SECOND 100
#define EP1  12  // Arduino pin tied to both trigger and echo pins on the ultrasonic sensor.
#define TP1 13
#define TP2 11
#define EP2 10
#define MAX_DISTANCE 64 // Maximum distance we want to ping for (in centimeters). Maximum sensor dista
#define SONAR_NUM 2
NewPing sonar[SONAR_NUM] = {   // Sensor object array.
NewPing(11, 10, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(13, 2, MAX_DISTANCE),
};

void setup() {
delay( 1500 ); // power-up safety delay
Serial.begin(115200);
Serial.println("resetting");
LEDS.addLeds<WS2813, DATA_PIN, RGB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
currentBlending = LINEARBLEND;
for (int thisReading = 0 ; thisReading < numReadings ; thisReading++)
{
readings [ thisReading ]  =  0 ;
}
int proxcont;
proxcont = sonar[1].ping_cm();
int colorchange;
colorchange = sonar[0].ping_cm();
}

void loop()
{
ChangePalettePeriodically();

uint8_t maxChanges = 10;
nblendPaletteTowardPalette( currentPalette, targetPalette, maxChanges);
nblendBrightnessTowardBrightness( currentBrightness, targetPalette, maxChanges);

static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */
FillLEDsFromPaletteColors( startIndex);

FastLED.show();
FastLED.delay(1 / UPDATES_PER_SECOND);

for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
delay(1); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print(i);
Serial.print("=");
Serial.print(sonar[i].ping_cm());
Serial.print("cm ");
}
Serial.println();
delay(1);

int proxcont;
proxcont = sonar[1].ping_cm();
int colorchange;
colorchange = sonar[0].ping_cm();
int brightness = 160 - 2.5 * proxcont;

FastLED.setBrightness(brightness);

}
void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
uint8_t brightness = 255;

for ( int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette( currentPalette, colorIndex + sin8(i * 16), brightness);
colorIndex += 3;
}
}

void ChangePalettePeriodically()
{
int colorchange;
colorchange = sonar[0].ping_cm();
uint8_t secondHand;
secondHand = 54;

CRGB p = CHSV( HUE_PURPLE, 255, 255);
CRGB g = CHSV( HUE_GREEN, 255, 255);
CRGB b = CRGB::Black;
CRGB w = CRGB::White;
if ( secondHand == 2 )  {
targetPalette = occ028_gp;
}
if ( secondHand == 4 )  {
targetPalette = occ028_gp;
}
if ( secondHand == 6 )  {
targetPalette = occ028_gp;
}

.... etc
}

void BrightnessOfLEDS( uint8_t Brightness)
{

for ( int i = 0; i < NUM_LEDS; i++) {
leds[i] = FastLED.setBrightness( currentBrightness, colorIndex + sin8(i * 16));
colorIndex += 3;
}
}

void ChangeBrightnessPeriodically()
{
int brightness;
brightness = sonar[1].ping_cm();
uint8_t secondHand;
secondHand = brightness;

if ( secondHand == 2 )  {
targetPalette = occ028_gp;
}
if ( secondHand == 4 )  {
targetPalette = occ028_gp;
}
if ( secondHand == 6 )  {
targetPalette = occ028_gp;
}

..... etc

}*
```

int proxcont;
proxcont = sonar[1].ping_cm();
int colorchange;
colorchange = sonar[0].ping_cm();
}

This code was a complete waste of effort typing it. It accomplishes absolutely nothing.

nblendBrightnessTowardBrightness( currentBrightness, targetPalette, maxChanges);

Why are you blending currentBrightness to targetPalette?

delay(1); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
That is NOT what that code does.

delay(1);

I just want to scream "You f**king moron", when someone complains that their code is slow, when it littered with crap like this. But, I won't.

int colorchange;
colorchange = sonar[0].ping_cm();

You do not appear to be aware that you can declare AND value a variable in one statement. It's time you became aware.

It's also long past time that you learned about scope.