Data rate. Android (Bluetooth)-> Bluetooth module -> Arduino

Got a hardware problem? ask for help

Data rate. Android (Bluetooth)-> Bluetooth module -> Arduino

Postby spectrem12 » March 20th, 2012, 9:23 am

Hello,

I am using the amarino-toolkit to send data from my Android phone to my Arduino.

On the Android, it is a simple program that watches a seekbar and sends the progress int values (as they change sliding the bar either direction) via bluetooth to my receiving bluetooth module. The Arduino takes the values and does a simple output to the serial monitor to show me what values it is receiving.

For some reason, it is displaying these values at a slow rate. Updating the output values every second or two. I know that the Android and Amarino toolkit API are fast enough to be able to handle this output. I would think that the receiving BT should be able to receive them quickly as well.
Any idea what could be causing this delay?

BT module: http://www.mdfly.com/index.php?main_pag ... cts_id=769

Amarino-toolkit.net for the API

Thanks in advance for any help
spectrem12
 
Posts: 17
Joined: March 31st, 2011, 8:31 am

Re: Data rate. Android (Bluetooth)-> Bluetooth module -> Ard

Postby spectrem12 » March 21st, 2012, 5:20 pm

Anyone ?
spectrem12
 
Posts: 17
Joined: March 31st, 2011, 8:31 am

Re: Data rate. Android (Bluetooth)-> Bluetooth module -> Ard

Postby Colecago » March 21st, 2012, 7:37 pm

I would blame Amarino, I've made a few bluetooth Android programs and I send packets several times a second.
https://play.google.com/store/apps/deta ... GguYWd2Il0.
User avatar
Colecago
 
Posts: 245
Joined: January 27th, 2011, 9:34 am

Re: Data rate. Android (Bluetooth)-> Bluetooth module -> Ard

Postby spectrem12 » March 21st, 2012, 7:57 pm

I asked the creators of Amarino and they said that they have had plenty of success.
spectrem12
 
Posts: 17
Joined: March 31st, 2011, 8:31 am

Re: Data rate. Android (Bluetooth)-> Bluetooth module -> Ard

Postby Osgeld » March 21st, 2012, 11:29 pm

post your arduino code
post your android code

if others have success and your not in the exact same field, generally your doing something wrong, but of course we have no clue as we have nothing to base anything on
User avatar
Osgeld
 
Posts: 658
Joined: January 25th, 2011, 6:16 pm

Re: Data rate. Android (Bluetooth)-> Bluetooth module -> Ard

Postby engineersteve » March 23rd, 2012, 9:24 am

You could sniff the serial bus coming off the bluetooth module if you have access to a scope, logic analyzer, or even with a TTL-RS232 converters. If you use the converter, don't use the TTL TX and RX, since you want to sniff, not drive a line. Use two TTL input lines, then break out each RS232 output to two different PC serial port RX lines, and open two comm ports.

This will tell you whether it's your Arduino dropping packets, or some combination of your android SW and the BT module. Or post your code and hope someone can spot something. Or do a detailed review yourself.

Steve
engineersteve
 
Posts: 45
Joined: May 8th, 2011, 3:09 pm

Re: Data rate. Android (Bluetooth)-> Bluetooth module -> Ard

Postby spectrem12 » March 23rd, 2012, 5:21 pm

Android Code of activity that is sending data using Amarino.

Code: Select all
public class ColorsActivity extends Activity implements SeekBar.OnSeekBarChangeListener {
   /** Called when the activity is first created. */

   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seekbars);
 
       
        Amarino.connect(this, "00:11:11:21:05:53");
       
        SeekBar red = (SeekBar)findViewById(R.id.seekBarRED);
        red.setOnSeekBarChangeListener(this);
       
        SeekBar blue = (SeekBar)findViewById(R.id.seekBarBLUE);
        blue.setOnSeekBarChangeListener(this);
       
        SeekBar green = (SeekBar)findViewById(R.id.seekBarGREEN);
        green.setOnSeekBarChangeListener(this);
       
    }
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
        TextView seekBarValue;
        seekBarValue = new TextView(this);
        if(seekBar == (SeekBar)findViewById(R.id.seekBarRED)){
           seekBarValue = (TextView)findViewById(R.id.seekBarValueRED);
           Amarino.sendDataToArduino(this, "00:11:11:21:05:53", 'A', progress);
        }
        else if(seekBar == (SeekBar)findViewById(R.id.seekBarBLUE)){
           seekBarValue = (TextView)findViewById(R.id.seekBarValueBLUE);
           Amarino.sendDataToArduino(this, "00:11:11:21:05:53", 'B', progress);
        }
        else if(seekBar == (SeekBar)findViewById(R.id.seekBarGREEN)){
           seekBarValue = (TextView)findViewById(R.id.seekBarValueGREEN);
           Amarino.sendDataToArduino(this, "00:11:11:21:05:53", 'C', progress);
        }
        seekBarValue.setText(String.valueOf(progress));
       
           
     }
     public void onStopTrackingTouch(SeekBar seekBar){
           
     }
     public void onStartTrackingTouch (SeekBar seekBar){
     }
       
       
 
   
}


Arduino Code

Code: Select all
/*
  Receives Test Events from your phone.
  After it gets a test message the led 13 will blink
  for one second.
*/
 
#include "MeetAndroid.h"

MeetAndroid meetAndroid;
int onboardLed = 13;

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(115200);
 
  // register callback functions, which will be called when an associated event occurs.
  // - the first parameter is the name of your function (see below)
  // - match the second parameter ('A', 'B', 'a', etc...) with the flag on your Android application
  meetAndroid.registerFunction(testEvent, 'A'); 

  pinMode(onboardLed, OUTPUT);
  digitalWrite(onboardLed, HIGH);

}

void loop()
{
  meetAndroid.receive();
}

void testEvent(byte flag, byte numOfValues)
{
  flushLed(300);
  Serial.println("Hello");
  Serial.println(meetAndroid.getInt(),DEC);
  flushLed(300);
}

void flushLed(int time)
{
  digitalWrite(onboardLed, LOW);
  delay(time);
  digitalWrite(onboardLed, HIGH);
  delay(time);
}


MeetAndroid.cpp

Code: Select all
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include "WConstants.h"
#endif

#include "HardwareSerial.h"
#include "MeetAndroid.h"

extern "C" {
#include <stdlib.h>
}


// Private methods
void MeetAndroid::processCommand(){
   if(buffer[0]-FunctionBufferOffset < FunctionBufferLenght){
      void (*H_FuncPtr)(uint8_t, uint8_t) = intFunc[buffer[0]-FunctionBufferOffset];
      if (H_FuncPtr != 0) {
         H_FuncPtr(buffer[0], getArrayLength());
      }
      else {
         send("Flag not registered: ");
         send(buffer[0]);
      }
   }
   else {
      if (customErrorFunc)
         errorFunc(buffer[0], getArrayLength());
      else {
         send("Flag out of bounds: ");
         send(buffer[0]);
      }
   }
}


void MeetAndroid::init()
{
   waitTime = 30;
   startFlag = 18;
   ack = 19;
   abord = 27;
   delimiter = 59; //';'

   numberOfValues = 0;
   
   for(int a = 0;a < FunctionBufferLenght;a++){
      intFunc[a] = errorFunc;
   }
}


// public methods
MeetAndroid::MeetAndroid()
{
    // it is hard to use member function pointer together with normal function pointers.
    customErrorFunc = false;
   errorFunc = 0;
   init();
}

// Constructur for use with HardwareSerial library
MeetAndroid::MeetAndroid(H_voidFuncPtr err)
{
    customErrorFunc = true;
   errorFunc = err;
   init();
}

void MeetAndroid::registerFunction(void(*userfunction)(uint8_t, uint8_t),uint8_t command){
   intFunc[command-FunctionBufferOffset] = userfunction;
}
void MeetAndroid::unregisterFunction(uint8_t command){
   intFunc[command-FunctionBufferOffset] = errorFunc;
}

bool MeetAndroid::receive(){
   uint8_t lastByte;
   boolean timeout = false;
   while(!timeout)
   {Serial.println("test");
      while(Serial.available() > 0)
      {
         lastByte = Serial.read();
         
         if(lastByte == abord){
            flush();
         }
         else if(lastByte == ack){
            processCommand();
            flush();
         }
         else if(bufferCount < ByteBufferLenght){
            buffer[bufferCount] = lastByte;
            bufferCount++;
         }
         else return false;
      }
      
               
                if(Serial.available() <= 0 && !timeout){
         if(waitTime > 0) delayMicroseconds(waitTime);
         if(Serial.available() <= 0) timeout = true;
      }
   }
   return timeout;
}




void MeetAndroid::getBuffer(uint8_t buf[]){

   for(int a = 0;a < bufferCount;a++){
      buf[a] = buffer[a];
   }
}

void MeetAndroid::getString(char string[]){

   for(int a = 1;a < bufferCount;a++){
      string[a-1] = buffer[a];
   }
   string[bufferCount-1] = '\0';
}

int MeetAndroid::getInt()
{
   uint8_t b[bufferCount];
   for(int a = 1;a < bufferCount;a++){
      b[a-1] = buffer[a];
   }

   b[bufferCount-1] = '\0';
   return atoi((char*)b);
}

long MeetAndroid::getLong()
{
   uint8_t b[bufferCount];
   for(int a = 1;a < bufferCount;a++){
      b[a-1] = buffer[a];
   }

   b[bufferCount-1] = '\0';
   return atol((char*)b);
}

float MeetAndroid::getFloat()
{
   return (float)getDouble();
}

int MeetAndroid::getArrayLength()
{
   if (bufferCount == 1) return 0; // only a flag and ack was sent, not data attached
   numberOfValues = 1;
   // find the amount of values we got
   for (int a=1; a<bufferCount;a++){
      if (buffer[a]==delimiter) numberOfValues++;
   }
   return numberOfValues;
}

void MeetAndroid::getFloatValues(float values[])
{
   int t = 0; // counter for each char based array
   int pos = 0;

   int start = 1; // start of first value
   for (int end=1; end<bufferCount;end++){
      // find end of value
      if (buffer[end]==delimiter) {
         // now we know start and end of a value
         char b[(end-start)+1]; // create container for one value plus '\0'
         t = 0;
         for(int i = start;i < end;i++){
            b[t++] = (char)buffer[i];
         }
         b[t] = '\0';
         values[pos++] = atof(b);
         start = end+1;
      }
   }
   // get the last value
   char b[(bufferCount-start)+1]; // create container for one value plus '\0'
   t = 0;
   for(int i = start;i < bufferCount;i++){
      b[t++] = (char)buffer[i];
   }
   b[t] = '\0';
   values[pos] = atof(b);
}

// not tested yet
void MeetAndroid::getDoubleValues(float values[])
{
   getFloatValues(values);
}

// not tested yet
void MeetAndroid::getIntValues(int values[])
{
   int t = 0; // counter for each char based array
   int pos = 0;

   int start = 1; // start of first value
   for (int end=1; end<bufferCount;end++){
      // find end of value
      if (buffer[end]==delimiter) {
         // now we know start and end of a value
         char b[(end-start)+1]; // create container for one value plus '\0'
         t = 0;
         for(int i = start;i < end;i++){
            b[t++] = (char)buffer[i];
         }
         b[t] = '\0';
         values[pos++] = atoi(b);
         start = end+1;
      }
   }
   // get the last value
   char b[(bufferCount-start)+1]; // create container for one value plus '\0'
   t = 0;
   for(int i = start;i < bufferCount;i++){
      b[t++] = (char)buffer[i];
   }
   b[t] = '\0';
   values[pos] = atoi(b);
}


double MeetAndroid::getDouble()
{
   char b[bufferCount];
   for(int a = 1;a < bufferCount;a++){
      b[a-1] = (char)buffer[a];
   }

   b[bufferCount-1] = '\0';
   return atof(b);
   
}


#if defined(ARDUINO) && ARDUINO >= 100
size_t MeetAndroid::write(uint8_t b){
   return Serial.print(b);
}
#else
void MeetAndroid::write(uint8_t b){
   Serial.print(b);
}
#endif
   


void MeetAndroid::send(char c ){
   Serial.print(startFlag);
   Serial.print(c);
   Serial.print(ack);
}

void MeetAndroid::send(const char str[]){
   Serial.print(startFlag);
   Serial.print(str);
   Serial.print(ack);
}
void MeetAndroid::send(uint8_t n){
   Serial.print(startFlag);
   Serial.print(n);
   Serial.print(ack);
}
void MeetAndroid::send(int n){
   Serial.print(startFlag);
   Serial.print(n);
   Serial.print(ack);
}
void MeetAndroid::send(unsigned int n){
   Serial.print(startFlag);
   Serial.print(n);
   Serial.print(ack);
}
void MeetAndroid::send(long n){
   Serial.print(startFlag);
   Serial.print(n);
   Serial.print(ack);
}
void MeetAndroid::send(unsigned long n){
   Serial.print(startFlag);
   Serial.print(n);
   Serial.print(ack);
}
void MeetAndroid::send(long n, int base){
   Serial.print(startFlag);
   Serial.print(n, base);
   Serial.print(ack);
}
void MeetAndroid::send(double n){
   Serial.print(startFlag);
   Serial.print(n);
   Serial.print(ack);
}
void MeetAndroid::sendln(void){
   Serial.print(startFlag);
   Serial.println();
   Serial.print(ack);
}

void MeetAndroid::flush(){
   for(uint8_t a=0; a < ByteBufferLenght; a++){
      buffer[a] = 0;
   }
   bufferCount = 0;
   numberOfValues = 0;
}
spectrem12
 
Posts: 17
Joined: March 31st, 2011, 8:31 am

Re: Data rate. Android (Bluetooth)-> Bluetooth module -> Ard

Postby Colecago » March 23rd, 2012, 6:51 pm

I'm not an Arduino king, but does it have anything to do with test even and flushled? It is supposed to flash lights and delay when it receives a message, are those delays keeping it from updating your output to the console? Try shrinking what the delay time is and see if there is a noticeable difference. Also, on Android, a better way to do this, would be to create a timer and send only on a certain time because every time you slide you are sending values which could lag the Android program. I recommend either sending after a certain amount of change or after a certain time period.
User avatar
Colecago
 
Posts: 245
Joined: January 27th, 2011, 9:34 am


Return to Help me! Hardware

Who is online

Users browsing this forum: umrashrf and 2 guests