Constraining sensor values question..

Constraining sensor values question… I’ve got a sensor giving me data, and want to use that data to control the brightness of my neopixels. So far so good.

I’m having trouble when the sensor numbers get too low or too high – the LEDs don’t just stay “off” if the numbers end up below 0, or stay “full bright” if the numbers go above 255. Here’s how I’ve tried to fix this… I think I’m doing it wrong. What’s a better way to constrain my post-calculated sensor values into 0-255?

proxbrightness = 255-(SENSORDATA*10);

if (proxbrightness < 1){
proxbrightness=1;
}
else if (proxbrightness > 250){
proxbrightness=250;
}
FastLED.setBrightness(proxbrightness);

Thanks!!

think I answered my own question, thanks to +Richard Bailey’s awesome Circuit Playground code. Thanks!

FastLED.setBrightness(constrain(map ((average*10),0,1023,0,255),10,255));

I am glad my code helped.

Another solution, although this is quickly typed on my phone and I might forget something. Say the sensor reports -500 to 500. (Sens+500)*255/1000 will map it.

oh this is helpful as i was working with some map function stuff and i think the constrain will fix my issue!