本帖最后由 -stark 于 2016-8-31 14:03 编辑
有空来填坑
手柄程序:
- /*
- // #
- // # Editor : Tong Hui from DFRobot, based on Lauren from DFRobot v1.0 code
- // # Date : 12.24.2012
-
- // # Product name: Wireless Joystick v2.2 for Arduino
- // # Product SKU : DFR0182
- // # Code Version: 2.0
-
- // # Description:
- // # The sketch for using the gamepad and print the button state and the analog values of the gamepad
- // #
-
- */
-
- int buttonState[17];
- int joystick[4];
- int AnalogButton[2];
-
- void setup()
- {
- Serial1.begin(115200); //Init the Serial baudrate
- InitIO(); // Initialize the inputs/outputs and the buffers
- }
-
- void InitIO(){
- for(int i = 0; i < 17; i++) pinMode(i, INPUT);
- }
-
- void loop()
- {
- DataUpdate(); //read the buttons and the joysticks data
- //printData(); //print the datas and states
- if(buttonState[5]==0)//手柄按UP,buttonState[5]=0
- Serial1.write(1);
- else if(buttonState[7]==0)
- Serial1.write(2);
- delay(100);
- }
-
- void DataUpdate(){
-
- for(int i = 3; i < 17; i++) buttonState[i] = digitalRead(i);
- buttonState[0] = analogRead(0);
- buttonState[1] = analogRead(1);
- for(int i = 0; i < 4; i++) joystick[i] = analogRead(i);
- for(int i = 4; i < 6; i++) AnalogButton[i-4] = analogRead(i);
-
- }
-
- String Buttons[17] = {
- "J2","J1","NULL","S2","S1","UP","LEFT","DOWN","RIGHT","1","4","2","3","RZ1","RZ2","LZ1","LZ2"};
- // Buttons Nmes
-
- void printData(){
- for(int i = 0; i < 17; i++) Serial.print(buttonState[i]),Serial.print(" ");
- for(int i = 0; i < 4; i++) Serial.print(joystick[i]),Serial.print(" ");
- for(int i = 0; i < 2; i++) Serial.print(AnalogButton[i]),Serial.print(" ");
- Serial.println("");
- Serial.print("Button Pressed:");
- for(int i = 0; i < 2; i++) if(buttonState[i] < 100) Serial.print(Buttons[i]),Serial.print(",");
- for(int i = 3; i < 17; i++) if(buttonState[i] == 0) Serial.print(Buttons[i]),Serial.print(",");
- Serial.println("");
- Serial.print("Analog Sticks:");
- for(int i = 0; i < 4; i++) Serial.print(joystick[i]),Serial.print(",");
- for(int i = 0; i < 2; i++) Serial.print(AnalogButton[i]),Serial.print(",");
- Serial.println("");
- Serial.println("");
- }
复制代码
mega程序:- int incoming=0;
- void setup(){
- Serial.begin(115200);
- }
-
- void loop(){
-
- if (Serial.available() > 0) {// 只在收到数据时发送数据
- incoming = Serial.read(); // 读取传入的字节
- Serial.println(incoming, DEC); // 指示你收到的数据
-
- if(incoming== 6)
- digitalWrite(13,HIGH);
- else if(incoming== 24)
- digitalWrite(13,LOW);
- }
- }
-
-
-
复制代码
|