PS2 Controller problem with l293d motor driver

Hi there,
I am controlling my robot with PS controller. I am using 2 motor driver and one of them is l293d. i Wrote following code to control my robot.

#include <PS2X_lib.h>
#define pressures   true
#define rumble      true

byte vibrate = 0;
PS2X ps2x;

int error = 0;
int type = 0;

int l_speed = 50;
int m_speed1 = 100;
int m_speed2 = 150;
int h_speed = 255;
int Speed = 70;

int LX;              //Variables for reading stick values
int LY;
int RX;
int RY;

int L_DIR = 14;          //Pins For left side of motor
int L_PWM = 10;
int L_BRK = 15;

int R_DIR = 16;          //Pins for right side of motor
int R_PWM = 11;
int R_BRK = 17;

int enable = 6;
int i1 = 5;           //for gripper motor
int i2 = 4;
int i3 = 3;           //for gripper up down motor
int i4 = 2;

int StopperUp = 1;
int StopperDown = 18;
int gripperStopper = 19;

int readUp;
int readDown;

void rotateclockwise(unsigned int );
void rotateanticlockwise(unsigned int );
void l_forward(unsigned int );
void l_backward(unsigned int );
void r_forward(unsigned int );
void r_backward(unsigned int );
void run(void);

void gripperStop(void)
{
  digitalWrite(i1, LOW);
  digitalWrite(i2, LOW);
}
void upDownStop(void)
{
  digitalWrite(i3, LOW);
  digitalWrite(i4, LOW);
}

/*void gripperOpen()
{
  if(digitalRead(gripperStopper) != HIGH)
  {
    digitalWrite(i1, LOW);
    digitalWrite(i2, HIGH);
  }
  else
    gripperStop();
}
void gripperClose(void)
{
  digitalWrite(i1, HIGH);
  digitalWrite(i2, LOW);
}*/
/*void gripperUp(void)
{
  if(digitalRead(StopperUp) != HIGH && digitalRead(StopperDown) == HIGH)
  {
    digitalWrite(i3, LOW);
    digitalWrite(i4, HIGH);
  }
  else
    upDownStop();
}
void gripperDown(void)
{
  if(digitalRead(StopperDown) != HIGH && digitalRead(StopperUp) == HIGH)
  {
    digitalWrite(i3, HIGH);
    digitalWrite(i4, LOW);
  }
  else
    upDownStop();
}*/

void setup()
{
  
  delay(300);
  
  pinMode(L_DIR, OUTPUT);
  pinMode(L_PWM, OUTPUT);
  pinMode(L_BRK, OUTPUT);
  
  pinMode(R_DIR, OUTPUT);
  pinMode(R_PWM, OUTPUT);
  pinMode(R_BRK, OUTPUT);

  pinMode(enable, OUTPUT);
  pinMode(i1, OUTPUT);
  pinMode(i2, OUTPUT);
  pinMode(i3, OUTPUT);
  pinMode(i4, OUTPUT);

  pinMode(gripperStopper, OUTPUT);
  pinMode(StopperUp, OUTPUT);
  pinMode(StopperDown, OUTPUT);

  digitalWrite(enable, HIGH);
  
  //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  error = ps2x.config_gamepad(9,8,7,12,pressures,rumble);
   
}

void loop()
{
  if(error == 1) //skip loop if no controller found
    return;
  
  digitalWrite(L_BRK, HIGH);
  digitalWrite(R_BRK, HIGH);
  upDownStop();
  gripperStop();
  
  ps2x.read_gamepad(false, vibrate);
  

  //code for l293d
  if(ps2x.Button(PSB_L1))
  {
      //gripperOpen();
  }

  else if(ps2x.Button(PSB_L2))
  {
      //gripperClose();
  }

  else if(ps2x.Button(PSB_PAD_UP))
  {
      digitalWrite(i3, HIGH);
      digitalWrite(i4, LOW);
  }

  else if(ps2x.Button(PSB_PAD_DOWN))
  {
      digitalWrite(i3, LOW);
      digitalWrite(i4, HIGH);
  }
  
   //Code for bot movement

  LY = ps2x.Analog(PSS_LY);
  delay(5);
  LX = ps2x.Analog(PSS_LX);
  delay(5);
  RY = ps2x.Analog(PSS_RY);
  delay(5);
  RX = ps2x.Analog(PSS_RX);
  delay(5);
  
  if(ps2x.ButtonPressed(PSB_CROSS))
    Speed = l_speed;
  
  else if(ps2x.ButtonPressed(PSB_SQUARE))
    Speed = m_speed1;
  
  else if(ps2x.ButtonPressed(PSB_CIRCLE))
    Speed = m_speed2;
  
  else if(ps2x.ButtonPressed(PSB_TRIANGLE))
    Speed = h_speed;
  
  if(LY < 127 && LX == 128)
  {
    while(LY < 127 && LX == 128)
    {
      run();
      
      if(ps2x.ButtonPressed(PSB_CROSS))
      {
        Speed = l_speed;
      }
      
      else if(ps2x.ButtonPressed(PSB_SQUARE))
      {
        Speed = m_speed1;
      }
      
      else if(ps2x.ButtonPressed(PSB_CIRCLE))
      {
        Speed = m_speed2;
      }
      
      else if(ps2x.ButtonPressed(PSB_TRIANGLE))
      {
        Speed = h_speed;
      }
      
          LY = map(LY, 0, 127, 0, Speed);
          LY = Speed - LY;
          //l_forward(LY);
          //r_forward(LY);
            digitalWrite(L_DIR, LOW);
            analogWrite(L_PWM, LY);
            delay(5);
            digitalWrite(R_DIR, LOW);
            analogWrite(R_PWM, LY);
            delay(5);
          ps2x.read_gamepad();
          LY = ps2x.Analog(PSS_LY);
    }
  }
  
  else if(LY > 127 && LX == 128)
  {
    while(LY > 127 && LX == 128)
    {
      run();
      
      if(ps2x.ButtonPressed(PSB_CROSS))
      {
        Speed = l_speed;
      }
      
      else if(ps2x.ButtonPressed(PSB_SQUARE))
      {
        Speed = m_speed1;
      }
      
      else if(ps2x.ButtonPressed(PSB_CIRCLE))
      {
        Speed = m_speed2;
      }
      
      else if(ps2x.ButtonPressed(PSB_TRIANGLE))
      {
        Speed = h_speed;
      }
      
        LY = map(LY, 128, 255, 0, Speed);
        //l_backward(LY);
        //r_backward(LY);
        digitalWrite(L_DIR, HIGH);
        analogWrite(L_PWM, LY);
        delay(5);
        digitalWrite(R_DIR, HIGH);
        analogWrite(R_PWM, LY);
        delay(5);
        ps2x.read_gamepad();
        LY = ps2x.Analog(PSS_LY);
    }
  }
 else if(RX < 128 && RY == 127)
 {
   while(RX < 128 && RY == 127)
   {
     run();
     
     if(ps2x.ButtonPressed(PSB_CROSS))
      {
        Speed = l_speed;
      }
      
      else if(ps2x.ButtonPressed(PSB_SQUARE))
      {
        Speed = m_speed1;
      }
      
      else if(ps2x.ButtonPressed(PSB_CIRCLE))
      {
        Speed = m_speed2;
      }
      
      else if(ps2x.ButtonPressed(PSB_TRIANGLE))
      {
        Speed = h_speed;
      }
     
     RX = map(RX, 0, 127, 0, Speed);
     RX = Speed - RX;
     rotateanticlockwise(RX);
     ps2x.read_gamepad();
     RX = ps2x.Analog(PSS_RX);
   }
 }
 
 else if(RX > 128 && RY == 127)
 {
   while(RX > 128 && RY == 127)
   {
     run();
     
     if(ps2x.ButtonPressed(PSB_CROSS))
      {
        Speed = l_speed;
      }
      
      else if(ps2x.ButtonPressed(PSB_SQUARE))
      {
        Speed = m_speed1;
      }
      
      else if(ps2x.ButtonPressed(PSB_CIRCLE))
      {
        Speed = m_speed2;
      }
      
      else if(ps2x.ButtonPressed(PSB_TRIANGLE))
      {
        Speed = h_speed;
      }
     
     RX = map(RX, 128, 255, 0, Speed);
     rotateclockwise(RX);
     ps2x.read_gamepad();
     RX = ps2x.Analog(PSS_RX);
   }
 }
 else
 {
   digitalWrite(L_BRK, HIGH);
   digitalWrite(R_BRK, HIGH);
   gripperStop();
   upDownStop();
 }
}

void l_forward(unsigned int ly)
{
  digitalWrite(L_DIR, LOW);
  analogWrite(L_PWM, ly);
  delay(5);
}

void r_forward(unsigned int ly)
{
  digitalWrite(R_DIR, LOW);
  analogWrite(R_PWM, ly);
  delay(5);
}

void l_backward(unsigned int ly)
{
  digitalWrite(L_DIR, HIGH);
  analogWrite(L_PWM, ly);
  delay(5);
}

void r_backward(unsigned int ly)
{
  digitalWrite(R_DIR, HIGH);
  analogWrite(R_PWM, ly);
  delay(5);
}

void rotateclockwise(unsigned int ly)
{
  l_forward(ly);
  r_backward(ly);
  delay(5);
}

void rotateanticlockwise(unsigned int ly)
{
  r_forward(ly);
  l_backward(ly);
  delay(5);
}

void run()
{
  digitalWrite(L_BRK, LOW);
  digitalWrite(R_BRK, LOW);
}

using joystick bot is moving fine but when i press up button, ps2 controller reconnects and motor starts moving itself and weird things start happenings. If i remove the Code for l293d everything is working fine
as soon as i added the l293d problem started happening. Is there any problem with the code?

Likely a power or interference problem - the current surge when turning on/off a motor is
causing issues. How are you powering everything (the Arduino should not share power with
the motor, that's an obvious way to cause such problems)

I am powering all motors with external 11.1v 200mah lipo and arduino is powered with 9v battery. Ground of both are connected together.

I tried following things.
I disconnected the lipo battery which is powering motors. Added serial.pintln() like following

if(ps2x.Button(PSB_R1))
  {
    if(readUp != 1)
    {
        digitalWrite(i3, HIGH);
        digitalWrite(i4, LOW);
        Serial.println("Inside Up");
    }
  }

  else if(ps2x.Button(PSB_R2))
  {
    if(readDown != 1)
    {
        digitalWrite(i3, LOW);
        digitalWrite(i4, HIGH);
        Serial.println("Down");
    }
  }

When lipo battery is not connected, it's printing up and down correctly on serial monitor when i press up and down. When i connect lipo back, Weird things start s happening like controller loses signal. motor spinning by itself etc.

Hi,
Can you post a picture of your project and a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
What size power wise is your motors?

Tom.... :slight_smile:

Motor is not taking current more than 300ma and i am sorry, i can't post picture of my circuit right now.

Thank you all for your time. I found my mistake, it was connection problem.