Communication with arduino needs speed improvement(linefeed, header).

edited March 2016 in Arduino

Hello,

I made an processing android mode app which is simply receiving data from an arduino via bluetooth. Got an arduino with a hc-05 bluetooth module and an hall sensor running. This arduino is sending an H as header, km/h, distanceand a linefeed:

Serial.print('H'); 
Serial.print(",");
Serial.print(KMH,2);
Serial.print(",");
Serial.print(DIST,DEC);
Serial.print(",");
Serial.println(); // CR/LF senden

I receive the data with the android mode sketch very slow (sometimes it takes 3sec until value is refreshed). I think this problem is related to " if ((char)data[data.length-1]=='\n') dataEnd = true;", but its needed to identify my data clearly. When i´m only text/print (log) the value of data i can see data comming in from the arduino very fast.

void onBluetoothDataEvent(String who, byte[] data) 
    {
        for (int i=0; i<data.length; i++ ) {
        echoArduino +=String.valueOf((char)data[i]);
        }
       if ((char)data[data.length-1]=='\n') dataEnd = true;
       if (dataEnd) {
       feedbackArdu = split(echoArduino, ',')[0]; 
      sensorArdu = split(echoArduino, ',')[1]; 
      echoArduino=""; 
        dataEnd = false; 
      }
    }

The whole android mode Sketch:

import processing.core.PFont;
import android.os.Bundle;
import ketai.net.bluetooth.*;
import android.content.Intent;
import ketai.net.bluetooth.*;
import ketai.ui.*;
import ketai.net.*;
KetaiBluetooth bt;
KetaiList klist;
String info = "";
ArrayList<String> devicesDiscovered = new ArrayList();
boolean isConfiguring = true;
String UIText;
char HEADER = 'H'; // Zeichen zur Identifikation des Anfangs einer Nachricht
short LF = 10; // ASCII-Linefeed
byte ardudata;
String str1;
float kmhArduino;
int distArduino;
int siebenArduino;
PFont font;
short portIndex = 0; // com-Port wa¨hlen, 0 ist der erste Port
boolean dataEnd = false;
String echoArduino = "";
// Datenstring mit den Servowerten, der als Byte Array konvertiert über die Schnittstelle gesendet wird.
String datenString = ""; 
String feedbackArdu = "", sensorArdu = "";
//********************************************************************
// The following code is required to enable bluetooth at startup.
//********************************************************************
void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  bt = new KetaiBluetooth(this);
}

void onActivityResult(int requestCode, int resultCode, Intent data) {
  bt.onActivityResult(requestCode, resultCode, data);
}

void setup() {
  size(displayWidth, displayHeight);
  font = loadFont ("Arial-BoldMT-48.vlw");
  bt.start(); // BT Service starten.
  //***************************************************************
  // Hier muss die individuelle Adresse deines BT Moduls eingegeben werden.
  bt.connectDevice("98:D3:31:30:2C:04"); // HC-05 BT-Modul 
  //***************************************************************
}
void draw() {
  background(255, 255, 255);
  fill(0, 0, 0);
  textFont(font, 60);
  text("Sk8tacho V0.1", 50, 100);
  textFont(font, 40);
  fill(0, 0, 0);
  text(feedbackArdu + "kmh", 100, 800);
  //text("Data:"+ardudata, 50, 700);
  text("Distance: " + sensorArdu + "m", 50, 1000);
  fill(0, 0, 0);
  textFont(font, 30);
  text("made by inventED 2016", 50, 1800);
}
    void onBluetoothDataEvent(String who, byte[] data) //Callback Methode: Es liegen Daten vom Arduino an der BT-Schnittstelle an.
{
  //Falls noch kein \n empfangen wurde, String vom Arduino also nicht nicht komplett empfangen.
  //if (!dataEnd) {
    //Byte Array elementweise in Char umwandeln und dem String hinzufügen.
    for (int i=0; i<data.length; i++ ) {
      echoArduino +=String.valueOf((char)data[i]);
    //}
  }

  //Wenn das letzte Zeichen ein \n war, ist String vom Arduino komplett empfangen worden.
  if ((char)data[data.length-1]=='\n') dataEnd = true;

  //Falls String vom Arduino komplett empfangen worden.
  if (dataEnd) {
  feedbackArdu = split(echoArduino, ',')[1]; // Substring vor dem ersten Komma heraustrennen
  sensorArdu = split(echoArduino, ',')[2];  // Substring zwischen ersten und zweiten Komma heraustrennen
  echoArduino=""; // Stingpufer zurück setzen.
    dataEnd = false; // Empfang des nächsten Strings ermöglichen.
  }
}

Anybody got an idea for another faster solution. Woulde be great! Or is something wrong in the skech?

Thank you.

sorry I´m new to coding.... and sorry for my english.

Answers

  • Answer ✓

    Hi, try put a delay (100) in arduino code, maybe it is too fast

  • Yes! You´re right! Thank you again!

  • Hey Camperos,

    do you got me an idea again? I can receive the data from the arduino with the deplay realy good........but the delay is disturbing my arduino program because im using an interrupt. i´m measuring speed with my arduino sketch. so with the delay sometimes the interrupt can´t be triggered and my speed value seems strange. Do you got an idea for a workaroud?

    here´s my arduino sketch:

    unsigned const int BAUD = 9600;
    unsigned long timeold, rpm, delta;
    float KMH, DIST, RAD= 0.165;
    
    
    volatile int ticks;
    void countRPM(){
      ticks++;
      rpm++;
      }
    
    void setup() {
    
      Serial.begin(BAUD);
      attachInterrupt(0,countRPM, FALLING);
      ticks = 0;
      timeold = 0;
    }
    
    void loop() {
    
      if (ticks>=2){
      delta = millis () - timeold;
      timeold = millis () ;
      KMH = ((ticks * RAD)/ delta)*3600.0;
      ticks = 0;
      } 
      DIST = rpm * RAD ;
    
      //Serial.print("KMH:");
      Serial.print(KMH,1);
      Serial.print(",");
      Serial.print(DIST,1);
      Serial.println(",");
      Serial.println(); // CR/LF senden
      //delay(50);
    
    }
    

    Thank you!

  • Now i tried to use delayMicroseconds (100); intead of delay(50); this helps my arduino to run correctly again..... but it didn´t help to improve my serial connection problem with my processnig sketch. Something more i can try? Thank you!

Sign In or Register to comment.