motor control with joystick!

hi im trying to accomplish control over two motors on each axis of the joystick forward and backward on x axis for motor a A, and forward and backward on Y axis on motor b , using l298n module with arduino , if anyone knows a code or how to write plz let me know :slight_smile:

Have you looked for tutorials on each component?
That is usually the way I approach a project.
Find a tutorial on the joysticks.
Wire it up, get your code to compile, see the results printed to the monitor as you move the joysticks.

Then a new sketch. Wire up the motor driver. Code from the tutorial. Can you make the motors run?

Then combine the sketches.

We will gladly help you with any bumps you run into along the way.

Have you purchased the hardware already? What did you get?

Edit: I did some googling for you.

https://tronixlabs.com.au/news/tutorial-l298n-dual-motor-controller-module-2a-and-arduino/

hi yes i have bought arduino l298n motor driver torque motors and joystick module i have a set up and all hooked up , i can only get one motor to go forward and backward on the y axis i need two motors to work on one joystick using x axis for second motor im using for exoskeleton project , essentially im turning torque motors into big servos lol

Is the other motor busted?

If you show us the code that works with one motor but not two and a schematic of how you have everything wired up we could probably help . But just guessing what you're doing wrong isn't very productive.

First thing to check, how are you supplying power to the motors? If it's through the Arduino then that's almost certainly your problem.

Steve

You should have two readings from the joystick, one from the x axis and one from the y axis.
Ignore the motor driver/motor part for a moment and make sure this is working correctly i.e. just create a sketch that reads the 2 input pins and prints the X and Y values to the serial monitor. Then move the joystick and confirm that the values are changing independently and as expected.

You will likely then want to map the input value (e.g. 0 to 1023) to a direction and output value (e.g. 0 to 255 if PWM, or just LOW or HIGH). Again, you can print these outputs to the serial monitor to check they are as expected.

holosound:
...i can only get one motor to go forward and backward on the y axis...

If you've done this then you must already be reading something from the joystick and enabling the motor based on that input. So just make sure that you've mapped each separate input to a separate output.

If you've already done all that and only one motor is working then there's probably a problem with your wiring of the second motor and any related wiring between the driver and arduino, or the code related to the x axis input or second motor output.

But as already said in other replies, it would be a lot easier if you show what you've done so far.

so this code is one ive adopted through a tutorial on youtube, im very new to programming and not very familier with code but im determined to learn this code is one im trying to modify to get the control i want , this is also the code that allows one motor to go back and forth on one axis , i just want two motors to do this, i can get both motors to work simultaneously but not both back and forth on each axis

/*
L298N Motor Control Demonstration with Joystick
L298N-Motor-Control-Demo-Joystick.ino
Demonstrates use of Joystick control with Arduino and L298N Motor Controller

DroneBot Workshop 2017

*/

// Motor A

int enA = 9;
int in1 = 8;
int in2 = 7;

// Motor B

int enB = 3;
int in3 = 5;
int in4 = 4;

// Joystick Input

int joyVert = A0; // Vertical
int joyHorz = A1; // Horizontal

// Motor Speed Values - Start at zero

int MotorSpeed1 = 0;
int MotorSpeed2 = 0;

// Joystick Values - Start at 512 (middle position)

int joyposVert = 512;
int joyposHorz = 512;

void setup()

{

// Set all the motor control pins to outputs

pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

// Start with motors disabled and direction forward

// Motor A

digitalWrite(enA, LOW);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

// Motor B

digitalWrite(enB, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

}

void loop() {

// Read the Joystick X and Y positions

joyposVert = analogRead(joyVert);
joyposHorz = analogRead(joyHorz);

// Determine if this is a forward or backward motion
// Do this by reading the Verticle Value
// Apply results to MotorSpeed and to Direction

if (joyposVert < 460)
{
// This is Backward

// Set Motor A backward

digitalWrite(in1, LOW);
digitalWrite(in2,LOW);

// Set Motor B backward

digitalWrite(in3, HIGH);
digitalWrite(in4,LOW );

//Determine Motor Speeds

// As we are going backwards we need to reverse readings

joyposVert = joyposVert - 460; // This produces a negative number
joyposVert = joyposVert * -1; // Make the number positive

MotorSpeed1 = map(joyposVert, 0, 460, 0, 255);
MotorSpeed2 = map(joyposVert, 0, 460, 0, 255);

}
else if (joyposVert > 564)
{
// This is Forward

// Set Motor A forward

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);

// Set Motor B forward

digitalWrite(in3,LOW);
digitalWrite(in4,LOW );

//Determine Motor Speeds

MotorSpeed1 = map(joyposVert, 564, 1023, 0, 255);
MotorSpeed2 = map(joyposVert, 564, 1023, 0, 255);

}
else
{
// This is Stopped

MotorSpeed1 = 0;
MotorSpeed2 = 0;

}

// Now do the steering
// The Horizontal position will "weigh" the motor speed
// Values for each motor

if (joyposHorz < 460)
{
// Move Left

// As we are going left we need to reverse readings

joyposHorz = joyposHorz - 460; // This produces a negative number
joyposHorz = joyposHorz * -1; // Make the number positive

// Map the number to a value of 255 maximum

joyposHorz = map(joyposHorz, 0, 460, 0, 255);

MotorSpeed1 = MotorSpeed1 - joyposHorz;
MotorSpeed2 = MotorSpeed2 + joyposHorz;

// Don't exceed range of 0-255 for motor speeds

if (MotorSpeed1 < 0)MotorSpeed1 = 0;
if (MotorSpeed2 > 255)MotorSpeed2 = 300;

}
else if (joyposHorz > 564)
{
// Move Right

// Map the number to a value of 255 maximum

joyposHorz = map(joyposHorz, 564, 1023, 0, 255);

MotorSpeed1 = MotorSpeed1 + joyposHorz;
MotorSpeed2 = MotorSpeed2 - joyposHorz;

// Don't exceed range of 0-255 for motor speeds

if (MotorSpeed1 > 255)MotorSpeed1 = 300;
if (MotorSpeed2 < 0)MotorSpeed2 = 0;

}

// Adjust to prevent "buzzing" at very low speed

if (MotorSpeed1 < 8)MotorSpeed1 = 0;
if (MotorSpeed2 < 8)MotorSpeed2 = 0;

// Set the motor speeds

analogWrite(enA, MotorSpeed1);
analogWrite(enB, MotorSpeed2);

}

holosound:
im trying to accomplish control over two motors

I think you need to explain what you mean by 'control'.

The code you posted could be used to control a 2 wheeled vehicle. The joystick Y axis controls velocity (speed, forward/backward) and the joystick X axis modifies the speed of each motor (increases one, decreases the other) to cause the vehicle to turn.

But based on this comment...

holosound:
i can get both motors to work simultaneously but not both back and forth on each axis

...is this actually what you want?

If you want completely independent movement of each motor based on the 2 inputs coming from the joystick, then this code will not do it - in fact the required code would be much simpler.

yea i figured so xD i rly would love to know the code to be able to do it , the code i posted was one i adopted (i dnt know how to write code yet lol)from a tutorial for wired car to test the module , im building an exoskeleton and i want to be able to move both motors on each axis if u could post a code to do that i would be eternally grateful!

// Motor A

int enA = 9;
int in1 = 8;
int in2 = 7;

// Motor B

int enB = 3;
int in3 = 5;
int in4 = 4;

// Joystick Input

int joyVert = A0; // Vertical
int joyHorz = A1; // Horizontal

// Motor Speed Values - Start at zero

int MotorSpeed1 = 0;
int MotorSpeed2 = 0;

// Joystick Values - Start at 512 (middle position)

int joyposVert = 512;
int joyposHorz = 512;

void setup()

{

// Set all the motor control pins to outputs

pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

// Start with motors disabled and direction forward

// Motor A

digitalWrite(enA, LOW);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

// Motor B

digitalWrite(enB, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

}

void loop() {

// Read the Joystick X and Y positions

joyposVert = analogRead(joyVert);
joyposHorz = analogRead(joyHorz);

// Determine if this is a forward or backward motion
// Do this by reading the Verticle Value
// Apply results to MotorSpeed and to Direction

if (joyposVert < 460)
{
// This is Backward

// Set Motor A backward

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);

//Determine Motor Speeds

// As we are going backwards we need to reverse readings

joyposVert = joyposVert - 460; // This produces a negative number
joyposVert = joyposVert * -1; // Make the number positive

MotorSpeed1 = map(joyposVert, 0, 460, 0, 255);
MotorSpeed2 = map(joyposVert, 0, 460, 0, 255);

}
else if (joyposVert > 564)
{
// This is Forward

// Set Motor A forward

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

//Determine Motor Speeds

MotorSpeed1 = map(joyposHorz, 564, 1023, 0, 255);
MotorSpeed2 = map(joyposHorz, 564, 1023, 0, 255);
}
{ if (joyposHorz < 460)
{
// This is Backward

// Set Motor B backward

digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

//Determine Motor Speeds

// As we are going backwards we need to reverse readings

joyposHorz = joyposHorz - 460; // This produces a negative number
joyposHorz = joyposHorz * -1; // Make the number positive

MotorSpeed1 = map(joyposHorz, 0, 460, 0, 255);
MotorSpeed2 = map(joyposHorz, 0, 460, 0, 255);

}
else if (joyposHorz > 564)
{

// Set Motor B forward

digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

//Determine Motor Speeds

MotorSpeed1 = map(joyposHorz, 564, 1023, 0, 255);
MotorSpeed2 = map(joyposHorz, 564, 1023, 0, 255);

}
else
{
// This is Stopped

MotorSpeed1 = 0;
MotorSpeed2 = 0;
}
}
}

this is the code im working with now ,but i get no movement from either motor , i thought this would work lol any know what im doing wrong?

It will be much easier if you create this one piece at a time.

Ignore the motors for now, and just get to the point where you can print the values of each joystick axis to the serial monitor.
Moving the stick in one axis should affect only one of the outputs.
Moving the stick in the other axis should affect only the other output.
If you try this you would probably come to realise that something is wrong in the above sketch - you have not declared joyHorz and joyVert as INPUTS in the setup() routine.

Then, separately, write a sketch that will make the motors run (e.g. forward for 1 sec, stop for 1 second, backward for 2 sec etc) just to demonstrate that everything is working.
Once you can do both things, you can combine them to make the motors run based on the (mapped) outputs from the joystick.

And the main loop for motorA will look something like...

  1. Read the vertical position of the stick
  2. Map to acceptable input (i.e. 0 to 255) plus a direction
  3. Set in1/in2 depending on the direction you want
  4. Set enA depending on the speed you want

Do it for a single motor to start with. When that works, and only when that works, do the same thing for the second stick and motor.
The code above has the right sort of idea, but

  1. you should not be setting both motor speeds based on a single stick, only one
  2. it never actually uses the motor speeds that you have calculated. enA and enB will need to be set to the desired motor speed.

Also please use code tags when posting code. How to use the forum.

thank you for the info :slight_smile: so ived changed some things up and it worls but bareley lol the motors turn sometimes and arent complying

// Motor A

int enA = 9;
int in1 = 8;
int in2 = 7;

// Motor B

int enB = 3;
int in3 = 5;
int in4 = 4;

// Joystick Input

int joyVert = A0; // Vertical
int joyHorz = A1; // Horizontal

// Motor Speed Values - Start at zero

int MotorSpeed1 = 0;
int MotorSpeed2 = 0;

// Joystick Values - Start at 512 (middle position)

int joyposVert = 512;
int joyposHorz = 512;

void setup()

{

// Set all the motor control pins to outputs

pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

// Start with motors disabled and direction forward

// Motor A

digitalWrite(enA, LOW);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

// Motor B

digitalWrite(enB, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

}

void loop() {

// Read the Joystick X and Y positions

joyposVert = analogRead(joyVert);
joyposHorz = analogRead(joyHorz);

analogWrite(enA,MotorSpeed1);
analogWrite(enB,MotorSpeed2);
// Determine if this is a forward or backward motion
// Do this by reading the Verticle Value
// Apply results to MotorSpeed and to Direction

if (joyposVert < 460)
{
// This is Backward

// Set Motor A backward

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);

//Determine Motor Speeds

// As we are going backwards we need to reverse readings

joyposVert = joyposVert - 460; // This produces a negative number
joyposVert = joyposVert * -1; // Make the number positive

MotorSpeed1 = map(joyposVert, 0, 460, 0, 255);

}
else if (joyposVert > 564)
{
// This is Forward

// Set Motor A forward

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

//Determine Motor Speeds

MotorSpeed1 = map(joyposHorz, 564, 1023, 0, 255);

}
{ if (joyposHorz < 460)
{
// This is Backward

// Set Motor B backward

digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

//Determine Motor Speeds

// As we are going backwards we need to reverse readings

joyposHorz = joyposHorz - 460; // This produces a negative number
joyposHorz = joyposHorz * -1; // Make the number positive

MotorSpeed2 = map(joyposHorz, 0, 460, 0, 255);

}
else if (joyposHorz > 564)
{

// Set Motor B forward

digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

//Determine Motor Speeds

MotorSpeed2 = map(joyposHorz, 564, 1023, 0, 255);

}
else
{
// This is Stopped

MotorSpeed1 = 0;
MotorSpeed2 = 0;

}
}
}

now i can get 1 motor to move like before but i cant get y axis movement

// Motor A

int enA = 9;
int in1 = 8;
int in2 = 7;

// Motor B

int enB = 3;
int in3 = 5;
int in4 = 4;

// Joystick Input

int joyVert = A0; // Vertical
int joyHorz = A1; // Horizontal

// Motor Speed Values - Start at zero

int MotorSpeed1 = 0;
int MotorSpeed2 = 0;

// Joystick Values - Start at 512 (middle position)

int joyposVert = 512;
int joyposHorz = 512;

void setup()

{

// Set all the motor control pins to outputs

pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

// Start with motors disabled and direction forward

// Motor A

digitalWrite(enA, LOW);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

// Motor B

digitalWrite(enB, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

}

void loop() {

// Read the Joystick X and Y positions

joyposVert = analogRead(joyVert);
joyposHorz = analogRead(joyHorz);

// Determine if this is a forward or backward motion
// Do this by reading the Verticle Value
// Apply results to MotorSpeed and to Direction

if (joyposVert < 460)
{
// This is Backward

// Set Motor A backward

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);

//Determine Motor Speeds

// As we are going backwards we need to reverse readings

joyposVert = joyposVert - 460; // This produces a negative number
joyposVert = joyposVert * -1; // Make the number positive

MotorSpeed1 = map(joyposVert, 0, 460, 0, 255);

analogWrite(enA, MotorSpeed1);

}
else if (joyposVert > 564)
{
// This is Forward

// Set Motor A forward

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);

//Determine Motor Speeds

MotorSpeed1 = map(joyposVert, 564, 1023, 0, 255);

analogWrite(enA, MotorSpeed1);
}
{ if (joyposHorz < 460)
{
// This is Backward

// Set Motor B backward

digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

//Determine Motor Speeds

// As we are going backwards we need to reverse readings

joyposHorz = joyposHorz - 460; // This produces a negative number
joyposHorz = joyposHorz * -1; // Make the number positive

MotorSpeed2 = map(joyposHorz, 0, 460, 0, 255);

analogWrite(enB, MotorSpeed2);
}
else if (joyposHorz > 564)
{

// Set Motor B forward

digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

//Determine Motor Speeds

MotorSpeed2 = map(joyposHorz, 564, 1023, 0, 255);

analogWrite(enB, MotorSpeed2);

}
else
{
// This is Stopped

MotorSpeed1 = 0;
MotorSpeed2 = 0;

}
{
// here analog write was

}
}
}

Post your code in code tags - see the link from my last post. Like this...

thisIsCode();

Motor A will not move because you have changed

// Set Motor A backward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);

To

// Set Motor A backward
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);

Change it back. And the same for the forward motion.

Your latest code (and the one before) is an improvement.

But this description is not very helpful.

holosound:
...the motors turn sometimes and arent complying

When do they turn? What does 'aren't complying' mean in terms of actions and observed effects?

It will be much easier for you to start to see where things are or are not working if you print some values to the Serial monitor, for example, the input values you are reading from the joysticks, and the output values you are using to control motor speed.
If for example you can see that the values from the joysticks are not as expected then you can ignore everything else and work on fixing that.
At the moment you have no idea where the problem is. You need to work it out. Work through the steps of you program and make sure it is acting as you expect - again, the simplest way is just to add some serial print statements so you can see what's happening on the serial monitor.

so this code now works but only one motor on the horizontal axis or x

// Motor A

int enB = 10;
int in1 = 8;
int in2 = 7;

// Motor B

int enA = 3;
int in3 = 5;
int in4 = 4;

// Joystick Input

int joyVert = A0; // Vertical
int joyHorz = A1; // Horizontal

// Motor Speed Values - Start at zero

int MotorSpeed1 = 0;
int MotorSpeed2 = 0;

// Joystick Values - Start at 512 (middle position)

int joyposVert = 512;
int joyposHorz = 512;

void setup()

{

// Set all the motor control pins to outputs

pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

// Start with motors disabled and direction forward

// Motor A

digitalWrite(enA, LOW);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

// Motor B

digitalWrite(enB, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

}

void loop() {

// Read the Joystick X and Y positions

joyposVert = analogRead(joyVert);
joyposHorz = analogRead(joyHorz);

// Determine if this is a forward or backward motion
// Do this by reading the Verticle Value
// Apply results to MotorSpeed and to Direction

if (joyposVert < 460)
{
// This is Backward

// Set Motor A backward

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);

//Determine Motor Speeds

// As we are going backwards we need to reverse readings

joyposVert = joyposVert - 460; // This produces a negative number
joyposVert = joyposVert * -1; // Make the number positive

MotorSpeed1 = map(joyposVert, 0, 460, 0, 255);

analogWrite(enA, MotorSpeed1);

}
else if (joyposVert > 564)
{
// This is Forward

// Set Motor A forward

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);

//Determine Motor Speeds

MotorSpeed1 = map(joyposVert, 564, 1023, 0, 255);

analogWrite(enA, MotorSpeed1);
}
{ if (joyposHorz < 460)
{
// This is Backward

// Set Motor B backward

digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

//Determine Motor Speeds

// As we are going backwards we need to reverse readings

joyposHorz = joyposHorz - 460; // This produces a negative number
joyposHorz = joyposHorz * -1; // Make the number positive

MotorSpeed2 = map(joyposHorz, 0, 460, 0, 255);

analogWrite(enB, MotorSpeed2);
}
else if (joyposHorz > 564)
{

// Set Motor B forward

digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

//Determine Motor Speeds

MotorSpeed2 = map(joyposHorz, 564, 1023, 0, 255);

analogWrite(enB, MotorSpeed2);

}
else
{
// This is Stopped

MotorSpeed1 = 0;
MotorSpeed2 = 0;

}
{
// here analog write was

}
}
}

Post your code in code tags. You are doing this:
heresMyCode();
But it should look like this:

heresMyCode();

If you don't understand what this means, read the link I provided in reply #10. People will be less likely to help you if you don't do this.

I'm not going to look through your latest code to try and see what you've changed. Why don't you try following some of the suggestions? I think you are unlikely to solve your problem (at least not quickly or easily) unless you take a more systematic approach.

The very first reply you got in this topic told you exactly the approach you should take

vinceherman:
Have you looked for tutorials on each component?
That is usually the way I approach a project.
Find a tutorial on the joysticks.
Wire it up, get your code to compile, see the results printed to the monitor as you move the joysticks.
Then a new sketch. Wire up the motor driver. Code from the tutorial. Can you make the motors run?

Try creating these two individual sketches. There are many tutorials on the internet (e.g. Sparkfun and other similar hobby electronics retailers have tutorials to go along with a lot of their products).