Arduino Forum

Using Arduino => Motors, Mechanics, Power and CNC => Topic started by: Roger_bb on Feb 03, 2014, 11:50 pm

Title: I need help to control 2 dc motors with a ps2 analog joystick
Post by: Roger_bb on Feb 03, 2014, 11:50 pm
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:
Code: [Select]
int joy1 = 0;
int motor = 3;

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

}

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

This code only control 1 motor.I need control of speed and direction of 2 motors simultaneosly.Using Arduino UNO R3.Any help please.
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: JimboZA on Feb 04, 2014, 03:51 am
To do the speed thing on another motor, just double up the code and adjust the pins and variables.

Something like this: untested, ymmv

Code: [Select]
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?

Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: roter45 on Feb 04, 2014, 04:06 pm
Quote
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)

Quote
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 http://forum.arduino.cc/index.php?topic=215312.0;prev_next=next#new (http://forum.arduino.cc/index.php?topic=215312.0;prev_next=next#new) 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
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: Roger_bb on Feb 04, 2014, 10:16 pm
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:https://www.mediafire.com/?evm2wyjclwh95f5 (https://www.mediafire.com/?evm2wyjclwh95f5)
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 http://www.alldatasheet.com/datasheet-pdf/pdf/22432/STMICROELECTRONICS/L293D.html (http://www.alldatasheet.com/datasheet-pdf/pdf/22432/STMICROELECTRONICS/L293D.html)
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: JimboZA on Feb 05, 2014, 04:12 am
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 (http://www.ecs.umass.edu/ece/m5/tutorials/tip122_transistor_tutorial.html). 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...

Quote
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.


Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: roter45 on Feb 05, 2014, 02:42 pm
I recommend to use something like this:

http://dx.com/es/p/l298n-stepper-motor-driver-controller-board-for-arduino-120542#.UvI-WT15N8E. 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.

http://blog.bricogeek.com/noticias/tutoriales/tutorial-robot-4x4-con-arduino/#more


Then any question that you might have ask here.
Quote
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:

Code: [Select]

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 http://forum.arduino.cc/index.php?topic=215164.msg1574518#msg1574518
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: Roger_bb on Feb 06, 2014, 06:21 pm
Quote
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:
Code: [Select]
 
  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?
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: JimboZA on Feb 06, 2014, 06:31 pm
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.
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: roter45 on Feb 06, 2014, 11:07 pm
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.

Code: [Select]

// 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);}
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: Roger_bb on Feb 07, 2014, 07:04 pm
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.
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: roter45 on Feb 07, 2014, 11:29 pm
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
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: JimboZA on Feb 08, 2014, 05:45 am

Here you have a complete tutorial controlrobotics.rodrigomompo.com


Nice site
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: Roger_bb on Feb 08, 2014, 07:03 pm
Quote
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?
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: roter45 on Feb 10, 2014, 09:46 pm
yes


My robot use this model too.


Just connect digital output  to the arduino and linear output without any wire.
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: Roger_bb on Feb 16, 2014, 07:43 pm
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.
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: JimboZA on Feb 16, 2014, 07:51 pm
Quote
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.
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: roter45 on Feb 18, 2014, 05:04 pm
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:
Code: [Select]

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  :smiley-eek-blue: :smiley-red:

Try it and post your code

Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: Roger_bb on Feb 22, 2014, 07:20 pm
thanks for the code, but i think i not explain what i'm doing with the joystick thing.
http://www.youtube.com/watch?v=PLHYmyXcQWY (http://www.youtube.com/watch?v=PLHYmyXcQWY)
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?
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: roter45 on Feb 24, 2014, 12:08 pm
In this case, duplicate the code but changing the variables.

If you use the same variables (Just copy and paste) it won't work.
Title: Re: I need help to control 2 dc motors with a ps2 analog joystick
Post by: Roger_bb on Feb 28, 2014, 08:13 pm
Quote
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.