I need help to control 2 dc motors with a ps2 analog joystick

To do the speed thing on another motor, just double up the code and adjust the pins and variables.

Something like this: untested, ymmv

int joy1 = 0;
int motor = 3;
int joy2 = 1;
int motor2 = 5;   //  pin 4 isn't pwm so use 5

void setup(){
  
pinMode(motor,OUTPUT);
pinMode(joy1,INPUT);

pinMode(motor2,OUTPUT);
pinMode(joy2,INPUT);
}

void loop(){
  int ppp;
int ppp2;
  ppp = analogRead(joy1);  
  ppp = map(ppp, 0, 1023, -255, 255);     
  analogWrite(motor,ppp);

 ppp2 = analogRead(joy2);  
  ppp2 = map(ppp2, 0, 1023, -255, 255);     
  analogWrite(motor,ppp2);
}

For direction, you will need to give details about the connections to the motor. Direction control needs an h-bridge to reverse the polarity on the motor. Can you post a circuit diagram?

I want to control 2 dc motors with the joystick,but i don't know how to mix two pwm outputs for the motors.
here is my initial code

I have a robot with two motors controlled with a joy stick. (You control the robot like an Rc car)

Direction control needs an h-bridge to reverse the polarity on the motor. Can you post a circuit diagram?

My setup it's with an H bridge. Depending on the model the code changes a little.. As jimboZA said post your setup and then we can see what solution is the best. I also have some problems using motors and an H bridge. I'm asking in Starting servo position code needed - Motors, Mechanics, Power and CNC - Arduino Forum this post and then if i have time I will write a post for my blog with the information controlrobotics.rodrigomompo.com

The code seems to use analogWrite with negative values. If you read the arduino reference you will see that you can only write values form 0 to 255 http://arduino.cc/es/Reference/AnalogWrite

JimboZA i've tested the code ,but its not working to me. i decided to use the l293d motor driver for speed and direction control.I'm
using a tip41c to drive the motor.here is my current conections:motor joystick_bb
roter45 can you post images of your robot?do you made this? Sorry if i don't write wright, i am from Brazil. Thanks!

l293d datasheet L293D Datasheet(PDF) - STMicroelectronics

Looks to me that you have the motor on the wrong side of the transistor. With an npn, the load should be above the collector ie between + and collector, with the emitter to ground. You have the collector to +ve and the load below the emitter.

Have a look here. You also need a diode across the motor as shown in that link. Also probably not wise to supply the motor's power from the Arduino.

Also, worth checking that the power rails on your breadboard go all the way from one end to the other: on some makes there is a break in the middle.

EDIT...

The code seems to use analogWrite with negative values

Well spotted, which makes for a bit of complication since OP presumably wants the joysticks centred to be 0 speed and then left to tuen one way, right the other. Need to think about the mapping for that....

MORE edit... something along the lines of if the stick reads 0 (full left), thats still a 255 PWM on the 293's enable pin and so is 1023, full right. Stick reading of 512 must be pwm of 0? But if the stick is <512 then set the 293's inputs on one channel to be hi/lo and >512, lo/high, so as to reverse the direction between the left and right side of the stick.

I recommend to use something like this:

Recomendaciones para ti - DealeXtreme. Here it's very cheap and would make the thing easier. It cost R$ 13,55 or 5,62 US$

Can you understand Spanish ? Here you have a good tutorial, with this type of controller and with a code. Google translator it's a good way too.

Then any question that you might have ask here.

Well spotted, which makes for a bit of complication since OP presumably wants the joysticks centred to be 0 speed and then left to tuen one way, right the other. Need to think about the mapping for that....

JimboZa ,any easy way of doing that is:

sensorRead = analogRead(0);
if(sensorRead > 700){ speed = map(sensorRead,512,1023,0,255);}
if(sensorRead < 400){ speed = map(sensorRead,0,512,255,0);}

The reason of using 700 and 400 it's for my experience. In real live the middle isn't 512. This way you have enough sensitive and not to much sensitive

You can see fotos of my robot in this post Rover 5 + 4 Motor Driver. Problem - Motors, Mechanics, Power and CNC - Arduino Forum

Well spotted, which makes for a bit of complication since OP presumably wants the joysticks centred to be 0 speed and then left to tuen one way, right the other. Need to think about the mapping for that....

MORE edit... something along the lines of if the stick reads 0 (full left), thats still a 255 PWM on the 293's enable pin and so is 1023, full right. Stick reading of 512 must be pwm of 0? But if the stick is <512 then set the 293's inputs on one channel to be hi/lo and >512, lo/high, so as to reverse the direction between the left and right side of the stick.

Yes, i need the joystick center to be 0 .But how i change direction when the joystick is full right or full left?
Somenthing like this:

  if(joystick < 512) {digitalWrite(2,HIGH);}    //digital outputs for l293's inputs
  if(joystick > 512) {digitalWrite(4,HIGH);}

May this work? when the stick is in one side , an signal goes to l293's to change the motor direction , in that case is more easy to map the sitck value.

what is more dificult,make the codes or making the mechanical parts and connections?

Yes I think what you suggest for direction is on the right track, coupled with roter's code to do the speed.

Not sure what you mean by "making the mechanical parts"?- the connections should be easy enough if you use breadboard.

Here it's the final code for one motor
Controlling two motors will be a bit different.

I recommed you to not start using the motor at 512. Because the motor will not be full stop never. The middle in joysticks it's not 512. Depending the alienation I have some with 456 the center and some with 598 the center . My limits are the result of hours trying different limits.

// 2,3,4 are the pins that controls the motor
// In some motor drivers I1 =2 ; I2=4; EA(Enablepin) = 3 
sensorRead = analogRead(0);
if(sensorRead > 700){
 speed = map(sensorRead,512,1023,0,255
digitalWrite(2,HIGH);
digitalWrite(4,LOW));
analogWrite(3,speed);}
if(sensorRead < 400){
 speed = map(sensorRead,0,512,255,0);
digitalWrite(4,HIGH);
digitalWrite(2,LOW));
analogWrite(3,speed);}

I will test the code, but first i have to buy the motor driver because i don't have one.It will take near a week, because i'm buying from internet. When i get the driver and test the code, i'll post the results. Thanks for your colaboration.

Your welcome

After testing it, you can improve your robot controling it using RF. With two arduinos and an Rf link kit 433 mhz you can have a very cheep set up

Here you have a complete tutorial controlrobotics.rodrigomompo.com

roter45:
Here you have a complete tutorial controlrobotics.rodrigomompo.com

Nice site

After testing it, you can improve your robot controling it using RF. With two arduinos and an Rf link kit 433 mhz you can have a very cheep set up

i have 1 or 2 of this rf kit,but with more pins. Is it compatible?

modulorfreceptortransmissor433mhz_MLBO3211240234_102012.jpg

yes

My robot use this model too.

Just connect digital output to the arduino and linear output without any wire.

I alredy get the l293d and test the code, it really works.I needed to make some adjustments for the center of the joystick to be 0,but now it works perfectly.The middle of my joystick is 502,i tested with the analogread example in arduino IDE.now i need the code for two motors,i tried to double the code but it not works,what do I do now?
I can post a video.

i need the code for two motors,i tried to double the code but it not works,what do I do now?

I think you post the code to show what you tried, and explain what didn't work.

Hi

Really when moving a robot, you have two motors (L motor and R motor)
So you have 4 possible combinations (could be more).

going straight (L forward ,R forward)
Back (L backward, R backward)
Right(L forward, R backward)
Left(L backward, R Forward)

you use a switch case with the four positions using the previous code.

How to obtain the case be need. There are different forms, for my robot I use this:

x= analogRead(0);// values 
y = analogRead(1);//

if(x > 700){
go straight}
if(x <400){
back}
if ( 400< x < 700){
      if(y > 700){
        left}
      if(y <400){
         right}
      if(400< y < 700){
      stop}
}

I recomend you to use this limit values, because I obtain it after trying hours through you should try and discover yours.

Also It's important to put the stop mode, you don't want your robot going ahead and falling from the table :fearful: :blush:

Try it and post your code

thanks for the code, but i think i not explain what i'm doing with the joystick thing.

my robot is much like this one in the video , however i want only to drill the PCB.I want to control it manually too,this is the joystick service.
does your code work for this purpose?

In this case, duplicate the code but changing the variables.

If you use the same variables (Just copy and paste) it won't work.

In this case, duplicated the code but changing the variables.

i duplicated the code, changed the variables and it working now. But one motor is not working properly, its not going backward,i think the problem is on the driver output. I'll make some tests and when my robot is working, i will post a video.
Thanks for the help.