Controlling LED Strip Brightness with Proximity sensor

Hello all,

I am trying to control various parameters of a WS2813 LED strip. In essence, I want to control the brightness (and other parameters) of the LED strip using a proximity sensor.

I have established the brightness of the LED Strip as int proxcont, but havent figured out how to establish it as a int/variable that can change when included in IF statements (like I attempted at the bottom of the code). As it stands, the brightness of the LED Strip is established in the beginning of the code and doesnt change during the IF statements… Is there a way that the brightness value of the LED Strips can be controlled by an int? Am I coming at this from the wrong angle?

This is where I am at thus far:

#include <FastLED.h>
#include <NewPing.h>
#define LED_PIN 7
#define NUM_LEDS 151
#define CHIPSET WS2813
#define TEMPERATURE_1 Tungsten100W
#define TEMPERATURE_2 OvercastSky
#define PING_PIN 12 // Arduino pin tied to both trigger and echo pins on the ultrasonic sensor.
#define MAX_DISTANCE 120 // Maximum distance we want to ping for (in centimeters). Maximum sensor dista

int distance;

int proxcont = 0;

const int numReadings = 20 ;
int readings [ numReadings ] ;
int readIndex = 0;
int total = 0;
int average = 0;
int newValue = 0;
int lastpixel = 0;

// How many seconds to show each temperature before switching
#define DISPLAYTIME 20
// How many seconds to show black between switches
#define BLACKTIME 3

void setup() {
delay(1000); // power-up safety delay

for(int thisReading = 0 ; thisReading < numReadings ; thisReading++) {
readings [ thisReading ] = 0 ; }


void loop()
distance = sonar.ping_cm();
delay(30); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("distance: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
// draw a generic, no-name rainbow
static uint8_t starthue = 0;
fill_rainbow( leds + 0, NUM_LEDS - 5, --starthue, 20);

// Choose which ‘color temperature’ profile to enable.
uint8_t secs = (millis() / 1000) % (DISPLAYTIME * 2);
if( secs < DISPLAYTIME) {
FastLED.setTemperature( TEMPERATURE_1 ); // first temperature
leds[0] = TEMPERATURE_1; // show indicator pixel
} else {
FastLED.setTemperature( TEMPERATURE_2 ); // second temperature
leds[0] = TEMPERATURE_2; // show indicator pixel

if(distance > 0 && distance <= 5) {
proxcont = 10;
if(distance = 6 && distance < 10) {
proxcont = 30;
if(distance > 10) {
proxcont = 50;


Please read the forum guidelines in the sticky post and then edit and fix your post above.

Is there a way that the brightness value of the LED Strips can be controlled by an int?

In theory it can be (or could have been) done the way you are imagining/expecting, yes. The C language has features that would make it possible. You would create a variable and then give a “reference” or “pointer” to that variable to another piece of code. That piece of code could then read the variable any time to see if its value had changed. But it’s not a very common way to write code. And it’s not the way that the developers of the FasLED library wrote the library. You need to call setBrightness() every time you want to change the brightness. The setBrightness() method is given only the value of the variable at the point in time it is used. It is not given a reference/pointer to the variable and cannot read its value later.

@SnacksDaily will be enjoying a short break from the forum.