极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12042|回复: 3

Arduino PS2控制程序,请帮我看下这个程序,老是报错,新手见谅

[复制链接]
发表于 2016-3-15 11:44:11 | 显示全部楼层 |阅读模式

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2016-3-16 09:25:49 | 显示全部楼层
1. 先确认库的样例运行是否正常
2. 建议贴完整代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-16 15:39:35 | 显示全部楼层
幻生幻灭 发表于 2016-3-16 09:25
1. 先确认库的样例运行是否正常
2. 建议贴完整代码

库的样例代码编译通过,我的PS2连接也正常,现在的问题是,我在用switch case语句时,选择按钮的函数不知道怎么设,我用的read—gamepad,但是报错
回复 支持 反对

使用道具 举报

 楼主| 发表于 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是错误的
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-3 18:41 , Processed in 0.041590 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表