Dimming a Led in an Interval

Good Evening,
I am trying to change the brightness of a Led inbetween two Values determined by two potentiometers. Third potentiometer imitates a sensor which controlls Led's brightness. To be more specific my aim is,

%40 Brightness of Led when "sensor" reaches "minVal"
When sensor is between "minVal" and "maxValue" brightness increases until it reaches %100 at "maxVal"
Stays at %100 brightness when sensor value is greater than "maxVal"

Thanks in advance,

int ledPin = 7;
int sensorPot = A4; // its a potentiometer for now
int maxPot = A6; // potentiometer 
int minPot = A2; // potentiometer 
void setup() {
  
  pinMode(ledPin, OUTPUT);
  pinMode(sensorPot, INPUT);
  pinMode(maxPot, INPUT);
  pinMode(minPot, INPUT);

  Serial.begin(9600);
 
}
void loop() {
  
  int minVal = analogRead(minPot);
  int maxVal = analogRead(maxPot);
  int sensor = analogRead(sensorPot);
  int writeval;
    Serial.println("sensor:");
    Serial.println(sensor/4);
    Serial.println("min val:");
    Serial.println(start/4);
    Serial.println("max val:");
    Serial.println(maxVal/4);
    sensor = constrain(sensor, minVal, maxVal);  
    analogWrite(ledPin,sensor);
}

You're printing things divided by 4 presumably to convert the 0-1023 of an analogRead to 0-255.

But when you do the constrain and the analogWrite to the LED you're back to using the original 0-1023 values. That's not going to work out well.

Steve

slipstick:
You're printing things divided by 4 presumably to convert the 0-1023 of an analogRead to 0-255.

But when you do the constrain and the analogWrite to the LED you're back to using the original 0-1023 values. That's not going to work out well.

Steve

Do you have any further ideas about how can I code this?

sensor = constrain(sensor, minVal/4, maxVal/4);

BulldogLowell:

sensor = constrain(sensor, minVal/4, maxVal/4);

And what do you suggest about 40% ----> 100% logic?

sensor = constrain(...
if (sensor < 102) sensor = 102;

BulldogLowell:

sensor = constrain(...

if (sensor < 102) sensor = 102;

I couldn't figure it out. Can you share a more detailed answer and code snippet?

You have a MinInput and MaxInput set by the two pots.
You have a MinOutput (40%) and MaxOutput (100%) which are constants.

Constrain the input and map to the output:

void loop() {
  int minInput = analogRead(minPotPin);
  int maxInput = analogRead(maxPotPin);
  const byte MinOutput = (255 * 4) / 10;  // 40% of full brightness
  const byte MaxOutput = 255;  // 100% of full brightness
  int input = analogRead(sensorPotPin);
    Serial.print("Input: ");
    Serial.println(input);
    Serial.print("min Input: ");
    Serial.println(minInput);
    Serial.print("max Input: ");
    Serial.println(maxInput);
    input = constrain(input, minInput, maxInput); 
    byte output = map(input, minInput, maxInput, MinOutput, MaxOutput);
    analogWrite(ledPin,output);
}

The map() function magically takes care of the scale difference between the analogRead() and analogWrite() since its function is to map between two ranges.