Arduino 飞鼠 空中鼠标 陀螺仪体感鼠标
空中飞鼠
还在担忧你的操作不行吗?还在厌倦枯燥的操作么?想不想像实战一样,手中握着枪与人对战?
下面带你实现你的梦想。
硬件准备
说到增强游戏体感,拿着实体枪去战斗,第一想到的就是任天堂的zapper了,但是他的价格与游戏设定,Emm…
想要实现体感控制,作为一名爱好者,当然是DIY,首先我们需要陀螺仪来感应操控的动作 ,然后就需要一个驱动器来帮助我们处理信号并传给游戏主机,在这里我选择的开发板是Arduino Pro Micro和陀螺仪ADXL345,当然从某宝上买回来得自己焊下引脚。
线路图
当硬件准备好了之后就是怎么接线了
程序控制
下面就是程序控制了,先预装好Arduino IDE,首先要引用两个类
#include <mouseWheel.h>
#include <mousePress.h>
- 1
- 2
读取ADXL345的XY值
y345 = -WireRead2( ADXAddressR, DATAX0,DATAX1);
x345 = WireRead2( ADXAddressR, DATAY0,DATAY1);
if(x345<0)
x345*=1.5;
if(y345>0)
y345*=1.5;
speedoX = constrain(pow(abs(x345),2)/100,0,12700);
speedoY = constrain(pow(abs(y345),2)/100,0,12700);
speedo();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
判断上下左右
if( x345 > value345X ){
x = -speedoxVal;
}else if( x345 < -value345X ){
x = speedoxVal;
}else
x = 0;
if( y345 > value345Y ){
y = -speedoyPos;
}else if( y345 < -value345Y ){
y = speedoyPos;
}else
y = 0;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
左键右键连击
myMouseLeft.press_mouse();
myMouseRight.press_mouse();
wheel = mywheel.read_wheel();
if( millis() - clickTime > clickSpeedo ){
if(!digitalRead(CLICK))
Mouse.click();
clickTime = millis();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
PWM控制鼠标速度
if( (micros() - ctrlMouseTimeX >= PWMSTOPX*80)//“停”
&& valuePWMX == true){
ctrlMouseTimeX = micros();
valuePWMX = !valuePWMX;
xVal = x;
}else if ( (micros() - ctrlMouseTimeX >= PWMGOX *80)//“走”
&& valuePWMX == false ){
ctrlMouseTimeX = micros();
valuePWMX = !valuePWMX;
xVal = 0;
}
if( (micros() - ctrlMouseTimeY >= PWMSTOPY*80) //“停”
&& valuePWMY == true){
ctrlMouseTimeY = micros();
valuePWMY = !valuePWMY;
yPos = y;
}else if ( (micros() - ctrlMouseTimeY >= PWMGOY *80)//“走”
&& valuePWMY == false ){
ctrlMouseTimeY = micros();
valuePWMY = !valuePWMY;
yPos = 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
鼠标移动
Mouse.move( xVal ,yPos ,wheel );
- 1
想要源码的同学点 这里下源码(赚点积分)
参考资料:
植物大战僵尸破解版: http://www.pvzbaike.com/archives/pvz_pojie/
植物大战僵尸必读:http://blog.sina.com.cn/s/blog_a7e0a15c0102z8uo.html