windows phone 7 重力感应控制wifi小车
022天津交友,薛伟森,蒙古国网站
建立好wifi并能通过socket发送控制指令之后,就想用手机的重力感应来控制小车行驶方向。感觉很酷哈~~~
其实获取重力感应信息的代码比较简单,就几行:
引用
using microsoft.devices.sensors;
定义
private accelerometer acc;
//下面几个变量是我程序里要用的
private int cpow = 0;
private double oldy = 0;
private bool issendright = false;
private bool issendleft = false;
private bool issendcenter = false;
private string movetype = string.empty;
然后在formload事件里加上
acc = new accelerometer();
acc.currentvaluechanged += new
www.2cto.comeventhandler<sensorreadingeventargs<accelerometerreading>>(acc_currentvaluechanged);
acc.start();
这样程序就能实时拿到重力感应数据了,然后为事件写几行代码:
void acc_currentvaluechanged(object sender, sensorreadingeventargs<accelerometerreading> e)
{
//3轴加速度值
vector3 vt = new vector3();
vt = e.sensorreading.acceleration;
//获取y坐标值,即手机横放之后左右两边方向的值
double y = vt.y * 100;
//允许+ -10范围内的晃动
if (y <= -10 || y >= 10)
{
//计算偏移量转化的速度值
cpow = (int)(oldy - math.abs(y));
//左偏移
if (y < 0 && issendleft == false)
{
senddata("moto:4");
issendleft = true;
}
//右偏移
if (y > 0 && issendright == false)
{
senddata("moto:3");
issendright = true;
}
issendcenter = false;
}
else
{
//归位时按照偏移之前的状态发送指令
issendleft = false;
issendright = false;
if (!string.isnullorempty(movetype) && issendcenter == false)
{
senddata("moto:" + movetype);
issendcenter = true;
}
}
}
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
相关文章:
-
-
-
-
-
-
WP7实例篇之土豆搜索器(1)
修改说明:由于youku的api不怎么geli ,转而采用土豆的api,文章下方就不怎么修改了,大家可以自行修改项目中的文本.
学习wp7也有一段时间了,就以...
[阅读全文]
-
WP7实例篇之土豆搜索器(2)
在上篇wp7实例篇之土豆搜索器(1) 中,我们创建了示例数据源并且将其绑定到页面中,接下来我们将要动态化绑定源数据,此时visual studio的用处就体现...
[阅读全文]
-
-
-
-
网友评论