- UID
- 77580
- 在线时间
- 小时
- 积分
- 16281
- 帖子
- 离线
- 18650 天
- 注册时间
- 2003-12-25
|

楼主 |
发表于 2012-4-23 00:14:27
|
显示全部楼层
以后还是持续更新到这个帖,有兴趣的就看看,没兴趣的忽略下。
使用arduino扑捉超声波测距的数据,然后使用串口发回电脑,电脑获取串口信号后,转化为相应高度的水平方向鼠标移动。
原理很简单
超声波-》arduino-》串口信号-》ACC KEYS-》键盘鼠标事件-》操作游戏
准备:
1.电脑
2.arduino主板
3.超声波测距感应器
4.在线切西瓜游戏
5.acc keys 软件(串口信号转换为键盘鼠标事件)
步骤
1.烧写源文件到arduino
2.超声波接线 VCC GROUND D5 D4
3.设置acc keys 相应的频率和串口
4.打开在线的切西瓜游戏。
源文件:- int inputPin=5;
- int outputPin=4;
- void setup() {
- Serial.begin(57600);
- pinMode(inputPin, INPUT);
- pinMode(outputPin, OUTPUT);
- }
- void loop(){
- long h=getUltra();
- int i;
-
- if(h<100){
- h=map(h,1,100,700,100);
- //Serial.println(h); //输出距离值
- Serial.print("\033,goto,100,");
- Serial.print(h);
- Serial.print(".");
- Serial.print("\033,moulock,left.");
- delay(10);
- Serial.print("\033,move,200,0.");
- delay(10);
- Serial.print("\033,move,200,0.");
- delay(10);
- Serial.print("\033,move,200,0.");
- delay(10);
- Serial.print("\033,move,200,0.");
-
-
- }
- delay(50);
- }
- unsigned long getUltra(){
- digitalWrite(outputPin, LOW); // 使发出发出超声波信号接口低电平2μs
- delayMicroseconds(2);
- digitalWrite(outputPin, HIGH); // 使发出发出超声波信号接口高电平10μs,这里是至少10μs
- delayMicroseconds(10);
- digitalWrite(outputPin, LOW); // 保持发出超声波信号接口低电平
- unsigned long distance = pulseIn(inputPin, HIGH); // 读出脉冲时间
- distance= distance/58; // 将脉冲时间转化为距离(单位:厘米)
- distance=max(0,min(200,distance));
-
- return distance;
- }
复制代码 |
|