|
基于無線模塊的遙控控制小車
0.jpg (28.08 KB, 下載次數: 137)
下載附件
2018-4-29 00:40 上傳
0.jpg (39.08 KB, 下載次數: 131)
下載附件
2018-4-29 00:41 上傳
手柄按鍵 | | | | 按鍵上或左搖桿上 | | | | 按鍵下或左搖桿下 | | | | 按鍵左或左搖桿左 | | | | 按鍵右或左搖桿右 | | | |
|
|
|
| 紅圓圈 | | | | 粉方塊 | | | | 綠三角 | | | |
|
|
|
| 左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)
2018-4-27 12:13 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
-
0.jpg
(22.85 KB, 下載次數: 177)
下載附件
2018-4-29 00:41 上傳
|