Bluetooth Arduino Processing Serial.available()

Hey there,

I have a Arduino Uno sending Sensor values to Processing. When I have a serial connection via USB everything works fine.But I want it working for Bluetooth as well.
So I open in processing the bluetooth port. Works. But it doesnt start sending values. AS son as i open the serial monitor in the arduino IDE, which uses the port the arduino is connected via usb, the bluetooth connection works. I figured it has something to do with the command Serial.available().

Does the command Serial.available() also recognizes incoming data for the tx pin?

Arduino

#include <NewPing.h>
#include <Servo.h>

#define trigger_pin 13
#define echo_pin 12
#define max_distance 200

NewPing sensor(trigger_pin, echo_pin, max_distance);
Servo servo1;

void setup(){
  Serial.begin(9600);
  servo1.attach(5);
  servo1.write(0);
  establishContact();
}

void loop(){

  if(Serial.available()>0){
    for (int i =0;i<90;i++){
    int direction=Serial.read();
    //delay(10);
    servo1.write(i);
    int distance = sensor.ping_cm();
    Serial.print(i);
    Serial.print(",");
    Serial.println(distance);

}
   for (int i =90;i>0;i--){
    int direction=Serial.read();
    //delay(10);
    servo1.write(i);
    int distance = sensor.ping_cm();
    Serial.print(i);
    Serial.print(",");
    Serial.println(distance);

}
}
}
void establishContact(){
  while (Serial.available() <= 0){
    Serial.println("hello");
    delay(300);
  }
}

Processing

import processing.serial.*;
Serial myPort;
int[] distances = new int[91];
boolean firstContact = false;
void setup(){
  println(Serial.list());
   
  myPort = new Serial(this, Serial.list()[2], 9600);
  myPort.bufferUntil('\n');
}

void draw(){
}
void serialEvent(Serial myPort){
  String myString = myPort.readStringUntil('\n');
  if (myString != null){
    myString = trim(myString);
    if(firstContact == false){
   
      if (myString.equals("hello")){
        myPort.clear();
        firstContact = true;
        myPort.write('A');
      }}
      else{
//String  distance = myString;
   // println(distance);
    int sensors[]=int(split(myString,','));
    int servo1 = sensors[0];
    int distance = sensors[1];
   distances[servo1]=distance; // puts the distance values from the ultrasonic sensor in an array
   int c = bestway(distances);
    println("Servo:" +servo1 + "Distance:"+distance +"bestdirection"+c);
    myPort.write('A');
  }
}
}

int bestway(int[] distances){ // this function gets the distance array passed on. Then the function finds the best way to drive!!!!
  int bestdirection=0;
  int maxweg = 0;
  int x = distances.length; //for the for-loop we need the size fo the array.
  for(int k=0; k<x; k++){
  if(maxweg<distances[k]){
  maxweg = distances[k];
  bestdirection = k;
   }
  }
return bestdirection;
}

Thanks for any suggestions

Does the command Serial.available() also recognizes incoming data for the tx pin?

The TX pin does not receive data, so, no Serial.available() will not tell you how much data has been received on the TX pin.

I figured it has something to do with the command Serial.available().

What led you to that conclusion?

In the Processing sketch, you are connecting to the 3rd serial port in the list. That is unlikely to be the same port when using the bluetooth device as when using the USB cable. Highly unlikely.

Hey,

The TX pin does not receive data, so, no Serial.available() will not tell you how much data has been received on the TX pin.

oh I ment the rx pin.

In the Processing sketch, you are connecting to the 3rd serial port in the list. That is unlikely to be the same port when using the bluetooth device as when using the USB cable. Highly unlikely.

The 3rd serial port is the bluetooth connection, and I open it in processing. And the port for the usb cable i open in the arduino ide.

I got it working. The arduino should send a "hello" every 300ms. The led for tx on the arduino starts blinking for a while. but then stops after around 10 seconds. If i haven't started the processing programm till then, there is not going to be a connection, cause no hello is being send. so I have to reset the arduino and it works.

But there is a new problem. I sometimes get the error in processing "error, disabling serialevent()". I guess it occurs as soon as something goes wrong in the serialevent() method. And sometimes it just works fine. Seems a bit random. Any idea?

Thx