欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

windows phone 7 重力感应控制wifi小车

程序员文章站 2023-02-19 22:30:44
建立好wifi并能通过socket发送控制指令之后,就想用手机的重力感应来控制小车行驶方向。感觉很酷哈~~~ 其实获取重力感应信息的代码比较简单,就几行:   引用 using micro...
建立好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;
                }
            }
        }