Joystick degrees

Hello all!
I recently tried to use a joystick in my project, but its not working so good...
My problem is that I want to see the angle of the joystick, but it only gives me X Y position.
Dont tell me about atan() or atan2() cuz its working only in a part of the range, but I need it 360.
So if there any other way- tell me please!! and if not- just tell me how to do it with the trigonometry..
Thanks For Any Help!! ;D

Arad.

Dont tell me about atan() or atan2() ... just tell me how to do it with the trigonometry

atan or atan2 are trigonometry. Make your mind up.

Pete

el_supremo,
haha, I meant dont tell me to use atan() or atan2(), unless there IS a way to do it... but not ONLY atan(), cuz its not working.. :confused:
xD anything you know about it??

atan2(y,x) (NOTE the order of the x and y here) returns the value of the angle in radians from -pi to pi (which in degrees is -180 to 180). To convert that to 0-360 you convert radians to degrees and then just add 180. The main thing you have to concern yourself with is how that relates to the way that you have oriented the joystick. But that is a simple bit of math.

BTW. Post your code (in code tags).
Pete

P.S. You could use atan instead of atan2 but atan only returns an angle in the range -pi/2 to pi/2 (-90 to 90 degrees) in which case you have to figure out (from the signs of x and y) which quadrant the angle lies in and then correct it yourself. atan2 does that bit for you.

Pete

thats my code:

#include <math.h>

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

int xpos, ypos;

void loop() {
 xpos = analogRead(4);
 ypos = analogRead(5);
 Serial.println(atan2(xpos, ypos));
}

simple as that... and I know that its in radians.
I can just do: degrees = radians * 180 / PI;
should it work?

Arad.

aradarbel10:
I recently tried to use a joystick in my project, but its not working so good...
My problem is that I want to see the angle of the joystick, but it only gives me X Y position.
[....] cuz its working only in a part of the range, but I need it 360.

Reading the joystick using analogRead() will give values between 0 and 1023. You can use the map() function to change that range into any other range that you want. If you want to change it to 0 to 360 you can do so without any need for atan() etc.

...R

Robin2:
Reading the joystick using analogRead() will give values between 0 and 1023. You can use the map() function to change that range into any other range that you want. If you want to change it to 0 to 360 you can do so without any need for atan() etc.

...R

I know I can use map, but its not gonna give me the angle.

 xpos = analogRead(4);
 ypos = analogRead(5);
 Serial.println(atan2(xpos, ypos));

The first problem is that analogRead only returns values from 0 to 1023 so atan2 will think that your values are always in the positive quadrant.
Second you have the x and y values the wrong way round for atan2 as I pointed out right at the beginning of message #3.

If your joystick is in its centre position, it probably will give x,y readings of around 511,511 or 512,512. In that case you can convert them to the range -512 to 511 by just subtracting 512 from x and y.

xpos = analogRead(4) - 512;
 ypos = analogRead(5) - 512;
 Serial.println(atan2(ypos, xpos));

Pete

aradarbel10:
I know I can use map, but its not gonna give me the angle.

What angle do you want?

How else are you going to convert 0-1023 to an angle?

If you mean the angle to which you pull the joystick lever you could measure the angle with a protractor and at the same time note the analogRead() value. Then you could make a conversion table. However I have no idea why you would want the angle of a joystick lever.

And I can't recall seeing any joystick that could work through 360 degrees.

...R

Hi

Can you tell us that output you need .

Output degrees for joystick in the centre.
Output degrees for X joystick fully left and Y centre (-X,Y) (0, 512)
Output degrees for Y joystick fully right and X centre (+X, +Y) (1023,512)

Also tan and other trig functions work in Radians, not Degrees.

Tom... :slight_smile:

I think you want to do a rectangular to polar conversion. That is textbook stuff and hasn't been much of a mystery since many centuries ago.