I am in the process of making a simple project, part of which involves the user moving a joystick and the program finding the angle the joystick is being held at, from 0 to 360 degrees. The calculations I have a handle on, but the problem is that my joystick isn't cooperating with me.
I tried to recreate the issue in the image below. When I move the stick straight down, it correctly reads the X and Y position. However, it outputs the same X and Y values for a pretty big rotational range. The same thing happens centered around the up, left, and right directions as well. The joystick doesn't output a lot of intermediate values.
I don't know the exact part number for my joystick, its the one that comes in Elegoo's Most Complete Mega 2560 Starter Kit, and looks to be the same as this one.
So I guess my question is, is this a hardware issue and, if so, what joystick could I buy which would be better at reading the actual stick position?
and the code, if needed. This doesn't include the rest of the project, the bit where I calculate angles etc etc. I'm just trying to get the position values correct first.
int VRx=A0;
int VRy=A1;
int SW=13;
int xPos=0;
int yPos=0;
int SW_State=0;
int mapX=0;
int mapY=0;
void setup() {
Serial.begin(9600);
pinMode(VRx,INPUT);
pinMode(VRy,INPUT);
pinMode(SW, INPUT_PULLUP);
}
void loop() {
xPos = analogRead(VRx);
yPos = analogRead(VRy);
SW_State = digitalRead(SW);
Serial.print("X: ");
Serial.print(xPos);
Serial.print(" | Y: ");
Serial.println(yPos);
delay(1000);
}
I just tried it, it does the same thing, though I was a but inaccurate describing the problem initially. If I'm wiggling the joystick around the down position, the Y value changes alright, but the X value barely changes at all, even when I'm approaching 40ish degrees off of the down position. It wavers between 0 and 2. But then, if I get close to the right position, the Y value gets "stuck" at its max value. If I get close to the left position, the Y value gets "stuck" at its min value. If I get close to the up position, the X value gets "stuck" at its max value.
That looks like a hardware issue to me. Do you have another joystick to try?
Also to get the angle the equation is angle =inverse sin (y/x)
inverse sin is sin ^-1 on your calculator or asin() when using math.h standard C library.
You will probably have to scale the x,y values.
I do not have another. Looking to buy a second one if this is a hardware issue, since all my parts are real cheap and I'd imagine a better joystick would work better. And yes, I have code set up to map the joystick values so the neutral position corresponds to (0,0) and find the angle from there, but I'm taking this one step at a time. :]
I have only one potentiometer, and it is being used. My project will eventually necessitate an LCD and 2 buttons. My last project also used an LCD screen and 2 buttons, so I never took them off the breadboard. The picture is cluttered, but I have checked my wiring. Don't believe that's the issue, since the joystick is, generally speaking, working correctly.
There is no break there. I know there is on some models, but not this one. Just to be sure, I moved my black and red wires leading from the Mega's GND and 5V pins to the same side of the board that the joystick is connected to. No change