Android Bluetooth to Arduino and vice versa

hi all

i made 2 sliders sending values to arduino

i want to use same in android mode and control arduino via bluetooth using ‘ketai’ library and i need to know how

import controlP5.*;
import processing.serial.*;
 
ControlP5 cP5a;
ControlP5 cP5b;
 
Serial arduino;
 
void setup() {
  size(350,120);
 printArray(Serial.list());
 
  arduino = new Serial(this, Serial.list()[0], 9600);
 
  cP5a = new ControlP5(this);
  cP5a.addSlider("CYCLE_TIME", 999, 1999,999, 10, 10, 255, 35);
  cP5b = new ControlP5(this);
  cP5b.addSlider("PULSE", 55, 455, 55, 10, 55, 255, 35);
}
 
void draw()  {
  background(0);
 
  }
 
void controlEvent(ControlEvent theEvent) {
  if(theEvent.isController()) {
 
 print("control event from : "+theEvent.getController().getName());
 println(", value : "+theEvent.getController().getValue());
 
 if(theEvent.getController().getName()=="CYCLE_TIME") {
   int val1 = int(theEvent.getController().getValue());
   arduino.write("a" + val1+"\n");
 
 }
 
 if(theEvent.getController().getName()=="PULSE") {
   int val2 = int(theEvent.getController().getValue());
   arduino.write("b" + val2+"\n" );
   }
  }
}
1 Like

Hi, @jafal See the first file here for an example. The setup wiring image you can find in the README.

1 Like

hi
thanks for you a lot i know how to wir tx with rx i just need to add my code via andoid with bt

thanks

It’s in this file.

1 Like

thanks again sir

its very long code

i am just learning if you please explain to me what add to my code

Extracted code necessary for you would be the below.
It’s not clear to me how you are sending the string, How is it split in your Arduino code to receive two separate values for each slider?

import ketai.net.bluetooth.*;

String device_name = "HC-05";
//String device_mac = "40:45:DA:00:25:05";

KetaiBluetooth bt;

void setup() {
  bt.getPairedDeviceNames();
  bt.start();
  //bt.connectDevice("40:45:DA:00:25:05");
  bt.connectToDeviceByName(device_name);
}

void send() {
  String str = "<"+str(pgr_r)+",0,"+str(button_toggle_value)+">";
  byte[] data = str.getBytes();
  //bt.write(device_mac, data);
  bt.writeToDeviceName(device_name, data);
  //OscMessage m = new OscMessage(str);
  //bt.broadcast(m.getBytes());
}

void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  bt = new KetaiBluetooth(this);
}
1 Like

hi

here is my result using serial and arduino

the sliders controls my signal as i want

controls my signal as i want

hi

where to use mine in you code ???`

Nice looking program.
Well you didn’t tell me how you split the string on the Arduino site, but if it’s working I guess it would be like:

import ketai.net.bluetooth.*;
import controlP5.*;
import android.os.Bundle;

ControlP5 cP5a;
ControlP5 cP5b;

String device_name = "HC-05";
//String device_mac = "40:45:DA:00:25:05";

KetaiBluetooth bt;

void setup() { 
  cP5a = new ControlP5(this);
  cP5a.addSlider("CYCLE_TIME", 999, 1999,999, 10, 10, 255, 35);
  cP5b = new ControlP5(this);
  cP5b.addSlider("PULSE", 55, 455, 55, 10, 55, 255, 35);
  bt.getPairedDeviceNames();
  bt.start();
  //bt.connectDevice("40:45:DA:00:25:05");
  bt.connectToDeviceByName(device_name);
}

void draw() {
}

void controlEvent(ControlEvent theEvent) {
  if (theEvent.isController()) {
    print("control event from : "+theEvent.getController().getName());
    println(", value : "+theEvent.getController().getValue());
    if (theEvent.getController().getName()=="CYCLE_TIME") {
      int val1 = int(theEvent.getController().getValue());
      send("a" + val1+"\n");
    }
    if (theEvent.getController().getName()=="PULSE") {
      int val2 = int(theEvent.getController().getValue());
      send("b" + val2+"\n");
    }
  }
}

void send(String str) {
  byte[] data = str.getBytes();
  //bt.write(device_mac, data);
  bt.writeToDeviceName(device_name, data);
  //OscMessage m = new OscMessage(str);
  //bt.broadcast(m.getBytes());
}

void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  bt = new KetaiBluetooth(this);
}
1 Like

But you have an integer in the string, so you’ll have to adapt that part using str(int), and convert it in the Arduino code again.

1 Like

thanks for you

its easy for me control arduino its work with sliders very fine i well try your code

this is the arduino code

  void bob() {  
  
 
  
     if (Serial.available() >= 2)
 
  {
    int value2;
    char command = Serial.read();
    switch (command)
    {
    case 'a': value2 = Serial.parseInt();
      CYCLE_TIME = value2;
     
      {
      if(CYCLE_TIME > CYCLE_TIME_MAX)CYCLE_TIME = CYCLE_TIME_MAX;
       }
     
      {
      if(CYCLE_TIME < CYCLE_TIME_MIN) CYCLE_TIME = CYCLE_TIME_MIN;
      }
     
      
      break;

    case 'b': value2 = Serial.parseInt();
     TX_PULSE = value2;
       
      {
       if(TX_PULSE < TX_PULSE_MIN) TX_PULSE = TX_PULSE_MIN;
       }
      
      {
      if(TX_PULSE > TX_PULSE_MAX) TX_PULSE = TX_PULSE_MAX;
      }
      break;

   
     }
    }
  }

Don’t forget to give all Bluetooth permissions.

1 Like

Don’t forget to give all Bluetooth permissions.

what do you mean ?

In the Processing’s IDE Android mode Menu/Android/Sketch permissions. Or are you using APDE?

1 Like

i am using Processing IDE on pc but my sketch i use java mode until now i enable the android mode but did not use it yet

If you want to test quickly, download the app APDE on the Play Store. It’s a tool that runs Processing for Android on the device itself, and compiles in seconds.

1 Like

i have it on my tab i work some sketches but i do not know how to import libraries yet i am new to processing what i am thinking to do is to make a software defined metal detector same as software defined radio i am planing to make a smart metal detector which can react and learn and update itself it is fantasia idea but i make progress

processing is a very great amazing program

The easiests way is to copy the library existent on your PC to the sketchbook libraries folder. If the libraries folder doesn’t exist, create one. Here is a link of a working Ketai lib. Just unzip it into the libraries folder.

1 Like

i mean import the libraries to app APDE i do not know how to

Exactly how I said. on your tablet download the link given above. Make a folder libraries in the Sketchbook folder, and unzip the ketai folder into it. That’s all

1 Like