Go Down

Topic: I need help to control 2 dc motors with a ps2 analog joystick (Read 12170 times) previous topic - next topic

Roger_bb

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.

JimboZA

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?

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

roter45

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

Roger_bb

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

JimboZA

#4
Feb 05, 2014, 04:12 am Last Edit: Feb 05, 2014, 05:00 am by JimboZA Reason: 1
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...

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.


Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

roter45

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

Roger_bb

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?

JimboZA

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.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

roter45

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);}

Roger_bb

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.

roter45

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

JimboZA

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

Roger_bb

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?

roter45

yes


My robot use this model too.


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

Roger_bb

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.

Go Up