The second motor on my ps2 controlled car is not working correctly

Basically, I'm constructing a 4x4 car with the left joystick controlling the two left motors and the right joystick controlling the two right motors. For the time being, I'm only utilizing one left and right motor, each with its own BTS 7960b speed controller, and the right motor isn't operating properly. For unclear reasons, I even fried two right-speed controllers. I know the issue is with the code because I tested it without the joysticks and everything worked great. When I try to operate the motors using the joystick, though, all of the problems reappear. I'm working with the [Robojax Library] (https://robojax.com/control-multiple-dc-motors-using-bst7960-module-arduino) and the [PS2xlibrary] (GitHub - madsci1016/Arduino-PS2X: Read a Playstation 2 Gamepad or Guitar Hero Controller using an Arduino). If anyone has any suggestions about what to do, I'd appreciate it.

#include <RobojaxBTS7960.h>

#include <PS2X_lib.h>  //for v1.6

/******************************************************************
 * set pins connected to PS2 controller:
 *   - 1e column: original 
 *   - 2e colmun: Stef?
 * replace pin numbers by the ones you use
 ******************************************************************/
#define PS2_DAT        22  //14    
#define PS2_CMD        23  //15
#define PS2_SEL        24  //16
#define PS2_CLK        25  //17

#define RPWM 3 // define pin 3 for RPWM pin (output)
#define R_EN 40 // define pin 2 for R_EN pin (input)
#define R_IS 42 // define pin 5 for R_IS pin (output)

#define LPWM 2 // define pin 6 for LPWM pin (output)
#define L_EN 41 // define pin 7 for L_EN pin (input)
#define L_IS 43 // define pin 8 for L_IS pin (output)



#define RPWM_2 6 // define pin 3 for RPWM pin (output)
#define R_EN_2 32 // define pin 2 for R_EN pin (input)
#define R_IS_2 34// define pin 5 for R_IS pin (output)

#define LPWM_2 5 // define pin 6 for LPWM pin (output)
#define L_EN_2 33 // define pin 7 for L_EN pin (input)
#define L_IS_2 35 // define pin 8 for L_IS pin (output)

#define CW 1 //do not change
#define CCW 0 //do not change
#define debug 1

RobojaxBTS7960 motor1(R_EN,RPWM,R_IS, L_EN,LPWM,L_IS,debug);
RobojaxBTS7960 motor2(R_EN_2,RPWM_2,R_IS_2,L_EN_2,LPWM_2,L_IS_2,debug);


/******************************************************************
 * select modes of PS2 controller:
 *   - pressures = analog reading of push-butttons 
 *   - rumble    = motor rumbling
 * uncomment 1 of the lines for each mode selection
 ******************************************************************/
//#define pressures   true
#define pressures   false
//#define rumble      true
#define rumble      false

PS2X ps2x; // create PS2 Controller Class

//right now, the library does NOT support hot pluggable controllers, meaning 
//you must always either restart your Arduino after you connect the controller, 
//or call config_gamepad(pins) again after connecting the controller.

int error = 0;
byte type = 0;
byte vibrate = 0;
int tax=0;
int tax2=0;
void setup(){

    motor1.begin();
    motor2.begin();
  Serial.begin(9600);
  
  delay(300);  //added delay to give wireless ps2 module some time to startup, before configuring it
   
  //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
  
  //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
  
  if(error == 0){
    Serial.print("Found Controller, configured successful ");
    Serial.print("pressures = ");
  if (pressures)
    Serial.println("true ");
  else
    Serial.println("false");
  Serial.print("rumble = ");
  if (rumble)
    Serial.println("true)");
  else
    Serial.println("false");
    Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
    Serial.println("holding L1 or R1 will print out the analog stick values.");
    Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
  }  
  else if(error == 1)
    Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
   
  else if(error == 2)
    Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");

  else if(error == 3)
    Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
  
//  Serial.print(ps2x.Analog(1), HEX);
  
  type = ps2x.readType(); 
  switch(type) {
    case 0:
      Serial.print("Unknown Controller type found ");
      break;
    case 1:
      Serial.print("DualShock Controller found ");
      break;
  case 2:
      Serial.print("Wireless Sony DualShock Controller found ");
      break;
   }
}

void loop() {

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

if(ps2x.Analog(PSS_LY)==128)
  motor1.stop();
if(ps2x.Analog(PSS_LY)<128){
  tax = map((ps2x.Analog(PSS_LY)),128,0,0,100);
  motor1.rotate(tax,CW);
}
if(ps2x.Analog(PSS_LY)>128){
  tax = map((ps2x.Analog(PSS_LY)),128,255,0,100);
  motor1.rotate(tax,CCW);
  }
if(ps2x.Analog(PSS_RY)==128)
  motor2.stop();
if(ps2x.Analog(PSS_RY)<128){
  tax2 = map((ps2x.Analog(PSS_RY)),128,0,0,100);
  motor2.rotate(tax2,CW); 
  } 
if(ps2x.Analog(PSS_RY)>128){
  tax2 = map((ps2x.Analog(PSS_RY)),128,255,0,100);
  motor2.rotate(tax2,CCW);
}
  
}

It's not easy to imagine how incorrect code could cause that. Please post a schematic!

Can you please click Tools-->Auto Format and re-post your code? It's entirely possible you have some { or } in the wrong places, leading to different operation of the left and right motor drivers. Auto Format should make any such errors easier to spot. Unless you're talking about python code, where indentation affects how the code runs, in most languages, indentation does not affect how the code runs, but it does affect human programmers, causing them to make and then fail to spot errors.

A link to the motors would help to check up why drivers have been lost.

It is very unclear to me how you have this wired or powered. Please post a schematic, not a frizzy picture of how you have it wired showing all power and ground connections. Also post links to each of the hardware items that show technical information.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.