Arduino游戏控制器

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

在这里我将详细解释我如何使用Processing IDE编程Arduino板使视频游戏的控制。观看下面的视频,看看Arduino游戏控制器在行动玩需要的速度。

亚博88下载


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

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

Arduino IDE


首先我使用Arduino IDE编程Arduino板作为一个服务器工作,将不断运行在Arduino板。该代码使Arduino板和处理IDE之间的串行通信。

源代码如下:

/* * Arduino游戏控制器* *装箱由Dejan Nedelkovski, * www.HowToMechatronics.co亚搏手机版官方下载m * */ /定义变量int pinX=A8;int松树的= 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 loop() {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的数据,并根据他们向视频游戏发送命令。为此,我开发了下面的代码,这就是它的工作原理:亚博88下载

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

仔细阅读代码,并在代码注释中找到特定函数和代码行的详细解释。

注意:代码中的值是根据我的传感器读数设置的。你应该根据你的阅读量调整它们。另外,在代码的注释中,我使用了上图中的值,这只是示例数字。

/* * Arduino游戏控制器项目* *由Dejan Nedelkovski装箱,* www.HowToMechatronics.com * */ 亚搏手机版官方下载import processing.serial.*;//导入库串行通信导入java.awt.Robot;//导入库的按键或释放模拟导入java.awt.event.KeyEvent;//从串口读取数据的imports库import java.io.IOException;串行端口;//定义对象串行机器人机器人;//定义对象机器人//定义变量字符串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泰姬

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

    回复
  2. Nilesh Bable

    你玩过哪个游戏?????这个程序是否适用于所有的赛车游戏??

    回复
  3. Tejas.

    当我在arduino中上传程序,并在处理ide中运行草图时,我的笔记本电脑自动开始不正常.....它开始输入一些东西sfwr....如何去掉它??

    回复
  4. 光辉D

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

    回复
  5. Erdi G.

    祝贺项目!

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

    回复
      • itachi.

        我明白你如何使用MPU6050,但我不明白你是如何改变电位计的价值而不触摸它?您可以...吗
        请解释一下吗?

  6. Huda

    祝贺项目!

    我想知道我如何改变我的游戏控制从arduino接受命令?

    回复
  7. 阿拉巴纳

    嘿,这是一个惊人的项目。我尝试实现它,但在处理2.2.1的原因下发现了一个错误,禁用COM3的SeriaLevent()
    null“,可以引导我找到一种方式吗?

    回复
  8. orcin.

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

    但让我问几个问题;

    我想用mpu6050控制我的键盘

    例如;

    如果角度大于X度,按“Q”,如果低于X度,则松开。

    我应该怎么做?我是Arduino编程的初学者,我会欣赏任何帮助。

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

    提前感谢,祝你的下一个项目好运。足彩网女欧洲杯

    orcin。

    回复
    • Dejan Nedelkovski.

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

      回复
      • orcin.

        我设法努力工作。

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

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

        游戏禁用IDE吗?

        我应该怎么办?

  9. Jonathan P.

    你好德国。

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

    谢谢。

    回复
    • Dejan Nedelkovski.

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

      回复
  10. 酸章

    你好,
    你所控制的只是数字信号,比如按键和释放键。我们能否在游戏中模拟模拟信号,如转向,油门,刹车?
    请建议。

    回复
    • Dejan Nedelkovski.

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

      回复

发表评论

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

推荐

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

为初学者和爱好者最好的示波器

推荐

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

初学者的8个最佳Arduino Starter Kits

推荐

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

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