Multiple task at once

Hi there,

I have 20A Duel Motor Driver which i am using to control 4 motors for my bot. I am using PS2 controller to control the movement of the bot using Bill Porter’s PS2 library. I wrote the following program for basic left, right, forward and backward.

#include<PS2X_lib.h>

#define pressures   true
#define rumble      true

byte vibrate = 0;
PS2X ps2x;

int error = 0;
int type = 0;

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

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

int L_DIR = 12;          //Pins For left side of motor
int L_PWM = 3;
int L_BRK = 13;

int R_DIR = 14;          //Pins for right side of motor
int R_PWM = 11;
int R_BRK = 15;

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 setup()
{
  Serial.begin(9600);
  
  pinMode(L_DIR, OUTPUT);
  pinMode(L_PWM, OUTPUT);
  pinMode(L_BRK, OUTPUT);
  
  pinMode(R_DIR, OUTPUT);
  pinMode(R_PWM, OUTPUT);
  pinMode(R_BRK, OUTPUT);
  
  //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  error = ps2x.config_gamepad(9,8,7,6,pressures,rumble);
}

void loop()
{
  if(error == 1) //skip loop if no controller found
    return;
    
    digitalWrite(L_BRK, HIGH);
    digitalWrite(R_BRK, HIGH);
    
    ps2x.read_gamepad(false, vibrate);
    
    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;
        }
        else if(ps2x.ButtonPressed(PSB_START))
        {
          Speed = s_speed;
        }
    
     if(LY < 127 && LX == 128)
    {
      while(LY < 127 && LX == 128)
      {
        run();
        
        digitalWrite(L_BRK, LOW);
        digitalWrite(R_BRK, LOW);
        
            LY = map(LY, 0, 127, 0, Speed);
            LY = Speed - LY;
            l_forward(LY);
            r_forward(LY);
            ps2x.read_gamepad();
            LY = ps2x.Analog(PSS_LY);
      }
    }
    
    else if(LY > 127 && LX == 128)
    {
      while(LY > 127 && LX == 128)
      {
        run();
          digitalWrite(L_BRK, LOW);
          digitalWrite(R_BRK, LOW);
           
          LY = map(LY, 128, 255, 0, Speed);
          l_backward(LY);
          r_backward(LY);
          ps2x.read_gamepad();
          LY = ps2x.Analog(PSS_LY);
      }
    }
   else if(RX < 128 && RY == 127)
   {
     while(RX < 128 && RY == 127)
     {
       run();
        digitalWrite(L_BRK, LOW);
        digitalWrite(R_BRK, LOW);
       
       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();
        digitalWrite(L_BRK, LOW);
        digitalWrite(R_BRK, LOW);
       
       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);
   }
}

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_backward(ly);
  r_forward(ly);
  delay(5);
}

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

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

Above code is working fine.But the problem is the it can only do one task at a time such as when i move left joystick upward, bot moves forward but at same time i can’t do anything else . So i modified the code as following

#include<PS2X_lib.h>
//#include<Servo.h>

#define pressures   true
#define rumble      true

byte vibrate = 0;
PS2X ps2x;

int error = 0;
int type = 0;

/*Servo base;
Servo elbow;
Servo gripper;*/

int pos1 = 160;
int pos2 = 180;
int pos3 = 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 = 12;          //Pins For left side of motor
int L_PWM = 3;
int L_BRK = 13;

int R_DIR = 14;          //Pins for right side of motor
int R_PWM = 11;
int R_BRK = 15;

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 setup()
{
  Serial.begin(9600);

  /*base.attach(16);
  base.write(pos1);
  elbow.attach(5);
  elbow.write(pos2);
  gripper.attach(10);
  gripper.write(pos3);*/

  pinMode(L_DIR, OUTPUT);
  pinMode(L_PWM, OUTPUT);
  pinMode(L_BRK, OUTPUT);

  pinMode(R_DIR, OUTPUT);
  pinMode(R_PWM, OUTPUT);
  pinMode(R_BRK, OUTPUT);

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

void loop()
{
  if(error == 1) //skip loop if no controller found
    return;

  digitalWrite(L_BRK, HIGH);
  digitalWrite(R_BRK, HIGH);

  LY = ps2x.Analog(PSS_LY);
  delay(1);
  LX = ps2x.Analog(PSS_LX);
  delay(1);
  RY = ps2x.Analog(PSS_RY);
  delay(1);
  RX = ps2x.Analog(PSS_RX);
  delay(1);

  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)
  {
      run();

      LY = map(LY, 0, 127, 0, Speed);
      LY = Speed - LY;
      l_forward(LY);
      ps2x.read_gamepad();
      LY = ps2x.Analog(PSS_LY);
  }

  if(LY > 127 && LX == 128)
  {
      run();

      LY = map(LY, 128, 255, 0, Speed);
      l_backward(LY);
      ps2x.read_gamepad();
      LY = ps2x.Analog(PSS_LY);
  }
  if(RY < 127 && RX == 128)
  {
      run();

      RY = map(RY, 0, 127, 0, Speed);
      RY = Speed - RY;
      r_forward(RY);
      ps2x.read_gamepad();
      RY = ps2x.Analog(PSS_RY);
  }

  if(RY > 127 && RX == 128)
  {
      run();

      RY = map(RY, 128, 255, 0, Speed);
      r_backward(RY);
      ps2x.read_gamepad();
      RY = ps2x.Analog(PSS_RY);
  }
    digitalWrite(L_BRK, HIGH);
    digitalWrite(R_BRK, HIGH);
}

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

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

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

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

void rotateclockwise(unsigned int ly)
{
  l_backward(ly);
  r_forward(ly);
}

void rotateanticlockwise(unsigned int ly)
{
  r_backward(ly);
  l_forward(ly);
}

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

But after modifying it’s not working at all.
I want control left 2 motors using left stick and right side of 2 motor using right stick both independent of each other. When i move left stick up only left side should go forward. when i move both stick up both side should move forward.
Is there any other way to solve this problem without using multiple if condition?Please help me

Arduino is powered with 9V battery.
Motors are powered with external battery using motor driver.

Don't use delay(). See how timing is managed with millis() in several things at a time.

...R

I also tried removing delays in loop. But still not working.

alivebj:
I also tried removing delays in loop. But still not working.

Post your revised code and tell us what it does. "not working" conveys no useful information.

...R