1

I am really new to coding and building stuff, so forgive me if it is anything offensive.i have been trying to convert my PS2 dualshock controller to USB using arduino. there is a library called ps2x that i can use to read the data and Joystick.h library to send it over USB-HID. i just want to know if this is efficient code, if not how can i improve it.

This is the loop:

 
 ps2x.read_gamepad(); //This needs to be called at least once a second
                        // to get data from the controller.
  
  Joystick.setXAxis(ANALOGSTATE1); //Joystick.setButton(-127 to 127); 0 is center
  Joystick.setYAxis(ANALOGSTATE2);
  Joystick.setZAxis(ANALOGSTATE3);
  Joystick.setThrottle(ANALOGSTATE4); //This one is different, (0 to 255) 127 is center
  
  Joystick.setButton(0, DIGITALSTATE1); //Joystick.setButton(0-31, 0-1);
                                        //Joystick.setButton(Select Button to Use, Specify ON/OFF of Button); 
  Joystick.setButton(1, DIGITALSTATE2);
  Joystick.setButton(2, DIGITALSTATE3);       
  Joystick.setButton(3, DIGITALSTATE4);
  Joystick.setButton(4, DIGITALSTATE5);
  Joystick.setButton(5, DIGITALSTATE6);
  Joystick.setButton(6, DIGITALSTATE7);
  Joystick.setButton(7, DIGITALSTATE8);
  Joystick.setButton(8, DIGITALSTATE9);
  Joystick.setButton(9, DIGITALSTATE10);
  Joystick.setButton(10, DIGITALSTATE11);
  Joystick.setButton(11, DIGITALSTATE12);
  Joystick.setButton(12, DIGITALSTATE13);
  Joystick.setButton(13, DIGITALSTATE14);
  Joystick.setButton(14, DIGITALSTATE15);
  Joystick.setButton(15, DIGITALSTATE16);
  
  
if(ps2x.ButtonPressed(PSB_TRIANGLE)) {
  DIGITALSTATE1 = 1;
}
  else if (ps2x.ButtonReleased(PSB_TRIANGLE)){
  DIGITALSTATE1 = 0;
}
if(ps2x.ButtonPressed(PSB_CIRCLE))  
  {
    DIGITALSTATE2 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_CIRCLE)){
    DIGITALSTATE2 = 0;
  }
if(ps2x.ButtonPressed(PSB_CROSS))  
  {
    DIGITALSTATE3 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_CROSS)){
    DIGITALSTATE3 = 0;
  }
if(ps2x.ButtonPressed(PSB_SQUARE))  
  {
    DIGITALSTATE4 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_SQUARE)){
    DIGITALSTATE4 = 0;
  }
if(ps2x.ButtonPressed(PSB_PAD_RIGHT))  
  {
    DIGITALSTATE5 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_PAD_RIGHT)){
    DIGITALSTATE5 = 0;
  }
if(ps2x.ButtonPressed(PSB_PAD_UP))  
  {
    DIGITALSTATE6 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_PAD_UP)){
    DIGITALSTATE6 = 0;
  }
if(ps2x.ButtonPressed(PSB_PAD_LEFT))  
  {
    DIGITALSTATE7 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_PAD_LEFT)){
    DIGITALSTATE7 = 0;,
  }
if(ps2x.ButtonPressed(PSB_PAD_DOWN))  
  {
    DIGITALSTATE8 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_PAD_DOWN)){
    DIGITALSTATE8 = 0;
  }
if(ps2x.ButtonPressed(PSB_L1))  
  {
    DIGITALSTATE9 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_L1)){
    DIGITALSTATE9 = 0;
  }
if(ps2x.ButtonPressed(PSB_L2))  
  {
    DIGITALSTATE10 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_L2)){
    DIGITALSTATE10 = 0;
  }
if(ps2x.ButtonPressed(PSB_R1))  
  {
    DIGITALSTATE11 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_R1)){
    DIGITALSTATE11 = 0;
  }
if(ps2x.ButtonPressed(PSB_R2))  
  {
    DIGITALSTATE12 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_R2)){
    DIGITALSTATE12 = 0;
  }
if(ps2x.ButtonPressed(PSB_L3))  
  {
    DIGITALSTATE13 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_L3)){
    DIGITALSTATE13 = 0;
  }
if(ps2x.ButtonPressed(PSB_R3))  
  {
    DIGITALSTATE14 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_R3)){
    DIGITALSTATE14 = 0;
  }
if(ps2x.ButtonPressed(PSB_START))  
  {
    DIGITALSTATE15 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_START)){
    DIGITALSTATE15 = 0;
  }
if(ps2x.ButtonPressed(PSB_SELECT))  
  {
    DIGITALSTATE16 = 1;
   }
  else if (ps2x.ButtonReleased(PSB_SELECT)){
    DIGITALSTATE16 = 0;
  }

  
  ANALOGSTATE1 = map(ps2x.Analog(PSS_LX), 0, 255, -127, 127);
  ANALOGSTATE2 = map(ps2x.Analog(PSS_LY), 0, 255, -127, 127);
  ANALOGSTATE3 = map(ps2x.Analog(PSS_RX), 0, 255, -127, 127);
  ANALOGSTATE4 = (ps2x.Analog(PSS_RY));
  

}```
4
  • I would move the part where you send the data with Joystick.setButton and stuff to after the part where you parse the data with all those if statements for the obvious reasons. You shouldn't send a message before you write it. But other than that if it's written and it works it's great. It could be shortened with an OOP approach maybe, but that has some costs as well. Anytime you're putting numbers on the ends of variable names what you really want is an array. That might be something worth going and learning about.
    – Delta_G
    Commented Jun 27, 2020 at 3:05
  • you could use ternary statement like DIGITALSTATE1 = (ps2x.ButtonPressed(PSB_TRIANGLE))? 1 : 0; ... or, since true and false are 1 and 0, you could use DIGITALSTATE1 = ps2x.ButtonPressed(PSB_TRIANGLE); ... or Joystick.setButton(0, ps2x.ButtonPressed(PSB_TRIANGLE));
    – jsotola
    Commented Jun 27, 2020 at 22:46
  • you could also put all of the PS2 button names in an array and use a loop for the joystick.setbutton()
    – jsotola
    Commented Jun 27, 2020 at 22:52
  • Thanks! i put the buttons in an array but set the joystick variables separate since i would get confused. it works great. but i guess dpad doesn't function as buttons but as Hat switch. so i got some more coding to do.
    – Hackermann
    Commented Jun 28, 2020 at 5:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.