-stark 发表于 2016-8-31 14:01:54

蓝牙手柄遥控bluno mega的led灯

本帖最后由 -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;
int joystick;
int AnalogButton;

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==0)//手柄按UP,buttonState=0
Serial1.write(1);
else if(buttonState==0)
Serial1.write(2);
delay(100);
}

void DataUpdate(){

for(int i = 3; i < 17; i++)buttonState = digitalRead(i);
buttonState = analogRead(0);
buttonState = analogRead(1);
for(int i = 0; i < 4; i++)joystick = analogRead(i);
for(int i = 4; i < 6; i++)AnalogButton = analogRead(i);

}

String Buttons = {
"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),Serial.print(" ");
for(int i = 0; i < 4; i++)Serial.print(joystick),Serial.print(" ");
for(int i = 0; i < 2; i++)Serial.print(AnalogButton),Serial.print(" ");
Serial.println("");
Serial.print("Button Pressed:");
for(int i = 0; i < 2; i++)if(buttonState < 100)Serial.print(Buttons),Serial.print(",");
for(int i = 3; i < 17; i++)if(buttonState == 0)Serial.print(Buttons),Serial.print(",");
Serial.println("");
Serial.print("Analog Sticks:");
for(int i = 0; i < 4; i++)Serial.print(joystick),Serial.print(",");
for(int i = 0; i < 2; i++)Serial.print(AnalogButton),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);
      }
}





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

谢谢分享

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

视频呢楼主{:5_165:}

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

iooops 发表于 2016-8-31 19:21
视频呢楼主

有空补:lol
页: [1]
查看完整版本: 蓝牙手柄遥控bluno mega的led灯