编码由BT和arduino控制的遥控车的左右功能?

时间:2012-12-27 05:31:42

标签: android eclipse

这是我用于前进和后退的代码

// Drive forward

 forward_button.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if ((event.getAction() == MotionEvent.ACTION_DOWN) | (event.getAction() == MotionEvent.ACTION_MOVE)) {
                forward_button.setPressed(true);
                Arduino = (byte) (Arduino | 16);
                write(Arduino);
                return true;

            }else if (event.getAction() == MotionEvent.ACTION_UP) {
                forward_button.setPressed(false);
                Arduino = (byte) (Arduino & 236);
                write(Arduino);
                return true;
            }
            forward_button.setPressed(false);
            return false;
        }
    });

   // Back up

 reverse_button.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if ((event.getAction() == MotionEvent.ACTION_DOWN) | (event.getAction() == MotionEvent.ACTION_MOVE)) {
                reverse_button.setPressed(true);
                Arduino = (byte) (Arduino | 32);
                write(Arduino);
                return true;

            }else if (event.getAction() == MotionEvent.ACTION_UP) {
                reverse_button.setPressed(false);
                Arduino = (byte) (Arduino & 220);
                write(Arduino);
                return true;
            }
            reverse_button.setPressed(false);
            return false;
        }
    });

我正在考虑对左右功能使用相同的方式,例如;

   left_button.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if ((event.getAction() == MotionEvent.ACTION_DOWN) | (event.getAction() == MotionEvent.ACTION_MOVE)) {
                left_button.setPressed(true);
                Arduino = (byte) (Arduino | 8);
                write(Arduino);
                return true;

            }else if (event.getAction() == MotionEvent.ACTION_UP) {
                left_button.setPressed(false);
                Arduino = (byte) (Arduino & 244);
                write(Arduino);
                return true;
            }
            left_button.setPressed(false);
            return false;
        }
    });

这是一种做法吗?

1 个答案:

答案 0 :(得分:0)

   mLButton = (Button) findViewById(R.id.button_l);
        mLButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Send a message using content of the edit text widget
                String message = "D," + left + "," + stop + "\n";
                sendMessage(message);
            }
        });

        });
        mRButton = (Button) findViewById(R.id.button_r);
        mRButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Send a message using content of the edit text widget
                String message = "D," + right + "," + stop + "\r\n";
                sendMessage(message);
            }

这是一个例子。