As my project needs a lot of ports ,in order to reduce the number of ports used, I'm trying to do a selector with a potentiometer, using only one analog port and a pot.
The problem is that as the digital values vary over the time (even if I don't touch the pot) there are some places in the path of the potentiometer that are not stable (the places between two values), and changes my selection over the time. In the example, in some point in the middle of the pot I get :
4,3,3,3,4,4,3
I've tried to use variations of the smooth example program (doing an average of input values) without result.
Maybe I can try a hardware selector with a resistive ladder, or buttons with a resistive ladder (also using the analog port), but before exploring these options, has anybody a code hint of how I can get an stable selector reading avoiding the change of value in a "frontier zone"?
The problem is that as the digital values vary over the time
No. The problem is that you are using the map function instead of an if/else if/else structure. You should revise your code to only "map" a new value if the reading changes by more than some amount.
You could put some deadband between the values. 1023 / 7 selections is about 146 / selection. So you could use a switch case with ellipses to select the track and have a bit of deadband between select values.
int var = analogRead(A0);
switch(var)
{
case 20 ... 140:
Serial.println("track 1");
track = 1;
break;
case 155 ... 280:
Serial.println("track 2");
track = 2;
break;
}
PaulS:
No. The problem is that you are using the map function instead of an if/else if/else structure. You should revise your code to only "map" a new value if the reading changes by more than some amount.
I've tried before something like this, but if I use an interval big enough to avoid the problem, if I move the pot very slow the value doesn't change even if I do all pot path.
groundfungus:
You could put some deadband between the values. 1023 / 7 selections is about 146 / selection. So you could use a switch case with ellipses to select the track and have a bit of deadband between select values.
I've tried before something like this, but if I use an interval big enough to avoid the problem, if I move the pot very slow the value doesn't change even if I do all pot path.
If you move the pot at the speed of a glacier, the current value eventually WILL exceed the last MAPPED value (not the last read value) by a certain amount. When it does, map again.