当前位置: 移动技术网 > IT编程>移动开发>WP > windows phone 7 重力感应控制wifi小车

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

2018年09月29日  | 移动技术网IT编程  | 我要评论

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;
                }
            }
        }

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网