|
楼主 |
发表于 2016-3-16 16:53:21
|
显示全部楼层
幻生幻灭 发表于 2016-3-16 09:25
1. 先确认库的样例运行是否正常
2. 建议贴完整代码
我的完整代码:
#include <PS2X_lib.h> //for v1.6
#define IN1 5
#define IN2 6
#define IN3 7
#define IN4 8
#define IN5 9
#define IN6 10
#define IN7 11
#define IN8 12
#define IN9 13
#define IN10 14
#define IN11 15
#define IN12 16
#define IN13 17
#define IN14 18
#define IN15 19
#define IN16 20
PS2X ps2x; // create PS2 Controller Class
int PS_LX;//PS2手柄左摇杆X轴数据
int PS_LY;//PS2手柄左摇杆Y轴数据
int PS_RX;//PS2手柄右摇杆X轴数据
int PS_RY;//PS2手柄右摇杆Y轴数据
int error = 0; //连接正确与否的判断标志
byte type = 0; //ps2x.readType()转换判别标志
byte vibrate = 0;
byte panduan=0;
void setup(){
Serial.begin(57600);
//CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
error = ps2x.config_gamepad(13,11,10,12, true, true); //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 www.billporter.info for updates and to report bugs.");
} //0号错误的串口提示信息
else if(error == 1)
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
//1号错误的串口提示信息
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");
//2号错误的串口提示信息
else if(error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
//3号错误的串口提示信息
//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;
}
}
void loop(){
//用Read Gamepad函数读取按键值
if(error == 1) //skip loop if no controller found
return;
if(type == 2){ //Guitar Hero Controller
return;
}
else { //DualShock Controller手柄选用的控制器
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
PS_LX=ps2x.Analog(PSS_LX); //把PS2手柄左摇杆X轴数据读到变量PS_LX
PS_RX=ps2x.Analog(PSS_RX); //把PS2手柄右摇杆X轴数据读到变量PS_RX
PS_LY=ps2x.Analog(PSS_LY); //把PS2手柄左摇杆Y轴数据读到变量PS_LY
PS_RY=ps2x.Analog(PSS_RY); //把PS2手柄右摇杆Y轴数据读到变量PS_RY
panduan=ps2x.read_gamepad();
switch(panduan){
case LEFT:
run1;
break;
case UP:
run2;
break;
case RIGHT:
run3;
break;
default:
Serial.println("别乱按");
}
}
delay(50);
}
标红的语句错误,按键函数我不知道是哪个,用的read—gamepad是错误的 |
|