Combine two separate arduino codes to one

Hello guys.I have two codes and i want it to make it one.I tried myself but it doesn’t work.

First Code

#include <PS3BT.h>

USB Usb;
BTD Btd(&Usb);
PS3BT PS3(&Btd);

int dirA = 2;
int dirB = 4;
int PWMA = 3;
int PWMB = 5;
int motorValue = 0;


void setup()
{
  //Serial.begin(115200);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }
  Serial.print(F("\r\nPS3 Bluetooth Library Started"));
  pinMode(dirA, OUTPUT);
  pinMode(dirB, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);

}

void loop()
{

  Usb.Task();

  if (PS3.PS3Connected || PS3.PS3NavigationConnected)
  {
    int leftHatValue = PS3.getAnalogHat(LeftHatY);
    motorValue = map(leftHatValue, 0, 120, 255, 0);
    motorValue = constrain(motorValue, 0, 255);
    digitalWrite(dirA, HIGH); //Forward
    analogWrite(PWMA, motorValue);
  }

  if (PS3.getButtonClick(PS)) {
    PS3.disconnect();
  }
}

Second Code

#include <PS3BT.h>

USB Usb;
BTD Btd(&Usb);
PS3BT PS3(&Btd);

int dirA = 2;
int dirB = 4;
int PWMA = 3;
int PWMB = 5;
int motorValue = 0;


void setup()
{
  //Serial.begin(115200);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }
  Serial.print(F("\r\nPS3 Bluetooth Library Started"));
  pinMode(dirA, OUTPUT);
  pinMode(dirB, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);

}

void loop()
{

  Usb.Task();

  if (PS3.PS3Connected || PS3.PS3NavigationConnected)
  {
    int leftHatValue = PS3.getAnalogHat(LeftHatY);
    motorValue = map(leftHatValue, 135, 255, 0, 255);
    motorValue = constrain(motorValue, 0, 255);
    digitalWrite(dirA, LOW); //Backrward
    analogWrite(PWMA, motorValue);
  }

  if (PS3.getButtonClick(PS)) {
    PS3.disconnect();
  }
}

combine code

#include <PS3BT.h>

USB Usb;
BTD Btd(&Usb);
PS3BT PS3(&Btd);

int dirA = 2;
int dirB = 4;
int PWMA = 3;
int PWMB = 5;
int motorValue = 0;


void setup()
{
  //Serial.begin(115200);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }
  Serial.print(F("\r\nPS3 Bluetooth Library Started"));
  pinMode(dirA, OUTPUT);
  pinMode(dirB, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);

}

void loop()
{

  Usb.Task();

  if (PS3.PS3Connected || PS3.PS3NavigationConnected)
  {
    int leftHatValue = PS3.getAnalogHat(LeftHatY);
    motorValue = map(leftHatValue, 0, 120, 255, 0);
    motorValue = constrain(motorValue, 0, 255);
    digitalWrite(dirA, HIGH);
    analogWrite(PWMA, motorValue);
  }

  if (int leftHatValue = PS3.getAnalogHat(LeftHatY))
  {
    motorValue = map(leftHatValue, 135, 255, 0, 255);
    motorValue = constrain(motorValue, 0, 255);
    digitalWrite(dirA, LOW);
    analogWrite(PWMA, motorValue);
  }


  if (PS3.getButtonClick(PS)) {
    PS3.disconnect();
  }
}

Can you help me please.What am i doing wrong?Thank you for your time guys.

What happens when you run the combined code that makes you say that it does not work? What should it do?

jbellavance:
What happens when you run the combined code that makes you say that it does not work? What should it do?

Hello sir, thank you for your reply.When i run the combined code the dc motor that i try to control make a noise.
if i run the code separately works fine.I want to control the speed of the dc motor with left joystick of PS3.

Combining Arduino Sketches
This is often difficult. Here is an approach that may help you be successful:

http://arduino-info.wikispaces.com/CombiningArduinoSketches