Carrinho de controle remoto PS2

Carrinho de controle remoto baseado no site https://acoptex.com/project/299/basics-project-040c-how-to-build-the-4wd-smart-robot-car-part-3-wieless-ps2-controller-l298n-h-at-lex-c/

lib necessária (link) | programa base (link)

/*
ALTERADO DE:  
Project: 4WD robot car, L298N dual h-bridge motor driver module, 4 DC motors withor without speed control (constant maximum speed),wireless PS2 controller
  Function: 4WD robot car movements controlled with wireless PS2 controller
  Email: info@acoptex.com
  Website: http://acoptex.com
*/
//************************************************************
#include <PS2X_lib.h>//include library code
//************************************************************
const int PS2_CLK = 8;//clock
const int PS2_CMD = 12;//command
const int PS2_ATT = 11;//attention
const int PS2_DAT = 13;//data
/*
  Select modes of PS2 controller:
  - pressures = analog reading of push-butttons
  - rumble    = motor rumbling
  uncomment 1 of the lines for each mode selection
*/
//const int rumble = false;
//const int pressures = false;
const int pressures = true;
const int rumble = true;
PS2X ps2x; //create PS2 Controller object
byte PS2 = 0;
int fspeed;//forward speed
//DC motors 3 and 4:
const int in1 = 4;//motor direction control port
const int in2 = 5;//motor direction control port
const int enA = 9;//motor speed control
//DC motors 1 and 2:
const int in3 = 6;//motor direction control port
const int in4 = 7;//motor direction control port
const int enB = 10;//motor speed control
//************************************************************
void setup() {
  PS2 = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_ATT, PS2_DAT, pressures, rumble);
  //sets all DC motors pins as OUTPUTs
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  //pinMode(8, OUTPUT);    //Forward lights
  //pinMode(2, OUTPUT);    //Back lights
  pinMode(15, OUTPUT);   //Left turn
  pinMode(14, OUTPUT);   //Right turn
  //pinMode(A2, OUTPUT);   //Buzzer
}
void loop() {
  ps2x.read_gamepad();
  if (ps2x.Analog(PSS_LY) == 128) analogWrite(enA, 0);
  if (ps2x.Analog(PSS_LY) == 128) analogWrite(enB, 0);
//if(ps2x.ButtonPressed(PSB_L1)) digitalWrite(8, HIGH);//Turn on forward lights
//if(ps2x.ButtonPressed(PSB_L2)) digitalWrite(8, LOW);//Turn off forward lights
//if(ps2x.ButtonPressed(PSB_R1)) digitalWrite(A2, HIGH);//Turn on buzzer
//if(ps2x.ButtonPressed(PSB_R2)) digitalWrite(A2, LOW);//Turn off buzzer
  if (ps2x.Analog(PSS_LY) > 128) { //MOVE BACK
    fspeed = map(ps2x.Analog(PSS_LY), 129, 255, 0, 255);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    //digitalWrite(2, HIGH);
    digitalWrite(15, LOW);
    digitalWrite(14, LOW);
    analogWrite(enA, fspeed);
    analogWrite(enB, fspeed);
  }
  if (ps2x.Analog(PSS_LY) < 128) { //MOVE FWD
    fspeed = map(ps2x.Analog(PSS_LY), 0, 127, 255, 0);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    //digitalWrite(2, LOW);
    digitalWrite(15, LOW);
    digitalWrite(14, LOW);
    analogWrite(enA, fspeed);
    analogWrite(enB, fspeed);
  }
  if (ps2x.Analog(PSS_LX) < 128) { //TURN LEFT
    fspeed = map(ps2x.Analog(PSS_LX), 0, 127, 255, 0);
    digitalWrite(15, HIGH);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    analogWrite(enA, 0);
    analogWrite(enB, fspeed);
  }
  if (ps2x.Analog(PSS_LX) > 128) { //TURN RIGHT
    fspeed = map(ps2x.Analog(PSS_LX), 129, 255, 0, 255);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(14, HIGH);
    analogWrite(enA, fspeed);
    analogWrite(enB, 0);
  }
  if (ps2x.Analog(PSS_RX) > 128) { //TURN HARD RIGHT
    fspeed = map(ps2x.Analog(PSS_RX), 129, 255, 0, 255);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    digitalWrite(14, HIGH);
    analogWrite(enA, fspeed);
    analogWrite(enB, fspeed);
  }
  if (ps2x.Analog(PSS_RX) < 128) { //TURN HARD LEFT
    fspeed = map(ps2x.Analog(PSS_RX), 0, 127, 255, 0);
    digitalWrite(15, HIGH);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    analogWrite(enA, fspeed);
    analogWrite(enB, fspeed);
  }
  if ((ps2x.Analog(PSS_RY) > 128) && (ps2x.Analog(PSS_RX) < 128)) { //TURN BACK LEFT
    fspeed = map(ps2x.Analog(PSS_RY), 129, 255, 0, 255);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    digitalWrite(15, HIGH);
    //digitalWrite(2, HIGH);
    analogWrite(enA, 0);
    analogWrite(enB, fspeed);
  }
  if ((ps2x.Analog(PSS_RY) > 128) && (ps2x.Analog(PSS_RX) > 128)) { //TURN BACK RIGHT
    fspeed = map(ps2x.Analog(PSS_RY), 129, 255, 0, 255);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(14, HIGH);
    //digitalWrite(2, HIGH);
    analogWrite(enA, fspeed);
    analogWrite(enB, 0);
  }
  delay(50);
}