Home Forums EasyTransfer Library Support Forum help with transmit and receive data from PS2 controller Reply To: help with transmit and receive data from PS2 controller

#2140
Anonymous
Inactive

the code i’m using, but doesn’t working

Help me guys! i’m getting crazy here!

 

for TX

#include <EasyTransfer.h>

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

#include <Servo.h>

 

Servo myservo;

int pos =90;

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 conect the controller,

//or call config_gamepad(pins) again after connecting the controller.

int error = 0;

byte type = 0;

byte vibrate = 0;

 

//create object

EasyTransfer ET;

 

struct SEND_DATA_STRUCTURE{

//put your variable definitions here for the data you want to send

//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO

byte PSX_LY; //PSX Left Joystick Y axis data

byte PSX_LX; //PSX Left Joystick Y axis data

byte PSX_RY; //PSX Left Joystick Y axis data

byte PSX_RX; //PSX Left Joystick Y axis data

 

};

 

//give a name to the group of data

SEND_DATA_STRUCTURE mydata;

 

void setup(){

Serial.begin(115200);

myservo.attach(10);

mydata.PSX_LY = 0;

mydata.PSX_LX = 0;

mydata.PSX_RY = 0;

mydata.PSX_RX = 0;

 

//mydata.mode = -1; //start with mode = 0

error = ps2x.config_gamepad(5,7,6,4, false, false);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error

 

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, see readme.txt to enable debug. visit http://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 http://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.println(“Unknown Controller type”);

break;

case 1:

Serial.println(“DualShock Controller Found”);

break;

case 2:

Serial.println(“GuitarHero Controller Found”);

break;

}//END PSX SETUP

//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.

ET.begin(details(mydata), &Serial);

pinMode(13, OUTPUT);

randomSeed(analogRead(0));

}

 

 

void loop(){

ps2x.read_gamepad();          //read controller

//will be TRUE if button was JUST pressed

{

 

}

mydata.PSX_LY = ps2x.Analog(PSS_LY); //store the different joystick values into the

mydata.PSX_LX = ps2x.Analog(PSS_LX);

mydata.PSX_RY = ps2x.Analog(PSS_RY);

mydata.PSX_RY = ps2x.Analog(PSS_RX);

ET.sendData();

if(ps2x.Analog(PSS_LY)){

myservo.write(pos);

delay(15);

ps2x.read_gamepad(false,vibrate);

pos = map(ps2x.Analog(PSS_LY),255,0,0,180);

}

delay (50);

}

 

 

 

 

 

now  for RX (where i think it’s wrong)!!!!!!!

#include <Servo.h>

#include <EasyTransfer.h>

#include <PS2X_lib.h>

 

Servo myservo;

int pos =90;

PS2X ps2x;

//create object

EasyTransfer ET;

 

//BEGIN EASYTRANSFER

struct RECEIVE_DATA_STRUCTURE{

//put your variable definitions here for the data you want to receive

//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO

byte PSX_LY; //PSX Left Joystick Y axis data

byte PSX_LX; //PSX Left Joystick Y axis data

byte PSX_RY; //PSX Left Joystick Y axis data

byte PSX_RX; //PSX Left Joystick Y axis data

 

};

 

//give a name to the group of data

RECEIVE_DATA_STRUCTURE mydata;

 

void setup(){

Serial.begin(115200);

myservo.attach(10);

//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.

ET.begin(details(mydata), &Serial);

pinMode(13, OUTPUT);

randomSeed(analogRead(0));

}

 

void loop(){

//check and see if a data packet has come in.

if(ET.receiveData()){

if(ps2x.Analog(PSS_LY)){

myservo.write(pos);

delay(15);

pos = map(ps2x.Analog(PSS_LY),255,0,0,180);

}

delay (50);

}

}