Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 7231|回復: 9
打印 上一主題 下一主題

Amarino bluetooth通訊問題

[複製鏈接]
跳轉到指定樓層
1#
發表於 2011-10-21 19:15:25 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
請教版上各位前輩目前在用android開發版接藍牙裝置溝通arduino,
我的arduino bluetooth模組為DF-BluetoothV3
我用arduino MultiColorLampTutorial下去執行發現
void blue(byte flag, byte numOfValues)
{
  analogWrite(blueLed, meetAndroid.getInt());
}

不管我android那邊怎麼調這個callback就是不會執行
有確定android有偵測到arduino

懇請版上前輩不吝指教,我的baud rate有試過115200和57600


謝謝

2#
發表於 2011-10-21 23:11:23 | 只看該作者
可以把全部的程式貼上來嗎?
3#
 樓主| 發表於 2011-10-22 00:04:02 | 只看該作者
arduino的code
#include <MeetAndroid.h>

// declare MeetAndroid so that you can call functions with it
MeetAndroid meetAndroid;

// we need 3 PWM pins to control the leds
int redLed = 9;   
int greenLed = 10;
int blueLed = 11;
char val;

void setup()  
{
  // use the baud rate your bluetooth module is configured to
  // not all baud rates are working well, i.e. ATMEGA168 works best with 57600
  Serial.begin(57600);
  
  // register callback functions, which will be called when an associated event occurs.
  meetAndroid.registerFunction(red, 'o');
  meetAndroid.registerFunction(green, 'p');  
  meetAndroid.registerFunction(blue, 'q');

  // set all color leds as output pins
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  
  // just set all leds to high so that we see they are working well
  digitalWrite(redLed, HIGH);
  digitalWrite(greenLed, HIGH);
  digitalWrite(blueLed, HIGH);

}

void loop()
{
  meetAndroid.receive(); // you need to keep this in your loop() to receive events
}

/*
* Whenever the multicolor lamp app changes the red value
* this function will be called
*/
void red(byte flag, byte numOfValues)
{
  Serial.print("red~~~\n");
  Serial.print(meetAndroid.getInt());
  analogWrite(redLed, meetAndroid.getInt());
}

/*
* Whenever the multicolor lamp app changes the green value
* this function will be called
*/
void green(byte flag, byte numOfValues)
{
  analogWrite(greenLed, meetAndroid.getInt());
}

/*
* Whenever the multicolor lamp app changes the blue value
* this function will be called
*/
void blue(byte flag, byte numOfValues)
{
  analogWrite(blueLed, meetAndroid.getInt());
}
4#
 樓主| 發表於 2011-10-22 00:05:57 | 只看該作者
android code
public class MultiColorLamp extends Activity implements OnSeekBarChangeListener{
       
        private static final String TAG = "MultiColorLamp";
       
        /* TODO: change the address to the address of your Bluetooth module
         * and ensure your device is added to Amarino
         */
        private static final String DEVICE_ADDRESS = "00:11:05:09:00:90";
       
        final int DELAY = 150;
        SeekBar redSB;
        SeekBar greenSB;
        SeekBar blueSB;
        View colorIndicator;
       
        int red, green, blue;
        long lastChange;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

                Amarino.connect(this, DEVICE_ADDRESS);
        
        // get references to views defined in our main.xml layout file
        redSB = (SeekBar) findViewById(R.id.SeekBarRed);
        greenSB = (SeekBar) findViewById(R.id.SeekBarGreen);
        blueSB = (SeekBar) findViewById(R.id.SeekBarBlue);
        colorIndicator = findViewById(R.id.ColorIndicator);

        // register listeners
        redSB.setOnSeekBarChangeListener(this);
        greenSB.setOnSeekBarChangeListener(this);
        blueSB.setOnSeekBarChangeListener(this);
    }
   
        @Override
        protected void onStart() {
                super.onStart();

                // load last state
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        red = prefs.getInt("red", 0);
        green = prefs.getInt("green", 0);
        blue = prefs.getInt("blue", 0);
        
        // set seekbars and feedback color according to last state
        redSB.setProgress(red);
        greenSB.setProgress(green);
        blueSB.setProgress(blue);
        colorIndicator.setBackgroundColor(Color.rgb(red, green, blue));
        new Thread(){
                public void run(){
                        try {
                                        Thread.sleep(6000);
                                } catch (InterruptedException e) {}
                                Log.d(TAG, "update colors");
                        updateAllColors();
                }
        }.start();
        
        }

        @Override
        protected void onStop() {
                super.onStop();
                // save state
                PreferenceManager.getDefaultSharedPreferences(this)
                        .edit()
                                .putInt("red", red)
                                .putInt("green", green)
                                .putInt("blue", blue)
                        .commit();
               
                // stop Amarino's background service, we don't need it any more
                Amarino.disconnect(this, DEVICE_ADDRESS);
        }



        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // do not send to many updates, Arduino can't handle so much
                if (System.currentTimeMillis() - lastChange > DELAY ){
                        updateState(seekBar);
                        lastChange = System.currentTimeMillis();
                }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
                lastChange = System.currentTimeMillis();
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
                updateState(seekBar);
        }

        private void updateState(final SeekBar seekBar) {
               
                switch (seekBar.getId()){
                        case R.id.SeekBarRed:
                                red = seekBar.getProgress();
                                updateRed();
                                break;
                        case R.id.SeekBarGreen:
                                green = seekBar.getProgress();
                                updateGreen();
                                break;
                        case R.id.SeekBarBlue:
                                blue = seekBar.getProgress();
                                updateBlue();
                                break;
                }
                // provide user feedback
                colorIndicator.setBackgroundColor(Color.rgb(red, green, blue));
        }
       
        private void updateAllColors() {
                // send state to Arduino
        updateRed();
        updateGreen();
        updateBlue();
        }
       
        private void updateRed(){
                // I have chosen random small letters for the flag 'o' for red, 'p' for green and 'q' for blue
                // you could select any small letter you want
                // however be sure to match the character you register a function for your in Arduino sketch
                Log.e("android red: ", Integer.toString(red));
                Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'o', red);
        }
       
        private void updateGreen(){
                Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'p', green);
        }
       
        private void updateBlue(){
                Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'q', blue);
        }
       
}
5#
發表於 2011-10-22 01:49:15 | 只看該作者
void red(byte flag, byte numOfValues)
{
  Serial.print("red~~~\n");
  Serial.print(meetAndroid.getInt());
  analogWrite(redLed, meetAndroid.getInt());
}
6#
 樓主| 發表於 2011-10-22 08:36:37 | 只看該作者
我就是這個callback沒呼叫到
7#
發表於 2011-10-22 19:41:33 | 只看該作者
本帖最後由 vegewell 於 2011-10-22 20:19 編輯

回復 6# iron


    try this way:

void red(byte flag, byte numOfValues)
{
int orient_val;
  orient_val = meetAndroid.getInt();
Serial.print("red~~~\n");
  Serial.print(orient_val);
   analogWrite(redLed, orient_val);
}
================
你的android開發版是哪家哪一型的?
接 藍牙裝置是哪家哪一型的?
8#
發表於 2011-10-22 20:37:15 | 只看該作者
本帖最後由 vegewell 於 2011-10-22 20:38 編輯

回復 6# iron

Android code use this below:

/*
  MultiColorLamp - Example to use with Amarino
  Copyright (c) 2009 Bonifaz Kaufmann.
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
package edu.mit.media.amarino.multicolorlamp;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import at.abraxas.amarino.Amarino;

public class MultiColorLamp extends Activity implements OnSeekBarChangeListener{

private static final String TAG = "MultiColorLamp";

/* TODO: change the address to the address of your Bluetooth module
  * and ensure your device is added to Amarino
  */
private static final String DEVICE_ADDRESS = "00:06:66:03:73:7B";

final int DELAY = 150;
SeekBar redSB;
SeekBar greenSB;
SeekBar blueSB;
View colorIndicator;

int red, green, blue;
long lastChange;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

  Amarino.connect(this, DEVICE_ADDRESS);
        
        // get references to views defined in our main.xml layout file
        redSB = (SeekBar) findViewById(R.id.SeekBarRed);
        greenSB = (SeekBar) findViewById(R.id.SeekBarGreen);
        blueSB = (SeekBar) findViewById(R.id.SeekBarBlue);
        colorIndicator = findViewById(R.id.ColorIndicator);

        // register listeners
        redSB.setOnSeekBarChangeListener(this);
        greenSB.setOnSeekBarChangeListener(this);
        blueSB.setOnSeekBarChangeListener(this);
    }
   
@Override
protected void onStart() {
  super.onStart();

  // load last state
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        red = prefs.getInt("red", 0);
        green = prefs.getInt("green", 0);
        blue = prefs.getInt("blue", 0);
        
        // set seekbars and feedback color according to last state
        redSB.setProgress(red);
        greenSB.setProgress(green);
        blueSB.setProgress(blue);
        colorIndicator.setBackgroundColor(Color.rgb(red, green, blue));
        new Thread(){
         public void run(){
          try {
     Thread.sleep(6000);
    } catch (InterruptedException e) {}
    Log.d(TAG, "update colors");
          updateAllColors();
         }
        }.start();
        
}

@Override
protected void onStop() {
  super.onStop();
  // save state
  PreferenceManager.getDefaultSharedPreferences(this)
   .edit()
    .putInt("red", red)
    .putInt("green", green)
    .putInt("blue", blue)
   .commit();
  
  // stop Amarino's background service, we don't need it any more
  Amarino.disconnect(this, DEVICE_ADDRESS);
}


@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  // do not send to many updates, Arduino can't handle so much
  if (System.currentTimeMillis() - lastChange > DELAY ){
   updateState(seekBar);
   lastChange = System.currentTimeMillis();
  }
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
  lastChange = System.currentTimeMillis();
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
  updateState(seekBar);
}

private void updateState(final SeekBar seekBar) {
  
  switch (seekBar.getId()){
   case R.id.SeekBarRed:
    red = seekBar.getProgress();
    updateRed();
    break;
   case R.id.SeekBarGreen:
    green = seekBar.getProgress();
    updateGreen();
    break;
   case R.id.SeekBarBlue:
    blue = seekBar.getProgress();
    updateBlue();
    break;
  }
  // provide user feedback
  colorIndicator.setBackgroundColor(Color.rgb(red, green, blue));
}

private void updateAllColors() {
  // send state to Arduino
        updateRed();
        updateGreen();
        updateBlue();
}

private void updateRed(){
  // I have chosen random small letters for the flag 'o' for red, 'p' for green and 'q' for blue
  // you could select any small letter you want
  // however be sure to match the character you register a function for your in Arduino sketch
  Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'o', red);
}

private void updateGreen(){
  Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'p', green);
}

private void updateBlue(){
  Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'q', blue);
}

}
9#
 樓主| 發表於 2011-10-23 21:29:36 | 只看該作者
感謝vegewell大回覆
問題解決了
是baud rate要設成9600
謝謝v大
10#
發表於 2012-10-28 00:27:57 | 只看該作者
本帖最後由 pizg 於 2012-11-10 18:12 編輯

請問各位前輩:
我也做MultiColorLampTutorial,
一切都正常,
但在Run As Android Application後,
手機顯示"異常終止", 請問該如何解決?
詳細過程請到 http://www.robofun.net/forum/viewthread.php?tid=7747&extra=page%3D1

問題已解決!!
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-6-15 21:56 , Processed in 0.193192 second(s), 8 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表