We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I am trying to output values from a slider in Android Mode (using the Controlp5 and Ketai libraries) to an Arduino. I am posting my working code (built from some example codes I found online) here for reference. The original code used simple on/off buttons to send characters to the Arduino. I am a newbie, so from building this example I taught myself some basics of how to send characters from the Android over Bluetooth.
Now I would like to send a range of values corresponding to a couple of sliders. I have been able to successfully create the sliders in my code, but I am really struggling to understand how to get these values to output to an Arduino like I did for each button.
I have since gone through and commented out all of the old, unnecessary lines corresponding to the buttons, but I was hoping to use them for reference/modify them to output the slider values.
Any help at all would be greatly appreciated. Thank you.
// reference for basic button control code: http://stormkitz.blogspot.com/
// references for adding sliders: http://wiki.bk.tudelft.nl/toi-pedia/Processing_Buttons_and_Sliders
// https://www.kasperkamperman.com/blog/processing-code/controlp5-library-example1/
// http://coretechrobotics.blogspot.com/2013/12/controlling-arduino-with-android-device.html
// http://forum.arduino.cc/index.php?topic=194265.0
// http://forum.arduino.cc/index.php?topic=193316.0
import android.content.Intent;
import android.os.Bundle;
import ketai.net.bluetooth.*;
import ketai.ui.*;
import ketai.net.*;
import controlP5.*; //KZ: slider
import cc.arduino.*; //KZ: slider
PFont fontMy; //declaring font
boolean bReleased = true; //no permament sending when finger lets off the button
KetaiBluetooth bt; // Create object from BtSerial class
boolean isConfiguring = true;
String info = "";
KetaiList klist;
ArrayList devicesDiscovered = new ArrayList(); //store in array the discovered device
boolean rectOver = false;
int rec = 0;
ControlP5 controlP5; //KZ: slider
Arduino arduino; //KZ: slider
int DC_speed = 0; // 0-6 //KZ: slider
int DC2_speed = 0; // 0-6 //KZ: slider
int DC3_speed = 0; // 0-6 //KZ: slider
// The following code is required to enable bluetooth at startup.
void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
bt = new KetaiBluetooth(this); //create the BtSerial object that will handle the connection
}
void onActivityResult(int requestCode, int resultCode, Intent data)
{
bt.onActivityResult(requestCode, resultCode, data); //to show the discovered device
}
void setup()
{
size(displayWidth, displayHeight); //size of the phone screen
smooth();
frameRate(10); //the frame rate of my screen
orientation(PORTRAIT); //vertical
bt.start(); //start listening for BT connections
isConfiguring = true; //at my phone start select device…
fontMy = createFont("SansSerif", 30); //font size
textFont(fontMy);
controlP5 = new ControlP5(this);
controlP5.addSlider("DC_speed",0,6,DC_speed,100,200,50,500); //KZ: slider
controlP5.addSlider("DC2_speed",0,6,DC2_speed,330,200,50,500); //KZ: slider
controlP5.addSlider("DC3_speed",0,6,DC3_speed,550,200,50,500); //KZ: slider
}
void draw()
{
//at app start select device
if (isConfiguring)
{
ArrayList names;
//create the BtSerial object that will handle the connection
//with the list of paired devices
klist = new KetaiList(this, bt.getPairedDeviceNames());
isConfiguring = false; //stop selecting device
}
//else
//{
//color a = color(255,0,0); //KZ: red- main axis
//color b = color(255,0,0); //KZ: red- main axis
//color d = color(255,0,0); //KZ: red- main axis
//color i = color(95, 191, 187); //KZ: aqua- background of dots???
//update(mouseX, mouseY); //update our finger point at where of the screen
//background(95, 191, 187); //background color
//if
//((mousePressed)&&(rectOver)&&(rec==2))
//{ d = color(10,237,26); //KZ: changes d from red to green
//}
//else if ((mousePressed)&&(rectOver)&&(rec==3))
//{ a = color(10,237,26); //KZ: changes a from red to green
//}
//else if ((mousePressed)&&(rectOver)&&(rec==4))
//{ b = color(10,237,26); //KZ: changes a from red to green
//}
// if ((rec == 2) && (rectOver)&&(mousePressed) && (bReleased == true)) { // If our finger is on the square,
// byte[] data = {'w'}; // send w to arduino when we click the button 2
// bt.broadcast(data); //send with bt
//bReleased = false; // send data for once until next time we click the button again
// }
// if ((rec == 3) && (rectOver)&&(mousePressed) && (bReleased == true)) {
// byte[] data = {'a'}; // send a to arduino when we click the button 3
// bt.broadcast(data); //send with bt
// bReleased = false;
// }
// if ((rec == 4) && (rectOver)&&(mousePressed) && (bReleased == true)) {
// byte[] data = {'d'}; // send d to arduino when we click the button 4
// bt.broadcast(data); //send with bt
// bReleased = false;
// }
// if((rectOver)&&(mousePressed == false)&& (bReleased == false)) { //when our finger move up from the button, send stop command to arduino
// byte[] data = {' '};
// bt.broadcast(data);
// bReleased = true;
//}
//fill(a); //fill each area of button with the color declared above
//stroke(162); //the shape covered with a grey color line
//triangle(350,600,450,550,350,500); //draw the triangle with the coordinates //KZ: FR
//fill(b);
//triangle(350,350,450,300,350,250); //KZ: 2x. FL
//fill(d);
//triangle(150,250,50,300,150,350); //KZ: 4x. BL
//}
//to print received data and show on screen
fill(255);
noStroke();
textAlign(LEFT);
text(info, 20, 104);
noFill();
}
//void update(int x, int y) { //to control the flag when we click a button
// if ( overRect(350, 500, 100, 100) ) { //x, y, width, height
// rectOver = true;
// rec = 3; //Confirmed: Front Right
//}
//else if
//( overRect(350, 250, 100, 100) ) { //x, y, width, height
// rectOver = true;
// rec = 4; //Confirmed: Front Left
// }
// else if
// ( overRect(50, 250, 100, 100) ) { //x, y, width, height
// rectOver = true;
// rec = 2; //Confirmed: Back Left
// }
//else
// {
// rectOver = false; //nothing s touched on screen
//}
//}
//boolean overRect(int x, int y, int width, int height) { //KZ: to scan what area is touched
// if (mouseX >= x && mouseX <= x+width &&
// mouseY >= y && mouseY <= y+height) //to see if the mouse cursor inside rect
//{
// return true;
// } else {
// return false;
// }
//}
void onKetaiListSelection(KetaiList klist)
{
String selection = klist.getSelection(); //select the device to connect
bt.connectToDeviceByName(selection); //connect to the device
klist = null; //dispose of bluetooth list for now
}
//Call back method to manage data received
void onBluetoothDataEvent(String who, byte[] data)
{
if (isConfiguring)
return;
//received
info += new String(data);
if(info.length() > 150) //clean the words on screen if string to long
info = "";
}//END of Android processing coding
Answers
Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit
ctrl+o
. Leave an empty line above and below your block of code. The above code seems to have a different formatting. Did you copied from a webpage?Kf
Ok code is now formatted. Thank you very much for the comment. Any thoughts on how to output the slider values?
Ok I figured out how to approximate what I want. Ideally, I would still like to send a smooth range of values from my slider, but instead I am sending incremental values (integers). Here is the updated code. However, I would still welcome any comments if anyone has a better way for me to output these values in an analog fashion.