2 potentiometers controlling a single LED

Hi, sorry for a neeb question. I have found this sketch that works great in my application but for some reasons I would like to split a part with map to two parts for each of potentiometers. I use some values from one potentiometers for some other actions so I need to do a map for first like this :
100, 900, 100, 3000
and second one:
0, 1023,100,3000
Thanks.

// CONSTANT DEFINITION
// you may need to change these constants to your hardware
const byte Analog[] {A0, A1};
const byte Output_ {2};
const byte Button_ {A2};
void setup() {
  Serial.begin(9600);
  pinMode (Output_, OUTPUT);
  pinMode( Button_,INPUT_PULLUP);
}
void loop () {
  unsigned long currentTime = millis();
  static unsigned long LEDmillis;
  if (currentTime - LEDmillis >= (unsigned long) map(analogRead(Analog[digitalRead(Output_)]), 0, 1023, 50, 500) && !digitalRead(Button_)) {
    digitalWrite(Output_, !digitalRead(Output_));
    LEDmillis = currentTime;
  }
}

What are those?

What do you want to change?

The first potentiometer changes delay between blinks and the second potentiometer changes duration of blink.

some reasons :slight_smile:
ok, I use values of first potentiometer like this:
If ...
0-100 for action: digitalwrite Led_Off ONLY,...
if...
100-900 action: led on-off depends on values
if...
900-1023: digitalwrite Led_ON ONLY....

Second potentiometer determines the Led Off time - works great (100-3000 ms)

lets say I have this map (0,1023,100,3000). I like 100 ms as smallest time for ON or Off
The problem is that when I go from pot. value 100 to 101, it starts blink depends on Pot. value but the value 100 means in map a value cca 428 ms.
But I would like have the smallest remaped value for LedON also 100 ms not 428 ms.

I though I just do map like 0,1023,0,3000, but it does not make much sense to second potentiometer with LedOFF. There is no sense to use smaller values then 100 ms...

So ...basically, I need to have map functions for each of potentiometers.

I do not follow you. Use translate.google.com to translate from your preferred language into English and stop trying to use slang.

map(analogRead(A0), 100, 900, 100, 3000);
map(analogRead(A1), 0, 1023, 100, 3000);

I do not understand why the originator of this "code" chose this:

Analog[0], Analog[1];

to represent this:

A0, A1;

The button chooses "0" or "1" (A0, A1) to do the analog reading.

do you need to map the one pot value to 2 different ranges?

    int pot  = analogRead (A0);
    int val0 = map (pot, 100,  900, 100, 3000);
    int val1 = map (pot,   0, 1023, 100, 3000);
1 Like

Hi, @tomastnt

That means the output value from the map function will have a span of -262 to 3445 for the full 0 to 1023 of the potentiometer.
The map function does not stop at your limits.

I think you need to look at the "constrain" function as well.
https://docs.arduino.cc/language-reference/en/functions/math/constrain/

Tom... :smiley: :+1: :coffee: :australia:

Hi, @tomastnt

Looking at your code I believe in your haste to get compact code, you are forgetting about the fact that the there is only ONE ADC and it is switched to each input, it takes time for the ADC to respond to a change in input value.
So the problem you have could be due to values not being stable at the ADC input when you do the conversion.

Can I suggest you spread your code out and do this.

At the start of the void loop() read your ADC and store the variables, use those variables in the rest of your code.
Tip; read each ADC channel twice to assure accurate values,

eg;

val1 = analogRead(A0);
val1 = analogRead(A0);
val2 = analogRead(A1);
val2 = analogRead(A1);

Only read your ADC at the start of the loop.
Then you could possibly be able to use 0, 1023 to 100, 3000 map on both pots.

Tom.. :smiley: :+1: :coffee: :australia:

1 Like

The logic might be clearer if you create meaningful variables for the different pots and don't try to do the reading, logic, and processing all in the same line:

If you name the first pot something like modePot, then you could use if statements to separate the different modes. Something like this completely untested snippet:

int modePot = analogRead(ModePotPin);

if(modePot <=100) {
  digitalWrite(Output_,OffLevel);
} else if (modePot  >= 900) {
  digitalWrite(Output_,OnLevel);
} else {
    unsigned long toggleTime = map(analogRead(TogglePotPin),0,1025,0,500
    if(currentTime - LEDmillis >= toggleTime && !digitalRead(Button_)) {
      digitalWrite(Output_, !digitalRead(Output_));
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.