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;
}
}
some reasons
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.
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.
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,
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: