7757浏览
查看: 7757|回复: 5

[进阶] 蓝牙手柄遥控bluno mega的led灯

[复制链接]
本帖最后由 -stark 于 2016-8-31 14:03 编辑

有空来填坑
手柄程序:
  1. /*
  2. // #
  3. // # Editor     : Tong Hui from DFRobot, based on Lauren from DFRobot v1.0 code
  4. // # Date       : 12.24.2012
  5. // # Product name: Wireless Joystick v2.2 for Arduino
  6. // # Product SKU : DFR0182
  7. // # Code Version: 2.0
  8. // # Description:
  9. // # The sketch for using the gamepad and print the button state and the analog values of the gamepad
  10. // #
  11. */
  12. int buttonState[17];
  13. int joystick[4];
  14. int AnalogButton[2];
  15. void setup()
  16. {
  17.   Serial1.begin(115200);  //Init the Serial baudrate
  18.   InitIO();             // Initialize the inputs/outputs and the buffers
  19. }
  20. void InitIO(){
  21.   for(int i = 0; i < 17; i++) pinMode(i, INPUT);
  22. }
  23. void loop()
  24. {
  25.   DataUpdate();  //read the buttons and the joysticks data
  26.   //printData();   //print the datas and states
  27.   if(buttonState[5]==0)//手柄按UP,buttonState[5]=0
  28.   Serial1.write(1);
  29.   else if(buttonState[7]==0)
  30.   Serial1.write(2);
  31.   delay(100);
  32. }
  33. void DataUpdate(){
  34.   
  35.   for(int i = 3; i < 17; i++)  buttonState[i] = digitalRead(i);
  36.   buttonState[0] = analogRead(0);
  37.   buttonState[1] = analogRead(1);
  38.   for(int i = 0; i < 4; i++)  joystick[i] = analogRead(i);
  39.   for(int i = 4; i < 6; i++)  AnalogButton[i-4] = analogRead(i);
  40.   
  41. }
  42. String Buttons[17] = {
  43.   "J2","J1","NULL","S2","S1","UP","LEFT","DOWN","RIGHT","1","4","2","3","RZ1","RZ2","LZ1","LZ2"};
  44.   // Buttons Nmes
  45. void printData(){
  46.   for(int i = 0; i < 17; i++)  Serial.print(buttonState[i]),Serial.print(" ");
  47.   for(int i = 0; i < 4; i++)  Serial.print(joystick[i]),Serial.print(" ");
  48.   for(int i = 0; i < 2; i++)  Serial.print(AnalogButton[i]),Serial.print(" ");
  49.   Serial.println("");
  50.   Serial.print("Button Pressed:");
  51.   for(int i = 0; i < 2; i++)  if(buttonState[i] < 100)  Serial.print(Buttons[i]),Serial.print(",");
  52.   for(int i = 3; i < 17; i++)  if(buttonState[i] == 0)  Serial.print(Buttons[i]),Serial.print(",");
  53.   Serial.println("");
  54.   Serial.print("Analog Sticks:");
  55.   for(int i = 0; i < 4; i++)  Serial.print(joystick[i]),Serial.print(",");
  56.   for(int i = 0; i < 2; i++)  Serial.print(AnalogButton[i]),Serial.print(",");
  57.   Serial.println("");
  58.   Serial.println("");
  59. }
复制代码

mega程序:
  1. int incoming=0;
  2. void setup(){
  3.   Serial.begin(115200);
  4. }
  5. void loop(){
  6.         
  7.         if (Serial.available() > 0) {// 只在收到数据时发送数据        
  8.                 incoming = Serial.read();  // 读取传入的字节
  9.                Serial.println(incoming, DEC); // 指示你收到的数据
  10.                if(incoming== 6)                    
  11.                     digitalWrite(13,HIGH);
  12.                else if(incoming== 24)           
  13.                     digitalWrite(13,LOW);
  14.         }
  15. }
复制代码


dsweiliang  初级技神

发表于 2016-8-31 14:44:21

谢谢分享
回复

使用道具 举报

iooops  中级技匠

发表于 2016-8-31 19:21:38

视频呢楼主
回复

使用道具 举报

luna  初级技神

发表于 2016-9-1 10:15:22

楼主,这个代码是用在哪个上面的啊?求视频
回复

使用道具 举报

-stark  中级技师
 楼主|

发表于 2016-9-1 16:19:24

luna 发表于 2016-9-1 10:15
楼主,这个代码是用在哪个上面的啊?求视频

有空补视频:P
回复

使用道具 举报

-stark  中级技师
 楼主|

发表于 2016-9-1 16:19:46


有空补:lol
回复

使用道具 举报

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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail