Arduino游戏控制器

这个Arduino游戏控制器项目是我的扩展机电一体化期末项目。这是我用来控制电子游戏的Arduino手套。在你继续之前,检查一下我的机电一体化期末项目为了看看这个设备是如何工作和制作的。

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

亚博88下载


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

  • 手套的传感器捕捉手臂的运动。
  • 捕获的值被发送到Arduino板模拟输入。
  • 从Arduino他们被发送到Processing 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()函数从Accelerometer中读取手臂的方向(3个变量)和电位器中读取手指的位置(5个变量)。现在这些值通过串口发送到Processing IDE。同样,在每个变量之后,我使用Serial、print()函数向Processing IDE发送一个特定的字符,该函数将作为索引工作。

因此,当我有手套连接到电脑,我将不断发送上述(上图)的数据通过串口线。这些数字是来自传感器的值,字符用于索引它们,这将在Processing IDE中接收它们时有所帮助。注意:例如,这些数字可以根据你的传感器读数而变化。


处理IDE


现在我必须接收数据从串口进入处理IDE,并根据他们发送命令到视频游戏。为此,我开发了下面的代码,这是它如何工作:亚博88下载

  • 首先,它从串口读取数据。
  • 它将数据从每个传感器分离为变量。
  • 根据每个传感器的值,它模拟键盘按键或释放,这实际上是控制视频游戏。

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

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

/ * * Arduino Game Controller项目* *由Dejan Nedelkovski贬低,* www.howtomechatroni亚搏手机版官方下载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. Nilesh貂

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

    回复
  2. 光辉

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

    回复
  3. 光辉D

    你好先生,我认为您为Processing IDE编写的程序有一些错误。请再次检查并重新上传。我们需要你的帮助。其实我们也试过了,但是没有用。

    回复
  4. Erdi G

    祝贺这个项目!

    我很好奇,如果你曾经尝试过这个控制器作为一个游戏控制器设备的Windows或类似的操作系统?如果有,我能找到什么消息来源吗?

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

  5. Huda

    祝贺这个项目!

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

    回复
  6. Aradhana

    嘿,这是个很棒的项目。我尝试实现它,但发现processing2.2.1中的一个错误为-“错误,禁用COM3的serialEvent()
    null“,可以引导我找到一种方式吗?

    回复
  7. 苔黑酚

    祝贺你。你做得很好。

    但让我问几个问题;

    我想用MPU6050控制我的键盘

    例如;

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

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

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

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

    orcin。

    回复
    • Dejan Nedelkovski.

      谢谢。本教程的重点正是这一点,只是使用了不同的传感器(加速计)。
      基本上,您需要读取MPU6050数据,将其发送到Processing IDE,并从那里模拟击键,就像在这个项目中一样。

      回复
      • 苔黑酚

        我成功破解了你的代码。

        当我打开微软的word,我可以看到键盘输入准确。

        但当我打开游戏时,什么也没发生。

        游戏禁用IDE吗?

        我该怎么办?

  8. 乔纳森·P。

    你好,德扬。

    我想有可能把arduino上的一个简单的数字按钮转换成按键输出,对吗?你能给我一个更简单的教程,只用几个按钮而不是电位器和加速计吗?如果没有,你能简单解释一下我将如何简化代码以适应这一点吗?

    谢谢。

    回复
    • Dejan Nedelkovski.

      嗨。是的,你可以用类似的方法来做这个项目。所以首先你需要了解如何使用Arduino和Processing一起。因此,从Arduino你将发送一个信号到Processing IDE为每个按钮按下。然后使用Processing IDE中的“Robot”库,可以在接收到特定信号时模拟击键。
      所以,首先我建议检查我的Arduino和Processing IDE,然后在这个Arduino游戏控制器项目的帮助下,使你的代码。

      回复
  9. sourabh

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

    回复
    • Dejan Nedelkovski.

      没错,手套只是在模拟数字信号,按键。此时我无法提出任何关于模拟模拟的建议,因为就与游戏的交流而言,这是完全不同的。

      回复

留下一个回复

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

推荐

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

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

推荐

2019年针对初学者的8个最佳Arduino入门工具包

初学者的8个最佳Arduino Starter Kits

推荐

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

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