Using a potentiometer to adjust a threshold

I'm trying to use a potentiometer as a method to adjust the sensitivity for color-changing LED to be used as a party light.

The idea is that an LED will display a specific color if a defined threshold is met; however, I want to adjust that value relative to the setting on a potentiometer.

Therefore if music is quieter I can adjust the threshold down or vice versa

How I'm reading the potentiometer value currently

float SensitivityValue = analogRead(Sensitivity);

Serial.println((SensitivityValue)/1023);

I am able to read the state of the potentiometer easily and I've been able to a value between 0.00 and 1. However, I want to try and get a value between 0.5 (Max potentiometer) and 1 (Min). Because I intend to divide my threshold value by the Sensitivity value in order to increase it.

Code snippet of how the threshold works.

else if (value > ( 520 / SensitivityValue) && value <= (525 / SensitivityValue))

{

Serial.println("Yellow"); // Value

RGBColor(255, 255, 0);

I would also appreciate any other suggestions on how to accomplish this.

If x goes from 0.0 to 1.0, then 0.5 + 0.5 * x will go from 0.5 to 1.

Did you skip Algebra? Computer maths works just like real maths.

Just watch out for integer types and little details. :wink:

a7

Why not read the analogue value and use the map() function to map it to the range that you require, or even easier, use the raw value to determine the threshold

1 Like

I think he wants it to go from 1 to 0.5, rather than 0.5 to 1. :grinning:

1 Like

Haahaha. Dont need much algebra to paint walls.

Thanks tho I'll give it a try.

I guess that's a different map from googles maps.

I am using the raw value. I've measured my ambient nose floor to be between 510 and 515.

So if I map the upper and lower values of the threshold / colour segment that should work?

I've been able to a value between 0.00 and 1. However, I want
to try and get a value between 0.5 (Max potentiometer) and 1 (Min). 

Right. Then it can’t be done, not even with Algebra.

a7

This works exactly as I wanted.

const float Sensitivity = A1;

void setup(){   
  Serial.begin(9600); //initialize serial
}


void loop()

{
  float val = analogRead(1);
  val = map(val, 0, 1023, 50, 100);
    Serial.println((val)/100);
}
1 Like