Arduino游戏控制器

这个arduino游戏控制器项目是我的延伸机电一体化最后一年项目。我正在使用我使用来控制视频游戏的Arduino手套。所以在继续之前,它也很好看看我的机电一体化最后一年项目为了了解设备如何运作以及它是如何制作的。

在这里,我将详细解释我如何使用处理IDE编程Arduino板来启用视频游戏的控制。观看下面的视频,以查看Arduino游戏控制器的动作需要速度。

betway


这是Arduino游戏控制器的工作原理:

  • 手套的传感器捕获臂的运动。
  • 捕获的值被发送到Arduino板模拟输入。
  • 从arduino他们被发送到处理IDE串行通信
  • 来自加工IDE他们被送到了视频游戏。

Arduino IDE


首先,我使用Arduino IDE编写了Arduino Board作为在Arduino板上连续运行的服务器工作。该代码使Arduino板与处理IDE之间的串行通信。

以下是源代码:

/ * * Arduino Game Controller * *由Dejan Nedelkovski(* www.howtomechabet188官方网站tronics.com * * ///定义变量int pinx = a8;int piny = a7;int pinz = a6;int pina0 = a0;int pina4 = a4;int pina3 = a3;int pina1 = a1;int pina2 = a2;void setup(){serial.begin(115200);//启动串行通信} void循环(){int valx = analogread(pinx); // reads the Analog Input, t.e the value from the X - axis from the accelerometer Serial.print(valX); // sends that value into the Serial Port Serial.print(","); // sends addition character right next to the read value needed later in the Processing IDE for indexing int valY=analogRead(pinY); Serial.print(valY); Serial.print("/"); int valZ=analogRead(pinZ); Serial.print(valZ); Serial.print(";"); int valA0=analogRead(pinA0); Serial.print(valA0); Serial.print(":"); int valA4=analogRead(pinA4); Serial.print(valA4); Serial.print("<"); int valA3=analogRead(pinA3); Serial.print(valA3); Serial.print("!"); int valA2=analogRead(pinA2); Serial.print(valA2); Serial.print("?"); int valA1=analogRead(pinA1); Serial.print(valA1); Serial.print("."); delay(30); }

从上面的代码中,您可以看到我使用Analogread()函数来读取来自加速度计的值的值,以便臂(3变量)的方向和电位计用于指状物的位置(5变量)。现在,这些值是通过串行端口发送到处理IDE的。在每个变量之后,我也会使用将作为索引工作的串行版本IDE向处理IDE发送特定字符。

因此,当我有连接到计算机的手套时,我将连续地通过串口发送上述数据(上面的图片)数据线。数字是来自传感器的值,并且字符用于索引它们,当在处理IDE中接收时,它将有助于帮助。笔记:数字只是例如,它们可以根据您的传感器读数而变化。


加工IDE


现在我必须收到从串行端口到处理IDE的数据,并根据他们向视频游戏发送命令。为此,我开发了下面的代码,这就是它的工作原理:betway

  • 首先它从串行端口读取数据。
  • 它将数据与每个传感器分开到变量中。
  • 根据每个传感器的值,它模拟键盘密钥按或释放,实际上是控制视频游戏。

Go Trough代码,并在代码的评论中找到特定功能和代码行的详细说明。

笔记:根据我的传感器的读数设置代码中的值。您应该根据您的读数调整它们。还用于解释代码的注释,我正在使用上图图片中的值,这只是示例号。

/ * * Arduino Game Controller项目* *由Dejan Nedelkovski贬低,* www.howtomechatronibet188官方网站cs.com * * /导入处理。*;//导入串行通信导入java.awt.robot的库;//导入键按或发布模拟导入java.awt.event.keyEvent;//导入用于从串行端口导入java.io.ioException读取数据的库;串行端口;//定义对象串行机器人机器人;//定义对象机器人//定义变量字符串x =“”;字符串y =“”;字符串z =“”;字符串A0 =“”; String A1= ""; String A2= ""; String A3= ""; String A4= ""; String data= ""; int index=0; int index2=0; int index3=0; int index4=0; int index5=0; int index6=0; int index7=0; int iX=0; int iY=0; int iZ=0; int iA0=0; int iA1=0; int iA2=0; int iA3=0; int iA4=0; // creates new robot object void setup() { try { robot = new Robot(); } catch (Exception e) { e.printStackTrace(); exit(); } delay(2000); size (800, 800); port = new Serial(this,"COM3", 115200); // starts the serial communication port.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: 215,214/141;315:314<316!314?315. } void draw() { background(0,0,0); fill(255, 255, 255); //Simulating key press or release // turn left if(iX>320) { delay(40); robot.keyPress(KeyEvent.VK_J); // Simulates "I" key press if the value from the accelerometer for the X axis is greater than 320 } if(iX<=320){ delay(40); robot.keyRelease(KeyEvent.VK_J); // Simulates "I" key release if the value from the accelerometer for the X axis is less than 320 } // turn right if( iX<280 ) { delay(40); robot.keyPress(KeyEvent.VK_L); } if(iX>=280){ delay(40); robot.keyRelease(KeyEvent.VK_L); } // turn up if(iY>320) { delay(40); robot.keyPress(KeyEvent.VK_I); } if(iY<=320){ delay(40); robot.keyRelease(KeyEvent.VK_I); } // turn down if( iY<280 ) { delay(40); robot.keyPress(KeyEvent.VK_K); } if(iY>=280){ delay(40); robot.keyRelease(KeyEvent.VK_K); } // accelerate - indexFinger if( iA4<510 ) { delay(40); robot.keyPress(KeyEvent.VK_W); } if(iA4>=510){ robot.keyRelease(KeyEvent.VK_W); } // handbrake - thumbFinger if( iA0<500 ) { robot.keyPress(KeyEvent.VK_SPACE); } if(iA0>=500){ robot.keyRelease(KeyEvent.VK_SPACE); } // reverse - middleFinger if( iA3<560 ) { robot.keyPress(KeyEvent.VK_S); } if(iA3>=560){ robot.keyRelease(KeyEvent.VK_S); } // shift up - ringFinger if( iA2<400 ) { robot.keyPress(KeyEvent.VK_R); } if(iA2>=400){ robot.keyRelease(KeyEvent.VK_R); } // shift down - littleFinger if( iA1<250 ) { robot.keyPress(KeyEvent.VK_F); } if(iA1>=250){ robot.keyRelease(KeyEvent.VK_F); } } // Reading data from the Serial Port void serialEvent (Serial port) // starts reading data from the Serial Port { data = port.readStringUntil('.'); // reads the data from the serial port up to the character '.' and it sets that into the String variable "data". So actually it reads this: 215,214/141;315:314<316!314?315. data = data.substring(0,data.length()-1); // it removes the '.' from the previous read. So this will be the String "data" variable: 215,214/141;315:314<316!314?315 // Finding the indexes in the data and setting the variables from the sensors by taking from the String "data" the appropriate values that are between the characters in the "data" String index = data.indexOf(","); // finds the index of the character "," from the String "data" variable X= data.substring(0, index); // sets into the variable X the string from position 0 of the hole string to where the index was. That would mean that read will be : 215 index2 = data.indexOf("/"); // finds the index of the character "/" from the String "data" variable Y= data.substring(index+1, index2); // sets into the variable Y data the string from position where the character "," was +1, to where the index2 was. That would mean that the read will be: 214 // We keep reading this way and that's how we get only the numbers, the values from the sensors coming from the serial port. index3 = data.indexOf(";"); Z= data.substring(index2+1, index3); index4 = data.indexOf(":"); A0= data.substring(index3+1, index4); index5 = data.indexOf("<"); A4= data.substring(index4+1, index5); index6 = data.indexOf("!"); A3= data.substring(index5+1, index6); index7 = data.indexOf("?"); A2= data.substring(index6+1, index7); A1= data.substring(index7+1, data.length()); // Converting the String variables values into Integer values needed for the if statements above iX= int(X); iY= int(Y); iZ= int(Z); iA0= int(A0); iA4= int(A4); iA1= int(A1); iA2= int(A2); iA3= int(A3); }

随意询问以下意见部分中的任何问题。

32回应

  1. awais mumtaz.

    我希望这么做。请你给我发一些可以帮助我的含水物吗?

    回复
  2. Nilesh Bable

    你玩过哪种游戏?????这个计划是否常见于所有赛车游戏???

    回复
  3. Tejas.

    当我在Arduino上传程序并在处理IDE中运行草图时,我的笔记本电脑自动启动行为......和它开始键入sfwww ... .sfwr。如何摆脱它?

    回复
  4. Tejas D.

    你好先生,我认为你为处理IDE编写的程序有一些错误。请再次检查它并重新上传。我们需要你的帮助。我们尝试了同样的尝试,但它不起作用。

    回复
  5. Erdi G.

    祝贺项目!

    如果您尝试了此控制器作为Windows或此类操作系统的游戏控制器,我很奇怪?如果是这样的话,那么我可以得到的话?

    回复
      • itachi.

        我知道你是如何使用mpu6050的,但我不明白你是如何改变电位器的值,而没有接触它?你能
        请解释一下吗?

  6. 哈瓦

    祝贺项目!

    我很好奇如何改变我的游戏控制,从Arduino接受订单?

    回复
  7. 阿拉巴纳

    嘿,这是一个惊人的项目。我尝试实现它,但在处理2.2.1的原因下发现了一个错误,禁用COM3的SeriaLevent()
    Null ",请指导我找到解决的方法??

    回复
  8. orcin.

    恭喜。你在这里做得很好。

    但让我问几个问题;

    我想用mpu6050控制我的键盘

    例如;

    如果角度大于X度,请按“Q”并释放,如果它低于x等等。

    我该怎么办?我是一个初学者在arduino编程,我将感激与任何帮助。

    我的电子邮件地址是:ozincir@gmail.com.

    谢谢,祝你下一个项目祝你好运。bet188me

    苔黑酚。

    回复
    • 德扬Nedelkovski

      谢谢。好吧,本教程的重点就是一个,只需使用不同的传感器(加速度计)。
      基本上,您需要读取MPU6050数据,将其发送到处理IDE,从那里仿真在这个项目中的关键笔画。

      回复
      • orcin.

        我设法努力工作。

        当我打开Microsoft Word时,我可以看到准确键入的键。

        但是当我打开游戏时,没有任何反应。

        游戏禁用IDE吗?

        我应该怎么办?

  9. Jonathan P.

    你好德国。

    我假设有可能将Arduino上的简单数字按钮转换为按键输出,正确?你能把我推荐给一个简单的教程,只使用几个按钮而不是电位计和加速度计?如果没有,你可以简要解释一下我如何简化代码来适应这个问题吗?

    谢谢。

    回复
    • 德扬Nedelkovski

      你好呀。嗯,你可以用与这个项目类似的方式做到这一点。首先,您需要了解如何使用Arduino和一起处理。因此,从Arduino开始,您将为每个按钮按下处理IDE发送信号。然后在处理IDE中使用“机器人”库,您可以在接收到特定信号时模拟关键笔划。
      所以,首先我建议检查我的arduino和处理IDE,然后在这个Arduino游戏控制器项目的帮助下进行代码。

      回复
  10. 酸章

    你好,
    您控制的所有内容都是数字信号,如关键按下和密钥释放。我们可以模拟游戏中的模拟信号,如转向,加速器,制动器吗?
    请建议。

    回复
    • 德扬Nedelkovski

      这是对的,手套只是模拟数字信号,钥匙笔画。此时我无法建议任何用于模拟模拟的东西,因为它在与游戏的沟通方面是完全不同的。

      回复

发表评论

您的电子邮件地址不会被公开。

受到推崇的

2019年初学者和爱好者的最佳进入级示波器

最适合初学者和爱好者的示波器

受到推崇的

2019年初学者的8个最佳Arduino Starter Kits

8个最好的arduino初学者工具包

受到推崇的

用于初学者和爱好者的最佳3D打印机 -  3D打印

初学者和爱好者的最佳3D打印机