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:
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 ) 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)
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'
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..