这个Arduino游戏控制器项目是我的扩展机电一体化期末项目。这是我用来控制视频游戏的Arduino手套。所以在你继续之前,检查一下我的机电一体化期末项目以便了解设备的工作原理和制作过程。
在这里,我将详细说明如何使用处理IDE编写Arduino板来实现对视频游戏的控制。观看下面的视频,看看Arduino游戏控制器在行动中玩的需要速度。
betway
这是Arduino游戏控制器的工作原理:
Arduino IDE
首先,我使用Arduino IDE为Arduino板编程,使其作为服务器工作,可以在Arduino板上持续运行。该代码使Arduino板和处理IDE之间的串行通信成为可能。
这里是源代码:
/* * Arduino游戏控制器* * Crated by Dejan Nedelkovski, * www.HowToMechatrbet188官方网站onics.com * */ //定义变量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,并根据它们发送命令给视频游戏。为此,我开发了下面的代码,它是如何工作的:betway
- 首先,它从串口读取数据。
- 它将数据从每个传感器中分离成变量。
- 根据每个传感器的值,它模拟了实际控制视频游戏的键盘按键的按下或释放。
浏览代码,并在代码的注释中找到特定函数和代码行的详细解释。
注意:代码中的值是根据传感器的读数设置的。你应该根据你的阅读来调整它们。为了便于解释,我在代码注释中使用了上图中的值,它们只是示例数字。
/* * Arduino游戏控制器项目* *由Dejan Nedelkovski创建,* www.HowToMechatronics.com * */ bet188官方网站import processing.serial.*;//用于串行通信的导入库import java.awt.event.KeyEvent;//从串口读取数据的导入库串行端口;//定义对象Serial Robot Robot;//定义对象Robot //定义变量String 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); }
请在下面的评论部分提出任何问题。
我希望这么做。请你给我发一些可以帮助我的含水物吗?
检查这个手套的原始项目。在这里,您可以找到有关它的构建方式,它是如何工作的更多详细信息,使用哪些部分,电路原理图,下载文件等。betway
这是链接:https://bet188官方网站howtomechatrbet188meonics.com/projects/mechatronics-final-year-project/
你玩过哪个游戏?????这个程序适用于所有的赛车游戏吗?
你可以玩任何游戏。这个控制器实际上是模拟键盘按键的按压。
滑块(线性电位器)的值是多少??我是说以欧姆为单位?
当我在arduino上上传程序并在处理ide中运行草图时,我的笔记本电脑自动启动行为不端.....然后它开始输入一些东西,比如....如何摆脱它??
这就是这个项目的重点。来模拟按键。例如,如果你向左倾斜手套,它会模拟左箭头键。您应该根据传感器的读数调整代码。
我得到了同样的东西,它与游戏没有互动
是什么问题?
当我运行处理它开始写信,它不工作的游戏!!我急需你的帮助
请回答我
你好先生,我认为你为处理IDE写的程序有一些错误。请再次检查并重新上传,我们需要你的帮助。其实我们也试过,但没有用。
什么样的错误?
恭喜这个项目!
我很好奇你是否曾经在Windows或其他操作系统上尝试过这个控制器作为游戏控制器?如果有,我能得到什么消息来源吗?
就是这样。我在我的Windows笔记本电脑上玩游戏。
你在哪里以及为什么使用HC-SR04传感器?
我没有使用过它,它只是处理IDE代码中的一个拼写错误,我已经纠正了它。
我知道你是如何使用mpu6050的,但是我不明白你是如何在不接触电位器的情况下改变电位器的值?你能
请解释一下吗?
恭喜这个项目!
我很好奇如何改变我的游戏控制来接受arduino的命令?
嘿,这是个很棒的项目。我尝试实现它,但发现一个错误,在处理2.2.1 as - "错误,禁用serialEvent()为COM3
空”,请您指导我找到一条出路??
尝试我的Arduino和处理IDE教程可能对您的问题很有用。
恭喜。你在这里做得很好。
但是让我问几个问题;
我想用MPU6050控制我的键盘
例如;
如果角度大于X度,按“Q”,如果低于X度则松开等。
我该怎么办?我是arduino编程的初学者,我会感激任何帮助。
我的电子邮件地址是:ozincir@gmail.com
提前感谢,祝你的下一个项目好运。bet188me
苔黑酚。
谢谢。好吧,本教程的重点就是一个,只需使用不同的传感器(加速度计)。
基本上,您需要读取MPU6050数据,将其发送到处理IDE,从那里仿真在这个项目中的关键笔画。
谢谢你删除了我的问题…
我设法努力工作。
当我打开Microsoft Word时,我可以看到准确键入的键。
但当我打开游戏时,什么都没有发生。
游戏禁用IDE?
我应该怎么办?
你好,德扬。
我想可以把arduino上的一个简单的数字按钮转换成按键输出,对吧?你能不能给我介绍一个更简单的教程,用几个按钮代替电位器和加速度计?如果没有,您能简要解释一下我将如何简化代码以适应这一点吗?
谢谢。
你好呀。嗯,你可以用与这个项目类似的方式做到这一点。首先,您需要了解如何使用Arduino和一起处理。因此,从Arduino开始,您将为每个按钮按下处理IDE发送信号。然后在处理IDE中使用“机器人”库,您可以在接收到特定信号时模拟关键笔划。
因此,我建议首先检查我的Arduino和处理IDE,然后在Arduino游戏控制器项目的帮助下制作代码。
你好,
你所控制的只是数字信号,就像按键和按键释放。我们可以在游戏中模拟模拟信号,比如转向,加速器,刹车吗?
请建议。
没错,手套只是在模拟数字信号,按键。现在我不能提出任何关于模拟模拟的建议,因为就与游戏的交流而言,这是完全不同的。
伟大的项目。密码很适合我的。
在此使用了什么类型的传感器
它使用了加速计。