Arduino joystick - wrong values

Thread Starter

boaz888

Joined Apr 16, 2024
1
Hey! starting my journy on Arduinio.When i tried to connect my joystick, and read the values from it,I gaet the whole range on X.

My problem is with Y: Resting pposition indicates 0, and if i move the joystick down it shows the value 512. if I move it upwards, the value does not change.
where is my problem? adding my code:




int xpin = A2;
int ypin = A1;
int xval;
int yval=512;
void setup() {

Serial.begin(9600);

pinMode(xpin , INPUT);
pinMode(ypin , OUTPUT);
}

void loop() {
xval=analogRead(xpin);
yval = analogRead(ypin);

delay(200);
Serial.print("X: ");
Serial.print(xval);
Serial.print(" y: ");
Serial.println(yval);
 
Last edited by a moderator:

dendad

Joined Feb 20, 2016
4,494
I note you defined yval = 512 but gave no value to xval. Maybe not important but consistency is not a bad thing.
You also have defined the pin modes differently. For analog read, I do not think you have to define the pin mode. Particularly, not "OUTPUT" ! That will muck up the analogRead .
Can you post your circuit?
If fixing your code, removing the "pinMode" lines, does not fix it, try swaping the wipers of the pots over to see if the readings change to show if it is a hardware fault or not.
 

dendad

Joined Feb 20, 2016
4,494
Oh, when you post some code, put open square bracket CODE close square bracket in front and open square bracket /CODE close square bracket at the end.

Try this. I think it will work. I've not programmed it as it is very late here and my Arduinos are in the shed, but I think it will be ok.

Code:
#define xpin A2   // use a #define in stead of an int as it saves memory
#define ypin A1
int xval;              // no predefined value is needed
int yval;

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

void loop() {
xval = analogRead(xpin);
yval = analogRead(ypin);
delay(200);
Serial.print("X: ");
Serial.println(xval);       // println will do a CR/LF after the print.
Serial.print(" y: ");
Serial.println(yval);
}
Arduinos are very much fun to play with. So enjoy!
I'm off to bed now so it will be interesting to see if you have replied in the morning.
 
Last edited:

Jerry-Hat-Trick

Joined Aug 31, 2022
578
You haven't mentioned the joystick you are using, but if it consists of two potentiometers, one for X and one for Y there are probably four connections to it. One will be for ground to be connected to the GND pin of the Arduino and one will be for connection to the positive rail. For an Arduino UNO that will be the 5V pin, some other boards will expect to see no more than 3.3V on the analogRead() pins so the connection should be to the 3.3V pin. It is easy to blow an input pin if you turn the pot to the maximum 5.0V when the input pin can accept a maximum of 3.3V. You are probably aware of this, but I just thought I'd mention it.

Also, you have made the Y axis an output when I think it should be an input, like the X axis? Dendad's code looks good. If you want the values of X and Y on the same line you can drop the "ln" in his line 15, actually as you had it. You don't need to state pinModes as they default to inputs.
 
Top