Go Down

Topic: turning right/left using arduinoduemilanove+l293d (Read 1 time) previous topic - next topic

k2s

hey

i was trying to make my bot just turn right using l293d and 2 dc motors at the same position(at junction of a grid)[make the right whell go backward and left wheel go forward]. This is a simple code i came up with but the problem is , my left wheel starts rotating first and my right wheel starts after a lag. plz see if there is anything wrong...
thx in advance!

Code: [Select]

int pwm2=6,rt1=11,rt2=12;//right motor
int pwm=9,lt1=1,lt2=2;//left motor

void turn_right();

void setup()
{
 //all pins are made OUTPUT
}

void loop()
{
  turn_right();
}
/************************************
forward means *t1=HIGH and *t2=LOW
*************************************/
void turn_right()
{
 
 digitalWrite(rt1,LOW);//right motor bckward
 digitalWrite(rt2,HIGH);
 digitalWrite(lt1,HIGH);//left motor forward
 digitalWrite(lt2,LOW);
 
 //digitalWrite(pwm2,HIGH);
 //digitalWrite(pwm,HIGH);
 analogWrite(pwm,127);
 analogWrite(pwm2,127);
 
 delay(100);//so that the condition for stopping is not reached before it even starts turning
while(1)
 {
   if(condition for stopping the turning)//
   return;
 }
 
}

//just read above, turn left and go forward functions will be easy if i figure out turn_right


my question is that in the function turn_right, once i give the command

analogWrite(blahblah_pin,xyz)

does the blahblah_pin keep sending the pwm signal till the rest of the program... ie.. till while(1) condition is dissatisfied?

or do we have to keep calling the turn_right func repeatedly from loop?

AWOL

analogWrite is a one-off - the setting is kept until the next analogWrite
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

k2s

so how do i implement pwm with arduino to control two motors simultaneously? can u help me with the code?

k2s

also if its a one off line.. then my wheels should move alternately..
in my case.. they both move simultaneously but the right wheel starts moving after a lag of about 3-4 secs

AWOL

Quote
so how do i implement pwm with arduino to control two motors simultaneously?

Two consecutive "analogWrite"s to different pins.
The execution time between the two writes will (in a mechanical motor scale of things) be miniscule, and you won't notice that the motors don't quite start instantaneously.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

k2s

#5
Dec 26, 2009, 08:32 pm Last Edit: Dec 26, 2009, 08:33 pm by k2s Reason: 1
thats what ive done:

<quote>
analogWrite(pwm,127);
analogWrite(pwm2,127);
</quote>

k2s

i even tried using just one pwm pin which is connected to both the enable pins of the l293d. still same problem

ringz

k2s,

Are you using geared motors?

If you are it's possible that you are suffering from backlash from the gears in the gearbox. 3 to 4 seconds is a bit much for backlash but something you can easily check out.

--
Martyn.


k2s

yes i am using geared motors.. what is backlash and how do i get rid of it?

AWOL

3 or 4 seconds isn't backlash.
It sounds more like your processor is resetting and then restarting.
You may need some decoupling/suppression - have a look at Grumpy Mike's tutorials.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

k2s


k2s

yea i think u may be right.. i found that tutorial...

http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

ill try and implement it tom morn..

also.. do you think this can be averted by using two batteries? 1 for the arduino and the other for the motors?

k2s

thank you everyone.. i used two batteries and voila.. its all good..w00t

Go Up