Need help programming a controller in arduino

Hi, me and my team are new to the arduino programming language. We are tying to run the test the connection between the robotshop.com/en/lynxmotion-ps2-controller-v4.html
and the arduino Mega 2560 by using the [font=Verdana][highlight=#ffffff]Bill Porter’s PS2 library. [/highlight][/font]
[font=Verdana][highlight=#ffffff]When ever we try it we keep getting the errors:[/highlight][/font]
[font=Verdana][highlight=#ffffff]
[font=Verdana, Arial, sans-serif, Tahoma]In member function ‘void PS2X::read_gamepad(boolean, byte)’:[/font][font=Verdana, Arial, sans-serif, Tahoma][/font][font=Verdana, Arial, sans-serif, Tahoma]and[/font][font=Verdana, Arial, sans-serif, Tahoma][/font][font=Verdana, Arial, sans-serif, Tahoma]warning: narrowing conversion of ‘motor2’ from ‘byte {aka unsigned char}’ to ‘char’ inside { } -Wnarrowing][/font][font=Verdana, Arial, sans-serif, Tahoma][/font][font=Verdana, Arial, sans-serif, Tahoma]char dword[9] = {0x01,0x42,0,motor1,motor2,0,0,0,0};[/font]
[/highlight][/font]
So any arduino veterans out there can you help explain what are problem is.

Sample program:

[code]#include <PS2X_lib.h>
PS2X PS2X;
int error = 0;
byte type = 0;
byte vibrate = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
error = PS2X.config_gamepad(29,25,27,23, true, true); //(clock, command, attention, data, Pressures?, Rumble?)
if(error == 0){
Serial.println(“Found Controller, configured successful”);
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(“Go to http://www.billporter.info for updates and to report bugs.”);
}
else if(error == 1)
Serial.println(“No controller found, check wiring”);
else if(error == 2)
Serial.println(“Controller found but not accepting commands.”);
else if(error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
type = PS2X.readType();
switch(type) {
case 0:
Serial.println(“Unknown Controller type”);
break;
case 1:
Serial.println(“DualShock Controller Found”);
break;
}
}

void loop(){
if(error == 1)
return;
else if(type==2){
PS2X.read_gamepad(false, vibrate);
if(PS2X.Button(PSB_START))
Serial.println(“Start is being held”);
if(PS2X.Button(PSB_SELECT))
Serial.println(“Select is being held”);
if(PS2X.Button(PSB_PAD_UP)) {
Serial.print("Up held this hard: ");
Serial.println(PS2X.Analog(PSAB_PAD_UP), DEC);
}
if(PS2X.Button(PSB_PAD_RIGHT)){
Serial.print(“Right held this hard: “);
Serial.println(PS2X.Analog(PSAB_PAD_RIGHT), DEC);
}
if(PS2X.Button(PSB_PAD_LEFT)){
Serial.print(“LEFT held this hard: “);
Serial.println(PS2X.Analog(PSAB_PAD_LEFT), DEC);
}
if(PS2X.Button(PSB_PAD_DOWN)){
Serial.print(“DOWN held this hard: “);
Serial.println(PS2X.Analog(PSAB_PAD_DOWN), DEC);
}
else{
Serial.print(“ERROR, no Dualshock controoler found”);
Serial.end();
}
vibrate = PS2X.Analog(PSAB_BLUE);
if (PS2X.NewButtonState()) {
if(PS2X.Button(PSB_L3))
Serial.println(“L3 pressed”);
if(PS2X.Button(PSB_R3))
Serial.println(“R3 pressed”);
if(PS2X.Button(PSB_L2))
Serial.println(“L2 pressed”);
if(PS2X.Button(PSB_R2))
Serial.println(“R2 pressed”);
if(PS2X.Button(PSB_GREEN))
Serial.println(“Triangle pressed”);
}
if(PS2X.ButtonPressed(PSB_RED))
Serial.println(“Circle just pressed”);
if(PS2X.ButtonReleased(PSB_PINK))
Serial.println(“Square just released”);
if(PS2X.NewButtonState(PSB_BLUE))
Serial.println(“X just changed”);
if(PS2X.Button(PSB_L1) || PS2X.Button(PSB_R1))
{
Serial.print(“Stick Values:”);
Serial.print(PS2X.Analog(PSS_LY), DEC);
Serial.print(”,”);
Serial.print(PS2X.Analog(PSS_LX), DEC);
Serial.print(”,”);
Serial.print(PS2X.Analog(PSS_RY), DEC);
Serial.print(”,”);
Serial.println(PS2X.Analog(PSS_RX), DEC);
}
}
delay(50);
}[/code]

^

Hi,

We tried your code as-is and it compiled fine on Arduino 1.6.12 (at this time, the latest versions is 1.8.1) for a Arduino/Genuino Mega or Mega 2560. In the future, we recommend you add your code as an attachment (zip) instead of pasting it in the post. It makes it easier for the community to help.

That being said, the first block of your code has a potential issue in it:

#include <PS2X_lib.h> PS2X PS2X; int error = 0; byte type = 0; byte vibrate = 0;
In this block of code, the variable (object pointer) PS2X is capitalized the same as the class name PS2X. As you may notice from the example in the library, this is not the case normally. It may indirectly be the cause of your issue.

Please find attached a copy of the code (as tried) and the libraries version we used for PS2X. You can extract the code (it is in a folder already) anywhere, but the library needs to be extracted in your libraries folder of your Arduino folder, usually in your Documents (actual location varies per OS/version/setup). Make sure to remove the folder of the current PS2X library you are using first.
Let us know if it compiles using these.

Also, which version of the Arduino IDE are you using?

Sincerely,
2017-01-17 - PS2X_lib.zip (16.4 KB)
2017-01-17 - PS2X_Customer_Example.zip (1.36 KB)

I tried the code and it complied, uploaded and executes fine for me (using the attached library). Unfortunately the result is “No controller found” when using the PIN selection in the attached example. The receiver lights up with power and syncs with the remote, but the Mega reports no connection.

error = ps2x.config_gamepad(29,25,27,23, true, true); //(clock, command, attention, data, Pressures?, Rumble?)

Are there any troubleshooting suggestions for my wiring? I’ve pulled apart and reassembled everything a dozen times and still nothing. I had purchased and ran this successfully on a BotBoarduino, but I’m unable to use it since it won’t work with certain shields, so I figured I would try the Mega instead. Are there wiring examples I could reference for connecting a Mega? I found an old thread on the RB forums but the picture had been removed/lost.

A good step at this point would be for you to wire everything up and take one or more pictures clearly showing all components (Mega, adapter board, wires, power sources, etc.). Post these in your reply using Attachments (in Full Editor mode).

We also recommend that you modify one line in the PS2X library: github.com/Lynxmotion/Arduino-PS2X/blob/master/PS2X_lib/PS2X_lib.h#L79
Simply uncomment this line to activate the debug mode, which will provide more information.

Most likely, there is either a wiring or a timing issue which prevents your Mega from discovering the proper ID for the controller (the library only supports a few IDs officially). Also post a copy of the message received when running the example with the uncomment debug #define in your reply.

Sincerely,