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 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.