# Getting joystick input data to Arduino

Hi,

I have an android application which sends the joystick values from 0-1023 for X and Y axis separately. On the arduino front, I have two motors which take PWM inputs for speed from 0-255. I am not sure how to configure the inputs such that I get the direction and speed using the joystick values. I am trying to write the code as below, but would be helpful if someone can write a much easier version of the same code. The below code is still not complete. It only gives x and y value as 0-255 and drives forward or backward.

``````boolean mdrivedir;
boolean turndir; // 0 for left and 1 for right
unsigned long x = 0;
unsigned long y = 0;

if (x > 509){
turndir = 1;  // turn right
x = (x-509)/2;
}
else{
turndir = 0;  // turn left
x = (509-x)/2;
}

if (y > 509){
mdrivedir = 1;  // drive forward
y = (y-509)/2;
}
else{
mdrivedir = 0;  // drive reverse
y = (509-y)/2;
}

leftmotor(y, mdrivedir);
rightmotor(y, mdrivedir);
``````

Where did the magic number 509 come from ?

My advice would be to first write a complete program that simply receives the pot values and writes them to the Serial monitor. An extension to that would be to only print them if they change significantly.

Such a program will allow you to test your setup and will provide the basis for it to be extended to control the motors. How are the motors connected to the Arduino and what determines their direction ?

Crawl back up from codeworld. Describe (to your mom) the controls in front of you and how everything works. At the level of pull this lever and what happens?

And what he said above.

-jim lee

The code I have added is just a snippet of the entire code. I am looking for a logic which can help me run the motors.

1. If the joystick is in the center, android application sends x = 509 and y = 509. If it is moved up, y changes between 509 to 1023. If pulled down, if reduces from 509 to 0.
2. There are two motors connected to PWM pins of Arduino. I do not need the complete code. I was looking for some logic which can convert values x and y to control the motors.
3. If Y coordinate is greater than 509, robot is expected to drive forward, else backwards.
``````leftmotor(motorspeed, mdrivedir);
rightmotor(motorspeed, mdrivedir);
``````

Crawl back up from codeworld. Describe (to your mom) the controls in front of you

Thanks Jim Lee. I went to my mom and she really does not have a clue. Some other valid suggestion?. :o

praveen_khm:
Thanks Jim Lee. I went to my mom and she really does not have a clue. Some other valid suggestion?. :o

I think you missed Jim's point.

When you can explain it so your Mom understands then you will understand it also.

I think you need something like this

``````xPotVal = analogRead(xPotPin);
xPotVal = xPotVal - 512;
``````

This will convert the 0 to 1023 into -512 to + 511 giving you direction as well as speed.

...R

I guess I failed to understand his statement and also failed to explain my problem.

Here it is:

I have an Android application which has a joystick. The joystick sends multiple bytes and when extracted joystick values, I get X value between 0-1023 and Y value from 0-1023.
The joystick is always centered in the application and the center is 511 (though I have used 509, assuming 511 to get the center axis right).

The application on Arduino controls two motors. rightmotor(motorspeed, mdrivedir) and leftmotor(motorspeed, mdrivedir) functions control the direction and speed of both motors. I can get the motor direction right by using the logic : if(y > 511) drive front, else drive reverse.

Now, I need an algorithm which can convert the joystick values to motorspeed by giving exact values to left and right motors. I guess it goes like leftmotor = (Y+X/Y) * 255 and right motor is (Y+X/X) * 255. Sadly, it is not giving the exact values. So, my question is if someone has an algorithm or logic to set this right, please help.

Apologies if I am still not clear.

Hi,
You have a joystick that you want to control a motor FWD(0 to 508) and STOP(509 to 515) and REV(516 to 1023).

You have another joystick that you want to steer LEFT(0 to 508), AHEAD(509 to 515) and RIGHT(516 to 1023).

I think you forgot about steering straight and stopping.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png showing us how you are connecting the UNO with the motors, and supplying power?

Can you tell us your electronics, programming, Arduino, hardware experience?

Tom... praveen_khm:
Now, I need an algorithm which can convert the joystick values to motorspeed by giving exact values to left and right motors. I guess it goes like leftmotor = (Y+X/Y) * 255 and right motor is (Y+X/X) * 255. Sadly, it is not giving the exact values. So, my question is if someone has an algorithm or logic to set this right, please help.

I thought that is what I included in Reply #4

The most likely reason why your calculations are not giving the correct value is because the integer values are overflowing or underflowing.

...R