# Controlling dual dc motors with an X&Y joystick module

Currently I’m working on this personal hoverboat project and all I want is to control my motors via joystick. I’m remotely controlling the motors via nrf24l01 transreceivers.

But I failed creating a properly working code that lets me drive the motors according to joystick feedback.

I’m interested in realtime feedback. The craft should tell motors how much to spin to make itself move forward, backwards, left and right according to joystick feedback.

Someone must have done a similar project somewhere, I looked for it but couldn’t find any. Perhaps someone can guide me here?

Here’s my craft. Hope it gives someone idea of what I’m after: http://imgur.com/h0W7K4s

UPDATE:

OK I did manage to drive my boat forward and backward but I can’t make it turn. Here’s my code so far:

``````if (joystikY > 550) { //FORWARD
value1 = map(joystikY,600,1023,0,255);
value1 = constrain(value1,0,255);

analogWrite(leftforward,value1);
analogWrite(rightforward,value1);

if (joystikY < 500) { //BACKWARD
value2 = map(joystikY,550,0,0,255);
value2 = constrain(value2,0,255);
analogWrite(leftbackward,value2);
analogWrite(rightbackward,value2);
}
if ((joystikY >=500) && (joystikY <=600)) { //FULL STOP
analogWrite(rightforward,0);
analogWrite(rightbackward,0);
analogWrite(leftforward,0);
analogWrite(leftbackward,0);
}
``````

So you're driving something like a tank? Forwards on the right motor and back on the left motor is a left spin? No rudder or steering vanes?

Rename your "value 1" variable to something more meaningful, like "speed". And you don't need value2 as that can also use "speed".

Then think about adding a "turn" modifier to the speed. If you have the joystick forwards and a little to the left, the speed should be forwards, but you subtract a little from the left motor and add a little to the right motor.

`````` turn = map(joystikX, 0, 1023, -128, 128);  //assumes stick-left gives higher analog numbers
analogWrite(leftforward,speed - turn);
analogWrite(rightforward,speed + turn);
``````

Of course this is not the full code as you will need to constrain the values to 0-255 and I don't fully understand how you are driving your motors backwards.

MorganS: So you're driving something like a tank? Forwards on the right motor and back on the left motor is a left spin? No rudder or steering vanes?

Rename your "value 1" variable to something more meaningful, like "speed". And you don't need value2 as that can also use "speed".

Then think about adding a "turn" modifier to the speed. If you have the joystick forwards and a little to the left, the speed should be forwards, but you subtract a little from the left motor and add a little to the right motor.

`````` turn = map(joystikX, 0, 1023, -128, 128);  //assumes stick-left gives higher analog numbers
analogWrite(leftforward,speed - turn);
analogWrite(rightforward,speed + turn);
``````

Of course this is not the full code as you will need to constrain the values to 0-255 and I don't fully understand how you are driving your motors backwards.

I'm using a L298N motor controller board so I have 4 pins to control my motors. INA and INB controls motor one, INC and IND controls motor 2. One is forward other is backwards respectively.

So I couldn't know how to insert your code into my project.

Calculate the speed, which can be negative for backwards.

Then add the turn modifier, which will give different speeds for left and right motors.

Then apply those speeds to the motors, noting that if the speed is negative, you need to drive the appropriate pins for backwards.

Simple? The turn modifier may even be itself modified by the speed, as you usually don't want to do a full-rate turn when travelling at high speed.

I went with this code since it encorporates what you have instructed.

But this time STRL and STRR values are out of charts. Left and right 360 degree turns are not working and there are throttling issues when running both motors forward at full speed.