Erratic Potentiometer Readings with joystick library

Hi all, I am new to the world of coding and working with Arduinos.

I am trying to use the Joystick Library to control a custom Joystick with one axis and 4 buttons. I have managed to follow a tutorial and have got the code to compile without any errors and upload to the a Leonardo board.

The problem I am having is that the readings coming from the Potentiometer is random and erratic without the any input being made. I have read several post with several suggestions ranging from double reading the axis to using 5k pots but I can't seem to find a solution to this.

Below is the code from the void loop relating to the axis:

rzAxis_ = analogRead(joyRZ);
rzAxis_ = map(rzAxis_,0,1023,0,255);
Joystick.setRzAxis (rzAxis_);

Any help will be greatly appreciated, thanks

Hi!

joystick I use give me a floating value at rest around 512 wich is the middle way between 0 (min value when joystick is pressed up or down) to 1023 (max value when joystick is pressed down or up).

the value at rest is never exactly 512 and can moove from 10 point roughly.

Please try this code:

Serial.println(analogRead(joyRZ)); // assuming joyRZ is the correct pin
delay(50);

it must display raw data from your joystick, without the need of a library, so you can observe the "natural" behavior of your peripheral.

very usefull for application when more stability is required. You may also sum then mean several values to reach better accuracy, but keep it for later.

do you mean add a resistor to the joystick? If not, what is the value of your pot? 10kOhm? it shouldn't be related to the output results: ranges from 0 to 1023 with a center value around 512, whatever a 10k or 5k pot.

EDIT: oh, and of course (first things first :smile:) could you check your wiring? The joystick must have 3 pins: probably + power and GND on each side, and the middle pin is the output pin. Power it through the +5V pin from your board, don't forget the GND.

EDIT EDIT: if needed, a simple multimeter may confirm your joystick is ok. A readings of the resistor between + and the output pin PLUS between output pin and GND should give you the total value of the joystick (like 10kOhm for example)

Thanks for replying so quicky,

I have just checked the wiring to make sure all is okay and all 3 wires are connected to the correct positions on the Leonardo.

A post suggested using 5kOhm pots not 10kOhm ones which made no difference as you had suggested.

Sorry for coming across as exceedingly dull here but I am still trying to get used to all this. At what point should I use the code

Serial.println(analogRead(joyRZ));
delay(50);

Thanks for your help

No problem.

here an easy sketch you may use immediately. Its purpose is only to read the joystick in a "raw" way.

const int JOYSTICK_Pin = X // put here the correct pin number (the central pin of your pot, normaly carrying the variable value)

Setup(){
pinMode(JOYSTICK_Pin , INPUT);
Serial.begin(9600);
Serial.print("First measure: ");
Serial.println(analogRead(JOYSTICK_Pin);
Serial.print("loop measures: ");
}

Loop(){
Serial.println(analogRead(JOYSTICK_Pin));
delay(50);
}

Will you take a try and report here the results displayed on serial monitor?
Note I have no experience with the leonardo, so I considered it as a "regular" arduino board like UNO or nano.

As said, I am expecting values near 512 at rest, then 0 or 1023 when pushed at max (up or down) and all other intermediate values for intermediate positions.

Thanks for replying, the sketch is returning an error, sorry

S_Stock_TBC:3:1: error: expected ',' or ';' before 'Setup'
 Setup(){
 ^~~~~
C:\Users\cr361\Desktop\S_Stock_TBC\S_Stock_TBC.ino:11:6: warning: ISO C++ forbids declaration of 'Loop' with no type [-fpermissive]
 Loop(){
      ^
exit status 1
expected ',' or ';' before 'Setup'

Spelling.
"void loop", "void setup"

I have made the change to the sketch and am getting

collect2.exe: error: ld returned 1 exit status

I really appreciate your efforts to help but I'm beginning to think that we could end up going round for hours as I am not very helpful sorry

EDIT:
I have just set a 50 ms delay after the

Joystick.setRzAxis (rzAxis_);

line and it seems to have solved the jumping issue. thank you for your help

That is good news :+1:

delay(50); should not be needed for a pot, delay(1); maybe.
But that delay could interfere with the rest of the program.
Pot problems could come from wiring shared with other devices, like a breadboard.
The analogue value should not jump more than one digit,
which can be eleminated if you need to convert to a byte at the end.
Try this sketch.
Leo..

int rawValue, oldValue;
byte potValue;

void setup() {
  Serial.begin(115200);
}

void loop() {
  rawValue = analogRead(A0); // read
  if (abs(rawValue - oldValue) >= 4) { // hysteresis
    oldValue = rawValue; // remember the change
    potValue = oldValue >> 2; // 10-bit to 8-bit
    Serial.println(potValue); // print changes
  }
}

Telling, without showing, isn't helpful.

For some reason without the delay the jump is more than 50% of the range in some cases but it does not jump at all with the delay.

And with reference to Semperidem last post

Is in reference to the void being omitted from the loop and set up, which is is why I stated I made “the” change

Thank you all for the help

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