久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 16233|回復: 11
打印 上一主題 下一主題
收起左側

arduino ps2遙控小車基于無線模塊

  [復制鏈接]
跳轉到指定樓層
樓主
基于無線模塊的遙控控制小車


  
手柄按鍵
  
發送字符
接收十六進制代碼
小車動作
  
按鍵上或左搖桿上
  
a
0x61
前進
  
按鍵下或左搖桿下
  
b
0x62
后退
  
按鍵左或左搖桿左
  
c
0x63
左轉
  
按鍵右或左搖桿右
  
d
0x64
右轉
  
  



  
紅圓圈
  
e
0x65
左原地轉圈
  
粉方塊
  
f
0x66
右原地轉圈
  
綠三角
  
g
0x67
劃弧度
  
  



  
1或右1
  
h
0x68
左平移
  
2或右2
  
i
0x69
右平移
  
  



  
右搖桿上
  
g
0x6a
45度左前
  
右搖桿下
  
k
0x6b
45度右后
  
右搖桿左
  
l
0x6c
45度右前
  
右搖桿右
  
m
0x6d
45度左后
  
  



  
標志位
  
n
0x6e
停止

arduino源程序如下:
  1. #include <PS2X_lib.h>  //for v1.6

  2. PS2X ps2x; // create PS2 Controller Class

  3. //right now, the library does NOT support hot pluggable controllers, meaning
  4. //you must always either restart your Arduino after you conect the controller,
  5. //or call config_gamepad(pins) again after connecting the controller.
  6. //不支持熱插拔,連線后需要重新啟動Arduino板

  7. int PS_LX;//PS2手柄左搖桿X軸數據
  8. int PS_LY;//PS2手柄左搖桿Y軸數據
  9. int PS_RX;//PS2手柄右搖桿X軸數據
  10. int PS_RY;//PS2手柄右搖桿Y軸數據

  11. int error = 0;         //連接正確與否的判斷標志
  12. byte type = 0;         //ps2x.readType()轉換判別標志     
  13. byte vibrate = 0;
  14. int banduan=0;

  15. void setup(){
  16. Serial.begin(57600);

  17. //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************

  18. error = ps2x.config_gamepad(13,11,10,12, true, true);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  19.                                                          //檢查引腳是否有連接錯誤
  20.                                                          
  21. if(error == 0){
  22.    Serial.println("Found Controller, configured successful");
  23.    Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
  24.   Serial.println("holding L1 or R1 will print out the analog stick values.");
  25.   Serial.println("Go to [url]www*billporter*info[/url] for updates and to report bugs.");
  26. }  //0號錯誤的串口提示信息                        

  27.   else if(error == 1)
  28.    Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit [url]www*billporter*info[/url] for troubleshooting tips");
  29.                   //1號錯誤的串口提示信息
  30.   else if(error == 2)
  31.    Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit [url]www*billporter*info[/url] for troubleshooting tips");
  32.                  //2號錯誤的串口提示信息
  33.   else if(error == 3)
  34.    Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
  35.                  //3號錯誤的串口提示信息

  36.    //Serial.print(ps2x.Analog(1), HEX);

  37.    type = ps2x.readType();    //正確連接后串口提示的信息  
  38.      switch(type) {
  39.        case 0:
  40.         Serial.println("Unknown Controller type");
  41.        break;
  42.        case 1:
  43.         Serial.println("DualShock Controller Found");
  44.        break;
  45.        case 2:
  46.          Serial.println("GuitarHero Controller Found");
  47.        break;
  48.      }

  49. }

  50. void loop(){
  51.    /* You must Read Gamepad to get new values
  52.    Read GamePad and set vibration values
  53.    ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
  54.    if you don't enable the rumble, use ps2x.read_gamepad(); with no values
  55.    
  56.    you should call this at least once a second
  57.    */
  58. //用Read Gamepad函數讀取按鍵值


  59. if(error == 1) //skip loop if no controller found
  60.   return;
  61.   
  62.   
  63.   
  64.   /******************************************************
  65.    Guitar Hero Controller不是手柄選用的控制器
  66.   //Guitar Hero Controller
  67. if(type == 2)            
  68. {
  69.   ps2x.read_gamepad();          //read controller

  70.    if(ps2x.ButtonPressed(GREEN_FRET))
  71.      Serial.println("Green Fret Pressed");
  72.    if(ps2x.ButtonPressed(RED_FRET))
  73.      Serial.println("Red Fret Pressed");
  74.    if(ps2x.ButtonPressed(YELLOW_FRET))
  75.      Serial.println("Yellow Fret Pressed");
  76.    if(ps2x.ButtonPressed(BLUE_FRET))
  77.      Serial.println("Blue Fret Pressed");
  78.    if(ps2x.ButtonPressed(ORANGE_FRET))
  79.      Serial.println("Orange Fret Pressed");


  80.     if(ps2x.ButtonPressed(STAR_POWER))
  81.      Serial.println("Star Power Command");

  82.     if(ps2x.Button(UP_STRUM))          //will be TRUE as long as button is pressed
  83.      Serial.println("Up Strum");
  84.     if(ps2x.Button(DOWN_STRUM))
  85.      Serial.println("DOWN Strum");


  86.     if(ps2x.Button(PSB_START))                            //will be TRUE as long as button is pressed
  87.          Serial.println("Start is being held");
  88.     if(ps2x.Button(PSB_SELECT))                  
  89.          Serial.println("Select is being held");


  90.     if(ps2x.Button(ORANGE_FRET)) // print stick value IF TRUE
  91.     {
  92.         Serial.print("Wammy Bar Position:");
  93.         Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
  94.     }
  95. }
  96. **********************************************************************/
  97. else { //DualShock Controller手柄選用的控制器
  98.     ps2x.read_gamepad(false, vibrate);          //read controller and set large motor to spin at 'vibrate' speed
  99.    
  100.     if(ps2x.Button(PSB_START))                      //start選中 //will be TRUE as long as button is pressed
  101.       Serial.println("Start is being held");   
  102.     if(ps2x.Button(PSB_SELECT))                     //select選中
  103.       Serial.println("Select is being held");

  104.     PS_LX=ps2x.Analog(PSS_LX);           //把PS2手柄左搖桿X軸數據讀到變量PS_LX
  105.    PS_RX=ps2x.Analog(PSS_RX);           //把PS2手柄右搖桿X軸數據讀到變量PS_RX
  106.    PS_LY=ps2x.Analog(PSS_LY);           //把PS2手柄左搖桿Y軸數據讀到變量PS_LY
  107.    PS_RY=ps2x.Analog(PSS_RY);           //把PS2手柄右搖桿Y軸數據讀到變量PS_RY
  108.   if(PS_RX<5)                  //從左到右0~255,從上到下0~255      
  109.   { banduan=1;
  110.         Serial.print("l");  
  111.       }
  112.    if(PS_RX>250)
  113.    { banduan=1;
  114.         Serial.print("m");  
  115.       }
  116.     if(PS_RY<5)
  117.   { banduan=1;
  118.         Serial.print("j");  
  119.       }
  120.       if(PS_RY>250)
  121.   { banduan=1;
  122.         Serial.print("k");  
  123.       }




  124.     if(PS_LY<5||ps2x.Button(PSB_PAD_UP)) //上                   //will be TRUE as long as button is pressed
  125.     {  banduan=1;
  126.        Serial.print("a");               
  127.      }
  128.      
  129.      if(PS_LY>250||ps2x.Button(PSB_PAD_DOWN))//下
  130.       { banduan=1;
  131.        Serial.print("b");     
  132.       }
  133.      
  134.          if(PS_LX<5||ps2x.Button(PSB_PAD_LEFT))//左
  135.       { banduan=1;
  136.       Serial.print("c");   
  137.       }
  138.       
  139.      if(PS_LX>250||ps2x.Button(PSB_PAD_RIGHT))//右
  140.      { banduan=1;
  141.         Serial.print("d");  
  142.       }
  143.       
  144.      
  145.      if(ps2x.Button(PSB_RED))               //紅圓圈
  146.          { banduan=1;
  147.         Serial.print("e");  
  148.       }

  149.     if(ps2x.Button(PSB_PINK))             //粉方形
  150.        { banduan=1;
  151.         Serial.print("f");  
  152.       }
  153.   
  154.       
  155.       
  156.       
  157.       vibrate = ps2x.Analog(PSAB_BLUE);        //this will set the large motor vibrate speed based on how hard you press the blue (X) button   
  158.      if(ps2x.NewButtonState(PSB_BLUE))        //藍叉叉
  159.         Serial.print("XX");                                         

  160.     if (ps2x.Button(PSB_GREEN))              //綠三角   //will be TRUE if any button changes state (on to off, or off to on)
  161.      { banduan=1;
  162.         Serial.print("g");  
  163.       }


  164.   


  165.     if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) //左1右1// print stick values if either is TRUE
  166.      { banduan=1;
  167.         Serial.print("h");  
  168.       }
  169.    if(ps2x.Button(PSB_L2) || ps2x.Button(PSB_R2)) //左2右2// print stick values if either is TRUE
  170.      { banduan=1;
  171.         Serial.print("i");  
  172.       }


  173.   if( banduan==0)
  174.       Serial.print("n");
  175.      else
  176.          banduan=0;
  177.      
  178. }
  179. delay(50);

  180. }
復制代碼


所有資料51hei提供下載:
遙控小車.rar (12.07 MB, 下載次數: 155)


0.jpg (22.85 KB, 下載次數: 177)

0.jpg
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏5 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:18156 發表于 2018-9-9 16:29 | 只看該作者
看輪子應該是可以橫向運動的。
回復

使用道具 舉報

板凳
ID:32574 發表于 2018-10-17 14:13 | 只看該作者
樓主厲害,輪子是叫什么名字?
回復

使用道具 舉報

地板
ID:79544 發表于 2018-10-19 08:25 | 只看該作者
樓主  接收是用的什么模塊?
回復

使用道具 舉報

5#
ID:390585 發表于 2018-11-8 19:06 | 只看該作者
redtxd 發表于 2018-10-17 14:13
樓主厲害,輪子是叫什么名字?

麥克納姆輪
回復

使用道具 舉報

6#
ID:460190 發表于 2019-2-15 21:00 | 只看該作者
感謝分享
回復

使用道具 舉報

7#
ID:448145 發表于 2019-11-7 12:22 | 只看該作者

感謝分享
回復

使用道具 舉報

8#
ID:676603 發表于 2019-12-29 15:03 | 只看該作者
很棒,一會研究源碼
回復

使用道具 舉報

9#
ID:646222 發表于 2020-2-5 19:14 | 只看該作者
感謝分享
回復

使用道具 舉報

10#
ID:652804 發表于 2020-2-25 21:37 | 只看該作者
感謝分享
回復

使用道具 舉報

11#
ID:643484 發表于 2020-3-3 15:07 | 只看該作者
感謝分享
回復

使用道具 舉報

12#
ID:171746 發表于 2021-1-26 16:57 | 只看該作者
有ARDUINO 小車程序嗎?     哪個是51單片機的
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 久久乐国产精品 | 成人深夜福利 | 毛片一区二区三区 | 国内自拍视频在线观看 | 精品久久国产 | 亚洲一区视频在线播放 | 91精品一区 | 91精品国产一区二区三区 | 日日夜夜天天 | 精品美女视频在线观看免费软件 | www..99re| 午夜视频在线播放 | 亚洲伊人精品酒店 | 成人av在线网站 | 黄色av网站在线观看 | 久久久www成人免费精品 | 日本中文在线视频 | 国产精品久久久久久久久久免费看 | 亚洲国产精品精华素 | 一区二区中文 | 色爱区综合 | 精产国产伦理一二三区 | 亚洲激情自拍偷拍 | 成人午夜精品 | 欧美日韩国产精品一区二区 | 看特级黄色片 | 3p视频在线观看 | 久草.com| 在线观看电影av | 中文字幕在线免费视频 | 天天插天天狠天天透 | 国产精品亚洲成在人线 | 亚洲视频精品在线 | 最新国产视频 | 毛片免费观看 | 精品麻豆剧传媒av国产九九九 | 天天操天天怕 | 亚洲97 | 亚洲精品一区二三区不卡 | 男女羞羞视频免费看 | 国产精品海角社区在线观看 |