# Android Bluetooth to Arduino and vice versa

**URL:** <https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578>\
**Category:** Processing for Android\
**Created:** [December 29, 2020, 8:52am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578 "2020-12-29T08:52:21Z")\
**Posts on this page:** 20\
**Page:** 7

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 8, 2021, 2:41pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/122 "2021-01-08T14:41:35Z")

</div>

> [@jafal](#):
>
> noel

hi  
if or when you have time adapt this code for me work with bluetooth in order to make ways for handling data TX/RX via bt

```auto
// 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;

}

}
``
```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 8, 2021, 2:48pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/123 "2021-01-08T14:48:27Z")

</div>

> [@jafal](#):
>
> if you have time

At the moment no, but you could make it a new topic since isn’t Android related.

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 8, 2021, 2:57pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/124 "2021-01-08T14:57:30Z")

</div>

my point handling data via bluetooth not the grafica libaray

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 17, 2021, 11:51am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/125 "2021-01-17T11:51:34Z")

</div>

> [@jafal](#):
>
> my point handling data via bluetooth not the grafica libaray

I will look into this later on. In the meantime, I’ve written a better native code (without Ketai) to better handle receiving data. You can find it in the Gallery category [here.](https://discourse.processing.org/t/android-bluetooth/27140)

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 17, 2021, 12:36pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/126 "2021-01-17T12:36:20Z")

</div>

[noel](https://discourse.processing.org/u/noel)

hi

Thank you for your interest, it seems to be a special work. I will study the code in detail and then apply it … and I will inform you of what is new

Bless you

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 17, 2021, 12:38pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/127 "2021-01-17T12:38:38Z")

</div>

hi

what libraries need in APDE??

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 17, 2021, 12:41pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/128 "2021-01-17T12:41:13Z")

</div>

None. The imports are native libraries; no third-party lib required.

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 17, 2021, 12:45pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/129 "2021-01-17T12:45:17Z")

</div>

i see

i understand thank you

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 17, 2021, 12:56pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/130 "2021-01-17T12:56:38Z")

</div>

> [@jafal](#):
>
> Now grafica library run on APDE

Did you build the extra grafica.dex file needed to make it functional on APDE?

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 17, 2021, 1:02pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/131 "2021-01-17T13:02:12Z")

</div>

yes i did i think i run all the examples also

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 17, 2021, 1:48pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/132 "2021-01-17T13:48:07Z")

</div>

> [@jafal](#):
>
> my point handling data via bluetooth not the grafica libaray

Could you explain what data you are going to use and at what speed,  
I mean, more details, to make a simple example sketch.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 17, 2021, 1:58pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/133 "2021-01-17T13:58:03Z")

</div>

It would be easier if you had something working on the PC to give the idea.

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 17, 2021, 2:04pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/134 "2021-01-17T14:04:53Z")

</div>

i did some on pc with arduino when i test it again i well post it

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 18, 2021, 12:25am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/135 "2021-01-18T00:25:24Z")

</div>

hi noel

this is nice if can run via bluetooth

```auto
import processing.serial.*;

 int adcValue = 0; // value received from Serial 
 // String Unit="mA";
 // String Unit="kΩ";
 // String Unit="°C"; // We can use Unicode chars directly
String Unit="ANALOG READ";  
String Unit1="VALUE";   

// Unicode codes may be entered as well
 
 Serial myPort;
 PFont Segment, Units; 

 void setup() {
                          // Setup the display window 
  size(666, 280); // Size of the window
  Segment = createFont("Segment7", 150); // Assign fonts and size 
  Units = createFont("Arial", 40);
  textFont(Segment);
  textAlign(RIGHT); // Text align 
  fill(250,250,0); // Font color is yellow = red + green

 // println(Serial.list()); // List all the available serial ports
  // Then open the port you're using, my is the first, i.e. '0'
myPort = new Serial(this,"COM3", 9600); 
 // myPort = new Serial(this, Serial.list()[], 9600);
 // don't generate a serialEvent() unless you get a newline character:
   myPort.bufferUntil('\n');
 }

void draw()
{ // Let's start to display
  background(0,0,0); // set the background color black

  textFont(Segment);
  text(adcValue, 311, 250);
  
  textFont(Units);
  text(Unit,433,65);
text(Unit1,511,211);

}

void serialEvent(Serial myPort) 
{
 String inString = myPort.readStringUntil('\n'); // get the ASCII string:
 if (inString != null) 
    {
     inString = trim(inString); // trim off any whitespace
     adcValue = int(inString); // convert into an integer
     adcValue =int(map(adcValue, 0, 1023, 0, 1023)); // possible range adjusting
    }
 }

```

arduino

```auto
#include <SoftwareSerial.h>

SoftwareSerial LEDdisplej(2, 3); //RX pin, TX pin

int adcValue = 0;

void setup() {

      Serial.begin(9600);

  LEDdisplej.begin(9600); // Talk to the Serial7Segment at 9600 bps
  LEDdisplej.write('v'); // Reset the display - this forces the cursor to return to the beginning of the display
}

void loop() 
{
  adcValue = 0; // Filter - average from 16 values 
  for (int i = 1; i<=16; i++)
    adcValue = adcValue + analogRead(0);
  adcValue = adcValue/16;

 
  char tempString[10]; // Used for sprintf
  sprintf(tempString, "%4d",adcValue); // Convert into a string that is right adjusted
  //sprintf(tempString, "%d", adcValue); // Convert into a string that is left adjusted (requires digit 1 command)
  //sprintf(tempString, "%04d", adcValue); // Convert into a string with leading zeros
  //sprintf(tempString, "%4X", adcValue); // Convert int HEX, right adjusted
  
  LEDdisplej.print(tempString); // Send serial string out the soft serial port to the S7S
 
  Serial.println(tempString); // Works too, but we don't need trailing spaces ' ' in the front
  // Serial.println(adcValue);

  delay(100);
}

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 18, 2021, 12:26am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/136 "2021-01-18T00:26:56Z")

</div>

![seg](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/410084b6ea7b02675cf18b2d5caa02f91d048e0a.jpeg)

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 18, 2021, 1:41am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/137 "2021-01-18T01:41:26Z")

</div>

hi noel

The Arduino handles all the operations in my project … and which originally depends on the timing generated through the integrated circuit 4017 … I replaced the integrated with the Arduino automatic, the device became digital … because the resistors that change the timing in the integrated circuit 4017 were dispensed with Arduino

Meaning as follows

All digital operations that have been added to the device are a matter of controlling how to operate, such as replacing the variable resistors with buttons

The final outputs are a signals with a frequency not exceeding 5 kilohertz

this is the timing section of the device all replaced with arduino and works as the original device as can you see 6 variable resistors become sliders instead and working great

all this schematic include ics diodes transistors caps etc… replaced with arduino

 ![okkk](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/891278b33e5c05fc638dbebe3846edeff44a879f.jpeg)

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 18, 2021, 2:05am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/138 "2021-01-18T02:05:44Z")

</div>

here the 4017 timing out range from 2us to 350us the arduino now acts as 4017 exactly

the timing just for the internal use of the device

 ![ssss](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9c2f0a345eaf7b4c35bf1aa80c9999fcff0d57aa.png)

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 18, 2021, 2:33am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/139 "2021-01-18T02:33:02Z")

</div>

noel  
My problem is as follows

Firstly, since last month I only used the processing program, and before that, I never used it

Second

I learned what I want and built the code in separate parts and stages according to each outcome of each stage

Third

I did not know that the serial is completely different from the Android Bluetooth, especially the touch problem in the controlP5 library

I am trying overcome the touch problem of Android and ControlP5  
I made good steps

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 18, 2021, 2:50am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/140 "2021-01-18T02:50:00Z")

</div>

hi noel

I posted a summary of what I am doing and did not mean to give you a headache 😇 😇 ❤ ❤

The help I want from you is that I can deal with the Bluetooth same as I can use the serial

I will study the code you posted . I applied the first sending code from Android to the Arduino it is working and sent to the serial monitor number 255

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 18, 2021, 9:54pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/141 "2021-01-18T21:54:25Z")

</div>

> [@jafal](#):
>
> the integrated circuit 4017

That’s a long time ago that I played with these and 555 as well, I remember.

> [@jafal](#):
>
> I am trying overcome the touch problem of Android and ControlP5

The best way to solve this is by using native android views. In the beginning, it seems complicated, but soon you will be used to it. I actually expected that you would be able to adapt the receiver code because very few changes were necessary. Just change the code in the plot(), draw(), and mouse() functions.  
All the other code you can place in another tab, and don’t need to bother about it. I posted the code [here](https://discourse.processing.org/t/android-bluetooth/27140/3) because this topic is too big for others to search in.

[Previous page](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578.md?page=6)
