Android Bluetooth to Arduino and vice versa

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

Are you sure? Isn’t it 1 kHz?

i measure and generate on analog pln about 1.6 microsecond

These HC-05 modules normally come with a baud rate set to 9600, but of course with the AT commands you can set them to higher values. Normally I use 115200. But even then, a baud rate of 115200 generates in theory 12800 bytes per second, and every character is a byte. You need 3 bytes to give you the Y value, so that leaves you with approximately 4266 Y-values per second. And then you need delimiters, and sometimes handshaking code. So I guess there’s something wrong with your calculation.

do not forget that the arduino it self can mange your readings before you send them out

It’s not about the frequency an Arduino can generate, but about the speed, the data is sent through Bluetooth. :smiley:

i know what you mean and i mean that arduino sending digitized signals not direct signals

my hantek pc oscilloscope is 80 mhz bandwidth and 250 mhz sampling rate and linking serial with pc at 9600 b/r

Well, I won’t argue, but I promised to make a code, so I will, but only after tomorrow midday, because I haven’t finished my current work.

ok and thanks in advance

i am waiting

Just a last question. How many milliseconds are you using in the delay() of your Arduino code at this moment?

the best vary from 100 to 200 ms

Today I have spent some time trying to plot some lines to the canvas like I’ve done before with the Ketai lib, but I found out that I can’t plot any data on my Android 10 phone. It connects and I can read the data in the console with the println() function but that’s it. So I installed the APDE version v0.4.1 Alpha which I used the last time for plotting and installed it on my Lollipop phone, Which worked again. But for me working with a library that can’ even transmit data to the draw() loop makes no sense, so I will try to write my own library.
I did write some code to handle the incoming string, and it works so that you can use a delay with just a few milliseconds in your Arduino code. It is as it is, for now, and needs some improvement, but the actual library is my priority now. If you test something please let me know, on what device with what versions of the software. I also saw your new topic with the graphic library. Did that work with plotting lines?

  • processing code
import ketai.net.bluetooth.*;
import android.os.Bundle;

String device_name = "HC-05";
String Str, opStr, pStr; 
int xPos, yPos, oldxPos, oldyPos, x, tyPos, m;

KetaiBluetooth bt;

void setup() { 
  fullScreen();
  orientation(LANDSCAPE);
  background(255);
  //strokeWeight(3);
  fill(0);
  stroke(0);
  bt.getPairedDeviceNames();
  bt.connectToDeviceByName(device_name);
  bt.start();
  m = height/2-128;
}

void draw() {
}

void onBluetoothDataEvent(String who, byte[] data) {
  if (data != null) {
    String Str = new String(data);
    //println(Str);

    if (Str.indexOf('!') == -1) {
      pStr = pStr + Str;
      Str = "";
    }

    if (Str == "!") {
      yPos = Integer.valueOf(pStr);
      line(xPos, oldyPos, xPos, yPos);
      oldyPos = yPos;
      xPos += 2;
      pStr = "";
      Str = "";
    }   

    if (Str.startsWith("!")) {
      yPos = Integer.valueOf(pStr);
      line(xPos, oldyPos, xPos, yPos);
      Str = Str.substring(1);
      oldyPos = yPos;
      xPos += 2;
      pStr = "";
    }

    if (Str.endsWith("!")) {
      Str = Str.substring(0, Str.length()-1); 
      if (Str.indexOf('!') != -1) { 
        Str = pStr + Str;
        for (String ts : Str.split("!")) {
          yPos = Integer.valueOf(ts);
          line(xPos, oldyPos, xPos, yPos);
          oldyPos = yPos;
          xPos += 2;
        }
        pStr = ""; 
        Str = "";
      } else {
        yPos = Integer.valueOf(pStr + Str);
        line(xPos, oldyPos, xPos, yPos);
        xPos += 2;
        pStr = "";
      }
    }

    if (Str.indexOf('!') != -1) { 
      opStr = pStr;
      pStr = Str.substring(Str.lastIndexOf('!')+1);
      Str = opStr + Str.substring(0, Str.lastIndexOf("!"));
      for (String ts : Str.split("!")) {
        yPos = Integer.valueOf(ts);
        line(xPos, oldyPos, xPos, yPos);
        oldyPos = yPos;
        xPos += 2;
      }
    }

    if (xPos >= width) {
      xPos = 0;
      background(255);
    }
    //   line(xPos, oldyPos, xPos, yPos);   
    //   point(xPos, yPos);
    //   println(xPos+" "+yPos);
  }
}


void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  bt = new KetaiBluetooth(this);
}
  • Arduino code I used for testing
#include <SoftwareSerial.h>
#include <Wire.h>

SoftwareSerial mySerial(11, 12); // RX, TX

int16_t counter = 0;
boolean toggle;

void setup() {
  mySerial.begin(9600); // Set the baudrate equal to HC06 setting
} 

void loop() {
  mySerial.print(String(counter)+'!');
  delay(5);
  if (counter > 255) toggle = true;
  if (counter < 0) toggle = false;
  if (toggle) counter--;
  else counter++;
}
1 Like

thanks for you i am going to try this code to day

for graphic library i am trying to run it on my tablet did not work with me yet i have a code and want to test it

Processing Code

// import require libraries
import grafica.*;
import processing.serial.*;

// create plot instance
GPlot plot;

// initialise global variables
int i = 0; // variable that changes for point calculation
int points = 450; // number of points to display at a time
int totalPoints = 500; // number of points on x axis
float noise = 0.1; // added noise
float period = 0.35;
long previousMillis = 0;
int duration = 20;

// Serial
Serial myPort; // Create object from Serial class

void setup(){
// set size of the window
size (900,450);

// set up serial connection
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 115200);

// initialise graph points object
GPointsArray points1 = new GPointsArray(points);

// calculate initial display points
for (i = 0; i < points; i++) {
points1.add(i,0);
}

// Create the plot
plot = new GPlot(this);
plot.setPos(25, 25); // set the position of to left corner of plot
plot.setDim(750, 300); // set plot size

// Set the plot limits (this will fix them)
plot.setXLim(0, totalPoints); // set x limits
plot.setYLim(0, 300); // set y limits

// Set the plot title and the axis labels
plot.setTitleText(“Distance Sensor Example”); // set plot title
plot.getXAxis().setAxisLabelText(“x axis”); // set x axis label
plot.getYAxis().setAxisLabelText(“Distance”); // set y axis label

// Add the two set of points to the plot
plot.setPoints(points1);

}

void draw() {
// set window background
background(150);

// draw the plot
plot.beginDraw();
plot.drawBackground();
plot.drawBox();
plot.drawXAxis();
plot.drawYAxis();
plot.drawTopAxis();
plot.drawRightAxis();
plot.drawTitle();
plot.getMainLayer().drawPoints();
plot.endDraw();

// check if i has exceeded the plot size
if (i > totalPoints){
i=0; // reset to zero if it has
}

// get new value from serial port
if ( millis() > previousMillis + duration) { // If data is available,
myPort.write(0);
println(“Sensor request sent”);
while (myPort.available() < 0){};
int val = myPort.read()*256 + myPort.read(); // read it and store it in val
println(val);
// Add the point at the end of the array
i++;
plot.addPoint(i,val);

// Remove the first point
plot.removePoint(0);

previousMillis = previousMillis + duration;

}

}
``


arduino code

arduino side

#include <NewPing.h>

#define TRIGGER_PIN 4 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 3 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
while(Serial.available()){
int temp = Serial.read();
}

}

void loop() {
if(Serial.available()){
int temp = Serial.read();
// *** Replace these lines with your sensor reading code

// I used a ping sensor to measure distance
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
int value = uS / US_ROUNDTRIP_CM; // convert from time to distance

// try looking at a floating analog input
//int value = analogRead(A1); // touch the A1 pin and you will see the value change

// *** end of sensor reading code (make sure you store as an integer called value)
Serial.write(value/256);
Serial.write(value%256);
}

}

Hi
It is work

This is my tablet

Those vertical lines are because the string handling still has failures, but I need to (try) finish my Bluetooth code first. If you make the delay a little bigger, the results will be probably better, I guess.
Did you build it on PC or APDE?

noel
with APDE

Newest version?
I also saw that you used Android Oreo, so did you add extra code for the permissions?

1 Like

5.1
i give permission to bluetooth manually without extra code