发送数据只要按钮正在被pressed到阿尔杜伊诺 [英] Send data to arduino as long as the button is being pressed

查看:260
本文介绍了发送数据只要按钮正在被pressed到阿尔杜伊诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想作为一个按钮被pressed和按钮被释放时就会停止发送信息到数据只要发送到一个Arduino兆2560。我使用onTouchListener与MotionEvent常数。但是,当我在我的手机,我preSS按钮运行这个并把它发送数据,即使有一段时间我释放了。我在哪里是错在这里?

 开关(v.getId()){    案例R.id.left1://检查什么按钮pressed        而(event.getAction()== MotionEvent.ACTION_DOWN){            bt.sendData(1); //而pressing的按钮,它发送数据        }
        如果(event.getAction()== MotionEvent.ACTION_UP){            //当它停止,什么都不做        }        打破;    }    返回true;


解决方案

您的问题是在无限循环而(event.getAction()== MotionEvent.ACTION_DOWN)的你开始在收到第一个事件。

OnTouchListener 称为该被分派查看,向下和向上是独立的事件和事件而被处理不改变每个事件。

所以要解决你的问题 - 你需要从一个单独的线程发送数据。
开始它 ACTION_DOWN ,也有一个标志,将在 ACTION_UP 进行修改,以表明线程退出。

I want to send data to an arduino mega 2560 as long as a button is being pressed and when that button is released it will stop sending informations. I am using onTouchListener with MotionEvent constants. But when I run this on my phone I press the button and it sends data even though after a while I release it. Where am I being wrong here?

    switch (v.getId()) {

    case R.id.left1: // check what button is pressed

        while(event.getAction() == MotionEvent.ACTION_DOWN) {

            bt.sendData("1"); // while pressing the button it sends data

        } 
        if(event.getAction() == MotionEvent.ACTION_UP) {

            // when it stops, do nothing

        }

        break;

    }

    return true;

解决方案

Your problem is in infinite loop while(event.getAction() == MotionEvent.ACTION_DOWN) that you start upon receiving the first event.

OnTouchListener is called for each event that is dispatched to view, down and up are separate events and event does not change while being processed.

So to solve your problem - you need to send data from a separate thread. Start it on ACTION_DOWN and also have a flag that will be modified on ACTION_UP to indicate thread to exit.

这篇关于发送数据只要按钮正在被pressed到阿尔杜伊诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆