Go Down

Topic: I had a problem on L293D (Read 602 times) previous topic - next topic

kokpat

Dear

I had a problem on L293D.
I would like to make an IR remote tank which using the IR remote library from Ken Shirriff's blog.
When I send IR signal to arduino and turn on the motor, the motor turn on but arduino hang.
I had checked out several possible problems and finally I can get the control if I disable the L293D after delay a short while.
However, I would like the tank continous to run without stop.
I had some similar project did not need to disable the L293D for a while, so it really confuse me.
Could anybody suggest a reason for me ?


+++++++++ here is the code that I can get the control if I disable the L293D after delay a short while ++++++

void motor(int MotorA1, int MotorA2, int MotorB1, int MotorB2) {
digitalWrite(MotorENAPin, HIGH);
digitalWrite(MotorENBPin, HIGH);
if (MotorA1==0) digitalWrite(MotorA1Pin,LOW);
if (MotorA2==0) digitalWrite(MotorA2Pin,LOW);
if (MotorB1==0) digitalWrite(MotorB1Pin,LOW);
if (MotorB2==0) digitalWrite(MotorB2Pin,LOW);
if (MotorA1==1) digitalWrite(MotorA1Pin,HIGH);
if (MotorA2==1) digitalWrite(MotorA2Pin,HIGH);
if (MotorB1==1) digitalWrite(MotorB1Pin,HIGH);
if (MotorB2==1) digitalWrite(MotorB2Pin,HIGH);
delay(800);
digitalWrite(MotorENAPin, LOW);
digitalWrite(MotorENBPin, LOW);
}

robtillaart


small code refactor, as LOW = 0 and HIGH = 1...

Code: [Select]
void motor(int MotorA1, int MotorA2, int MotorB1, int MotorB2)
{
  digitalWrite(MotorENAPin, HIGH);
  digitalWrite(MotorENBPin, HIGH);

  digitalWrite(MotorA1Pin, MotorA1);
  digitalWrite(MotorA2Pin ,MotorA2);
  digitalWrite(MotorB1Pin, MotorB1);
  digitalWrite(MotorB2Pin, MotorB2);
  delay(800);
  digitalWrite(MotorENAPin, LOW);
  digitalWrite(MotorENBPin, LOW);
}


please use the # button to get appropiate [ code] tags...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

JasonK

Please post you whole code, both the "working" and "not working" versions, otherwise we are left to guessing and divination.

Normally you want to avoid "delay" in code were you are doing multiple things and this may be your problem (see blink with out delay example code).

kokpat

The program mainly refer to the following :
http://www.e-shore.com.my/homepage/all-projects/193-ir-remote-control-mobile-robot.html

Here is the working code (stop at 800ms after each action)

Code: [Select]
//............ Import Library ...............
//
#include <IRremote.h>  // includes the IR Library - from Ken Shirriff's blog                                                                                                                                                                   
//
//............ Declare PIN Variables ............
//
const int FrontLED = 13;
//............ LEFT Gear Motor .................
const int MotorA1Pin =4;
const int MotorA2Pin = 2;
const int MotorENAPin = 3;
//............ RIGHT Gear Motor ................
const int MotorB1Pin = 8;
const int MotorB2Pin = 7;
const int MotorENBPin = 5;
//............ IR Receiver Pin ..................
int RECV_PIN = 12;
IRrecv irrecv(RECV_PIN);
decode_results results;
//
//............ Arduino Setup ................
//
void setup()
{
// Serial.begin(9600);
//............ Configure pins ...............
pinMode(FrontLED,OUTPUT);
//............ LEFT Gear Motor .................
pinMode(MotorA1Pin,OUTPUT);
pinMode(MotorA2Pin,OUTPUT);
pinMode(MotorENAPin,OUTPUT);
//............ RIGHT Gear Motor ................
pinMode(MotorB1Pin,OUTPUT);
pinMode(MotorB2Pin,OUTPUT);
pinMode(MotorENBPin,OUTPUT);
//............ Enable Motors ...............
digitalWrite(MotorENAPin, HIGH);
digitalWrite(MotorENBPin, HIGH);
//............ Start IR receiver ............
irrecv.enableIRIn();
}
//
//............ Arduino Loop .................
//
void loop() {
//............ IR signal received ...........
if(irrecv.decode(&results))
{
//............ IR Input - 2 - Tank Motion _Forward
if(results.value==0xFD8877)
{
    motor(0,1,1,0);
}
//............ IR Input - 8 - Tank Motion_Backward
else if(results.value==0xFD9867)
{
    motor(1,0,0,1);
}
//............ IR Input - 4 - Tank Motion_Left
else if(results.value==0xFD28D7)
{
      motor(0,1,0,0);
}
//............ IR Input - 6 - Tank Motion_Right
else if(results.value==0xFD6897)
{
      motor(0,0,1,0);
}
//............ IR Input - 1 - Tank Motion_Fast Left
else if(results.value==0xFD08F7)
{
      motor(0,1,0,1);
}
//............ IR Input - 3 - Tank Motion_Fast Right
else if(results.value==0xFD48B7)
{
      motor(1,0,1,0);
}
//............ IR Input - 7 - Tank motion Right_backward
else if(results.value==0xFD18E7)
{
      motor(0,0,0,1);
}
//............ IR Input - 9 - Tank motion Left_backward
else if(results.value==0xFD58A7)
{
      motor(1,0,0,0);
}
//............ IR Input - 5 - Stop Motion
else if(results.value==0xFDA857)
{
      motor(0,0,0,0);
}
//............ IR Input - EQ - Turn on the LED
else if(results.value==0xFDB04F)
{
if (digitalRead(FrontLED)==HIGH)
{
digitalWrite(FrontLED,LOW);
}
else
{
  digitalWrite(FrontLED,HIGH);
}
}
//............ Receive the next value ...............
irrecv.resume();
//............ Short delay waiting for repeating IR signal
delay(300);
}
}
//
// Function to control the motor
// Non-return value subroutine
//
void motor(int MotorA1, int MotorA2, int MotorB1, int MotorB2) {
digitalWrite(MotorENAPin, HIGH);
digitalWrite(MotorENBPin, HIGH);
if (MotorA1==0) digitalWrite(MotorA1Pin,LOW);
if (MotorA2==0) digitalWrite(MotorA2Pin,LOW);
if (MotorB1==0) digitalWrite(MotorB1Pin,LOW);
if (MotorB2==0) digitalWrite(MotorB2Pin,LOW);
if (MotorA1==1) digitalWrite(MotorA1Pin,HIGH);
if (MotorA2==1) digitalWrite(MotorA2Pin,HIGH);
if (MotorB1==1) digitalWrite(MotorB1Pin,HIGH);
if (MotorB2==1) digitalWrite(MotorB2Pin,HIGH);
delay(800);
digitalWrite(MotorENAPin, LOW);
digitalWrite(MotorENBPin, LOW);
}


The following is the code which hang after receive any motor action. (Same code but different in the last paragraph)

Quote
//............ Import Library ...............
//
#include <IRremote.h>  // includes the IR Library - from Ken Shirriff's blog                                                                                                                                                                   
//
//............ Declare PIN Variables ............
//
const int FrontLED = 13;
//............ LEFT Gear Motor .................
const int MotorA1Pin =4;
const int MotorA2Pin = 2;
const int MotorENAPin = 3;
//............ RIGHT Gear Motor ................
const int MotorB1Pin = 8;
const int MotorB2Pin = 7;
const int MotorENBPin = 5;
//............ IR Receiver Pin ..................
int RECV_PIN = 12;
IRrecv irrecv(RECV_PIN);
decode_results results;
//
//............ Arduino Setup ................
//
void setup()
{
// Serial.begin(9600);
//............ Configure pins ...............
pinMode(FrontLED,OUTPUT);
//............ LEFT Gear Motor .................
pinMode(MotorA1Pin,OUTPUT);
pinMode(MotorA2Pin,OUTPUT);
pinMode(MotorENAPin,OUTPUT);
//............ RIGHT Gear Motor ................
pinMode(MotorB1Pin,OUTPUT);
pinMode(MotorB2Pin,OUTPUT);
pinMode(MotorENBPin,OUTPUT);
//............ Enable Motors ...............
digitalWrite(MotorENAPin, HIGH);
digitalWrite(MotorENBPin, HIGH);
//............ Start IR receiver ............
irrecv.enableIRIn();
}
//
//............ Arduino Loop .................
//
void loop() {
//............ IR signal received ...........
if(irrecv.decode(&results))
{
//............ IR Input - 2 - Tank Motion _Forward
if(results.value==0xFD8877)
{
    motor(0,1,1,0);
}
//............ IR Input - 8 - Tank Motion_Backward
else if(results.value==0xFD9867)
{
    motor(1,0,0,1);
}
//............ IR Input - 4 - Tank Motion_Left
else if(results.value==0xFD28D7)
{
      motor(0,1,0,0);
}
//............ IR Input - 6 - Tank Motion_Right
else if(results.value==0xFD6897)
{
      motor(0,0,1,0);
}
//............ IR Input - 1 - Tank Motion_Fast Left
else if(results.value==0xFD08F7)
{
      motor(0,1,0,1);
}
//............ IR Input - 3 - Tank Motion_Fast Right
else if(results.value==0xFD48B7)
{
      motor(1,0,1,0);
}
//............ IR Input - 7 - Tank motion Right_backward
else if(results.value==0xFD18E7)
{
      motor(0,0,0,1);
}
//............ IR Input - 9 - Tank motion Left_backward
else if(results.value==0xFD58A7)
{
      motor(1,0,0,0);
}
//............ IR Input - 5 - Stop Motion
else if(results.value==0xFDA857)
{
      motor(0,0,0,0);
}
//............ IR Input - EQ - Turn on the LED
else if(results.value==0xFDB04F)
{
if (digitalRead(FrontLED)==HIGH)
{
digitalWrite(FrontLED,LOW);
}
else
{
  digitalWrite(FrontLED,HIGH);
}
}
//............ Receive the next value ...............
irrecv.resume();
//............ Short delay waiting for repeating IR signal
delay(300);
}
}
//
// Function to control the motor
// Non-return value subroutine
//
void motor(int MotorA1, int MotorA2, int MotorB1, int MotorB2) {
if (MotorA1==0) digitalWrite(MotorA1Pin,LOW);
if (MotorA2==0) digitalWrite(MotorA2Pin,LOW);
if (MotorB1==0) digitalWrite(MotorB1Pin,LOW);
if (MotorB2==0) digitalWrite(MotorB2Pin,LOW);
if (MotorA1==1) digitalWrite(MotorA1Pin,HIGH);
if (MotorA2==1) digitalWrite(MotorA2Pin,HIGH);
if (MotorB1==1) digitalWrite(MotorB1Pin,HIGH);
if (MotorB2==1) digitalWrite(MotorB2Pin,HIGH);
}


JasonK

I don't see anything wrong with the code that isn't turning the motor on and off, at least nothing that is different between it and the other code that would cause it to "hang." Have you tried putting in a few debugging statements in your code? (IE flash the LED or send data to the serial interface).

You might want to check  your hardware, you may be getting interference between your motors and your IR receiver.  You seem to be able to receive IR signals when the motors are off,  but not when on.

kokpat

Many Thx.
I will re-solder the circuit and change the electronic component...

BTW, when I use analogwrite (arduino pin is PWM) to control the 1,2 Enable of L293D, there is a high frequency sound produced from the board;
I don't know what's going on, I guess the voltage (6V for small motor) I supplied is not enough, is it ?

Thx.

Go Up