I have a problem, I found its causes, I have a workaround but I would like to understand what happens !
I need to use multiple potentiometers with analogRead on an Arduino Nano (ATmega328P old bootloader). I had mysterious interferences although my potentiometers are totally separated.
When I use this code (see attachment):
//analogRead(A0); p1=analogRead(A1); p2=analogRead(A2); Serial.print(p1); Serial.print(" "); Serial.println(p2);
Then if I move the potentiometer A2, the A1 value varies. The A1 value is not the same than the A2 value, but there’s clearly an interference, which doesn’t seems normal to me as of course I would like my potentiometers to be independent.
If I use this code (see attachment):
analogRead(A0); p1=analogRead(A1); p2=analogRead(A2); Serial.print(p1); Serial.print(" "); Serial.println(p2);
Then everything works perfect, my two potentiometers are independent.
The only difference is that I have called analogRead(A0) before calling analogRead(A1), even though I don’t care about A0.
Can somebody tell me why it makes a difference?
I’ll tell you some more about the circumstances, but I think it doesn’t change anything to my question…
Of course, my original program was way more complex, I have had a very hard time isolating the origin of this interference. I’m actually working on a robot and I use two physical joysticks so I need to call analogRead on A0 and A1 for the first joystick and A2 and A3 for the second joystick. I noticed than when I called analogRead(A1) before analogRead(A0), then I had an interference between A1 and A3, although they are connected to different joysticks.
All my potentiometers are connected to 5V, GND and an analog in. I’m pretty confident with my setup, especially as my program works perfectly as long as I call analogRead(A0) before analogRead(A1).
this_does_not_work.ino (216 Bytes)
this_works.ino (214 Bytes)