|
基于无线模块的遥控控制小车
手柄按键 | | | | 按键上或左摇杆上 | | | | 按键下或左摇杆下 | | | | 按键左或左摇杆左 | | | | 按键右或左摇杆右 | | | |
|
|
|
| 红圆圈 | | | | 粉方块 | | | | 绿三角 | | | |
|
|
|
| 左1或右1 | | | | 左2或右2 | | | |
|
|
|
| 右摇杆上 | | | | 右摇杆下 | | | | 右摇杆左 | | | | 右摇杆右 | | | |
|
|
|
| 标志位 | | | |
arduino源程序如下:
- #include <PS2X_lib.h> //for v1.6
-
- 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.
- //不支持热插拔,连线后需要重新启动Arduino板
- 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;
- int banduan=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 [url]www*billporter*info[/url] 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 [url]www*billporter*info[/url] for troubleshooting tips");
- //1号错误的串口提示信息
- else if(error == 2)
- Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit [url]www*billporter*info[/url] 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(){
- /* You must Read Gamepad to get new values
- Read GamePad and set vibration values
- ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
- if you don't enable the rumble, use ps2x.read_gamepad(); with no values
-
- you should call this at least once a second
- */
- //用Read Gamepad函数读取按键值
-
-
- if(error == 1) //skip loop if no controller found
- return;
-
-
-
- /******************************************************
- Guitar Hero Controller不是手柄选用的控制器
- //Guitar Hero Controller
- if(type == 2)
- {
- ps2x.read_gamepad(); //read controller
-
- if(ps2x.ButtonPressed(GREEN_FRET))
- Serial.println("Green Fret Pressed");
- if(ps2x.ButtonPressed(RED_FRET))
- Serial.println("Red Fret Pressed");
- if(ps2x.ButtonPressed(YELLOW_FRET))
- Serial.println("Yellow Fret Pressed");
- if(ps2x.ButtonPressed(BLUE_FRET))
- Serial.println("Blue Fret Pressed");
- if(ps2x.ButtonPressed(ORANGE_FRET))
- Serial.println("Orange Fret Pressed");
-
-
- if(ps2x.ButtonPressed(STAR_POWER))
- Serial.println("Star Power Command");
-
- if(ps2x.Button(UP_STRUM)) //will be TRUE as long as button is pressed
- Serial.println("Up Strum");
- if(ps2x.Button(DOWN_STRUM))
- Serial.println("DOWN Strum");
-
-
- if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
- Serial.println("Start is being held");
- if(ps2x.Button(PSB_SELECT))
- Serial.println("Select is being held");
-
-
- if(ps2x.Button(ORANGE_FRET)) // print stick value IF TRUE
- {
- Serial.print("Wammy Bar Position:");
- Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
- }
- }
- **********************************************************************/
- else { //DualShock Controller手柄选用的控制器
- ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
-
- if(ps2x.Button(PSB_START)) //start选中 //will be TRUE as long as button is pressed
- Serial.println("Start is being held");
- if(ps2x.Button(PSB_SELECT)) //select选中
- Serial.println("Select is being held");
-
- 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
- if(PS_RX<5) //从左到右0~255,从上到下0~255
- { banduan=1;
- Serial.print("l");
- }
- if(PS_RX>250)
- { banduan=1;
- Serial.print("m");
- }
- if(PS_RY<5)
- { banduan=1;
- Serial.print("j");
- }
- if(PS_RY>250)
- { banduan=1;
- Serial.print("k");
- }
-
-
-
-
- if(PS_LY<5||ps2x.Button(PSB_PAD_UP)) //上 //will be TRUE as long as button is pressed
- { banduan=1;
- Serial.print("a");
- }
-
- if(PS_LY>250||ps2x.Button(PSB_PAD_DOWN))//下
- { banduan=1;
- Serial.print("b");
- }
-
- if(PS_LX<5||ps2x.Button(PSB_PAD_LEFT))//左
- { banduan=1;
- Serial.print("c");
- }
-
- if(PS_LX>250||ps2x.Button(PSB_PAD_RIGHT))//右
- { banduan=1;
- Serial.print("d");
- }
-
-
- if(ps2x.Button(PSB_RED)) //红圆圈
- { banduan=1;
- Serial.print("e");
- }
-
- if(ps2x.Button(PSB_PINK)) //粉方形
- { banduan=1;
- Serial.print("f");
- }
-
-
-
-
- vibrate = ps2x.Analog(PSAB_BLUE); //this will set the large motor vibrate speed based on how hard you press the blue (X) button
- if(ps2x.NewButtonState(PSB_BLUE)) //蓝叉叉
- Serial.print("XX");
-
- if (ps2x.Button(PSB_GREEN)) //绿三角 //will be TRUE if any button changes state (on to off, or off to on)
- { banduan=1;
- Serial.print("g");
- }
-
-
-
-
-
- if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) //左1右1// print stick values if either is TRUE
- { banduan=1;
- Serial.print("h");
- }
- if(ps2x.Button(PSB_L2) || ps2x.Button(PSB_R2)) //左2右2// print stick values if either is TRUE
- { banduan=1;
- Serial.print("i");
- }
-
- if( banduan==0)
- Serial.print("n");
- else
- banduan=0;
-
- }
- delay(50);
-
- }
复制代码
所有资料51hei提供下载:
遥控小车.rar
(12.07 MB, 下载次数: 155)
|
-
0.jpg
(22.85 KB, 下载次数: 176)
|