Hi all, I was wondering if it is possible to "restrict" the readings from an analog joystick (2 axis pot) to a "circle" insted of a full 1023x1023 square.
Simple answer is yes. Just use trigonometry on the 0-1023 values returned from analogRead to workout the joystick angle and from this you can substitute the value you would get for inscribing a circle.
int reading = analogRead(A0); // map the reading from the analog input range to the output range: reading = map(reading, 0, 1023, 0, 180);
Should I “remap” this value again with a formula?
Do you understand what trigonometry means?
Yes, I'll work on it with effort and post the resulting code
Here is one approach:
long x = analogRead(A0) - 512; long y = analogRead(A1) - 512; float angle = atan2( y, x ); // in radians, zero is joystick move to right float magnitude = sqrt( x*x + y*y); // Pythagoras if( magnitude > 512) magnitude = 512; x = magnitude * cos( angle ); y = magnitude * sin( angle );
The above code assumes the x and y readings are 512 when the joystick is in its middle position. You would need to check the actual readings for your joystick and edit the code accordingly (including the "if" statement).
Note the small joysticks usually used with Arduinos normally have a 'dead' zone near the middle so you need to move the joystick some way from the middle before readings start to change. They also normally have a similar 'dead' zone near full travel.
Would the above code by Archibald be able to calculate the x and y positions of the joystick if juggled about if an angle was the input. Rather than using analog read, replacing these lines with xpos and ypos.
For example 360 degrees. Heading used as angle in equation? What would the code be please this is beyond my comprehension ??
Google "polar to rectangular coordinate transform"