Android Bluetooth to Arduino and vice versa

You are using “Serial.println(analogRead(A0));”
This will include a ‘\n’ to the bytes you read from the analog port, which will be used as a delimiter in the processing’s sketch where you use “myPort.bufferUntil(’\n’);”
The Ketai library is different from the Serial library and has no such method. You have to implement a Ring buffer(type FIFO) yourself, to avoid losing data after the delimiter. But for simple sketches where no high speed is required such as playing music, you can give a higher delay() in your Arduino sketch and use write() even without a delimiter, or use one and trim it. As you already have your own Terminal sketch, just try empirically what the best delay time is in your Arduino sketch. If you use a delimiter it doesn’t necessarily need to be ‘\n’ it can be any character.

1 Like

form arduino i can send any thing but i need working processing sketch

I don’t understand this. Your Terminal sketch isn’t working?
Here’s another one. It will change the background from black to white when you turn the potentiometer.
Use Serial.write(analogRead(A0)); and delay(100); in the Arduino code.
Btw. Only one solution is excepted in the topic.

import ketai.net.bluetooth.*; 
import android.os.Bundle; 
String device_name = "BT04-A"; 
String Str; 

KetaiBluetooth bt; 

void setup() { 
  fullScreen(); 
  orientation(PORTRAIT); 
  bt.getPairedDeviceNames(); 
  bt.connectToDeviceByName(device_name); 
  bt.start();
} 

void draw() { 
  background(int(Str));
} 

void onBluetoothDataEvent(String who, byte[] data) { 
  Str = new String(data); 
  println(Str);
} 
void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  bt = new KetaiBluetooth(this);
}

Hello,

If you have not already set up serial over Bluetooth:

:)

1 Like

hi sir

i did and i make great progress but i need to learn now how to send from arduino to processing sketch

thank you very much

hi

the sketch keep stopping unless i remove this line works with just white screen receiving data from arduino

} 

void draw() { 
 // background(int(Str)); // work when mark this line
} 

void onBluetoothDataEvent(String who, byte[] data) { 
  Str = new String(data); 
  println(Str);
}

Try this instead.

void onBluetoothDataEvent(String who, byte[] data) {
Str = new String(data);
background(int(Str));
}
And leave draw() empty.

its working but what if i want to use it inside void draw how to ?

I am sorry about the confusion because I totally forgot that the data string for some reason is not captured within draw(); although it is declared as global. All my Bluetooth sketches work this way. I use a function called void plot() that is called from within the onBluetoothDataEvent(), wherein I draw everything related to the data string Str. All other things not related to the string can be drawn normally within the draw() function.
Try to draw the text as well, you will see that all will be drawn if it were in draw(), because void onBluetoothDataEvent() is also a (time) driven function.

1 Like

I was thinking about this, and I am writing code without testing, but could you try the following?
Declare a string as global like:

String bt_string;

Then:

void onBluetoothDataEvent(String who, byte[] data) {
  String Str = new String(data);
  bt_string = Str;
}

And in draw():
background(int(bt_string));

same result

void onBluetoothDataEvent(String who, byte[] data) {
  String Str = new String(data);
  bt_string = Str;
}
And in draw():
background(int(bt_string));


did not work

work like this

}

void draw() {
// background(int(bt_string)); // work when mark this line
} ```
void onBluetoothDataEvent(String who, byte[] data) {
String Str = new String(data);
bt_string = Str;
background(int(bt_string));

}

Yes, I must have tested this long ago when I started. Thus the way to go is the one I described above. It works well in my sketches.

so is this is (int(Str)); which we can use it but in

a function called void plot()

void onBluetoothDataEvent(String who, byte[] data) {
Str = new String(data);
background(int(Str));
}

Yes, but not necessarily. You can draw everything in the void onBluetoothDataEvent() function, but my code is quite large there, because of the ring buffer I placed there. My sketches run using various channels with a baud rate velocity over 115,200, without any data loss, using the Ketai lib. I use a separated function to plot the data only because of oversight reasons.

so how to draw rect and change high or width or position etc upon this (int(Str)); ?

Just use the normal functions within the bluetooth event function like for example:

void onBluetoothDataEvent(String who, byte[] data) {
  Str = new String(data);
  background(255);
  rect(0, 0, int(Str), int(Str));
}
1 Like

noel

hi i made simple Bluetooth oscilloscope

1 Like

Nice! What frequency can you reach? If necessary I can make some code for better string handling. I can’t give you the ring buffer code, because it’s part of a commercial app I made.
But just pure string handling also can do a good job.

1 Like

that is what i need if you kind[quote="noel,

using arduino with out divider or any external components reach about 1 mhz