Basic examples - 1scale1/sweetbt GitHub Wiki
Connecting your Processing:Android sketch to ArduinoBT device
This will create a Processing:Android sketch that lets you connect to your ArduinoBT device, the small rectangle in the top-left corner turns green if a successful connection is established.
/**
* SweetBlue - Connect
* by Andreas Göransson.
*
* This sketch connects to ArduinoBT. If a connection was successfully
* established it will fill a small rectangle with green color.
*
* Note: Make sure to enable the BLUETOOTH and BLUETOOTH_ADMIN Sketch
* Permissions.
*/
import se.onescaleone.sweetblue.*;
/* Library obj. */
SweetBlue bt;
void setup() {
/* Connect to the ArduinoBT */
if ( bt == null ) {
bt = new SweetBlue( this );
bt.connect( /* ArduinoBT MAC */ );
}
}
void draw() {
/* UI - Bluetooth state */
if ( bt != null ) {
if ( bt.getState() == SweetBlue.STATE_CONNECTED ) {
fill( 0, 255, 0 );
}
else {
noFill();
}
rect( 5, 5, 40, 40 );
}
}
AnalogRead
This example shows you how to read analog data from the ArduinoBT.
/**
* SweetBlue - Analog Read
* by A.Göransson & D.Cuartielles
*
* This sketch connects to ArduinoBT and rotates a rectangle on the
* processing sketch depending on the value read from an analog
* sensor on the ArduinoBT. Rotation is between 0 and 45 degrees.
*
* Note: Make sure to enable the BLUETOOTH and BLUETOOTH_ADMIN Sketch
* Permissions.
*/
import se.onescaleone.sweetblue.*;
/* Library obj. */
SweetBlue bt;
/* Related to the communication */
boolean initiated = false;
long timer = 0;
long timerdelay = 100; // minimum amount of milliseconds between each reading
/* Pin where the sensor is connected */
int PIN = 0;
/* Variable to store read-values in (it has to be int array!) */
int[] val = new int[1];
void setup() {
/* Connect to the ArduinoBT */
if ( bt == null ) {
bt = new SweetBlue( this );
bt.connect( /* ArduinoBT MAC */ );
}
/* Lock PORTRAIT view */
orientation( PORTRAIT );
/* Simplifies position and rotation */
rectMode( CENTER );
/* Enable debug messages? */
//SweetBlue.DEBUG = true;
timer = millis();
}
void draw() {
if ( bt.isConnected() && !initiated ) {
/* Once the board has established a connection, set the pin modes... */
bt.pinMode( PIN, SweetBlue.INPUT );
/* ...but only do it once! */
initiated = true;
}
/* Draw background */
background( 126 );
/* Draw interface */
pushMatrix();
translate( screenWidth/2, screenHeight/2 );
rotate( map(val[0], 0, 1023, HALF_PI / 2, 0) );
fill( 255, 0, 0 );
rect( 0, 0, 400, 400 );
popMatrix();
/* Read value from ArduinoBT every "timerdelay" milliseconds */
if ( bt.isConnected() && (millis() - timer >= timerdelay) ) {
/* Read the digital pin, PIN, from the bluetooth board */
bt.analogRead( PIN, val );
/* Reset the timer */
timer = millis();
}
}
/* When processing stops, send the disconnect command to ArduinoBT */
void stop() {
bt.close();
}
AnalogWrite
This example shows you how to write analog data to the ArduinoBT.
/**
* SweetBlue - Analog Write
* by A.Göransson & D.Cuartielles
*
* Shows how to write analog values to ArduinoBT.
*
* Note: Make sure to enable the BLUETOOTH and BLUETOOTH_ADMIN Sketch
* Permissions.
*/
import se.onescaleone.sweetblue.*;
/* Library obj. */
SweetBlue bt;
/* Related to the communication */
boolean initiated = false;
long timer = 0;
long timerdelay = 100; // minimum amount of milliseconds between each reading
/* Pin where the sensor is connected */
int PIN = 0;
/* Slider UI */
int[] pos = new int[] {
0, 400
};
int[] dim = new int[] {
50, 200
};
boolean sliding = false;
void setup() {
/* Connect to the ArduinoBT */
if ( bt == null ) {
bt = new SweetBlue( this );
bt.connect( /* ArduinoBT MAC */ );
}
/* Lock PORTRAIT view */
orientation( PORTRAIT );
/* Enable debug messages? */
//SweetBlue.DEBUG = true;
//timer = millis();
}
void draw() {
/* Draw the background */
background( 126 );
/* Draw Slider UI */
fill( (sliding ? 0 : 255 ) );
rect( pos[0], pos[1], dim[0], dim[1] );
}
boolean surfaceTouchEvent( MotionEvent event ) {
/* Make sure to only interact with this element if bluetooth is connected */
if ( bt.isConnected() ) {
if ( event.getAction() == MotionEvent.ACTION_DOWN ) {
/* Try to initiate the sliding */
if ( (event.getX() > pos[0] && event.getX() < (pos[0] + dim[0])) &&
(event.getY() > pos[1] && event.getY() < (pos[1] + dim[1])) ) {
/* Selected, set sliding to true */
sliding = true;
bt.pinMode( PIN, SweetBlue.OUTPUT );
}
}
else if ( event.getAction() == MotionEvent.ACTION_MOVE ) {
if ( sliding ) {
/* Set slider value */
pos[0] = constrain( (int)event.getX(), 50, screenWidth - 50 );
/* Send to bluetooth, but only if enough time has passed */
if ( millis() - timer >= timerdelay ) {
bt.analogWrite( PIN, (int)map(pos[0], 50, screenWidth - 50, 0, 255) );
/* Reset timer */
timer = millis();
}
}
}
else if ( event.getAction() == MotionEvent.ACTION_UP ) {
/* Stop sliding */
sliding = false;
/* Reset the pinmode */
bt.pinMode( PIN, SweetBlue.INPUT );
}
}
return super.surfaceTouchEvent( event );
}
DigitalRead
This example shows you how to read digital data from the ArduinoBT.
/**
* SweetBlue - Analog Read
* by A.Göransson & D.Cuartielles
*
* This sketch connects to ArduinoBT and rotates a shape on the
* processing sketch depending on the value read from an analog
* sensor on the ArduinoBT.
*
* Note: Make sure to enable the BLUETOOTH and BLUETOOTH_ADMIN Sketch
* Permissions.
*/
import se.onescaleone.sweetblue.*;
/* Library obj. */
SweetBlue bt;
/* Related to the communication */
boolean initiated = false;
long timer = 0;
long timerdelay = 100; // minimum amount of milliseconds between each reading
/* Pin where the sensor is connected */
int PIN = 17;
/* Variable to store read-values in (it has to be int array!) */
int[] val = new int[1];
void setup() {
/* Connect to the ArduinoBT */
if ( bt == null ) {
bt = new SweetBlue( this );
bt.connect( /* ArduinoBT MAC */ );
}
/* Lock PORTRAIT view */
orientation( PORTRAIT );
/* Enable debug messages? */
//SweetBlue.DEBUG = true;
timer = millis();
}
void draw() {
if ( bt.isConnected() && !initiated ) {
/* Once the board has established a connection, set the pin modes... */
bt.pinMode( PIN, SweetBlue.INPUT );
/* ...but only do it once! */
initiated = true;
}
/* Draw background, black or white */
background( (val[0] == SweetBlue.HIGH) ? 255 : 0 );
/* Read value from ArduinoBT every "timerdelay" milliseconds */
if ( bt.isConnected() && (millis() - timer >= timerdelay) ) {
/* Read the digital pin, PIN, from the bluetooth board */
bt.digitalRead( PIN, val );
/* Reset the timer */
timer = millis();
}
}
/* When processing stops, send the disconnect command to ArduinoBT */
void stop() {
bt.close();
}
DigitalWrite
This example shows you how to write digital data to the ArduinoBT.
/**
* SweetBlue - Digital Write
* by A.Göransson & D.Cuartielles
*
* Desc...
*
* Note: Make sure to enable the BLUETOOTH and BLUETOOTH_ADMIN Sketch
* Permissions.
*/
import se.onescaleone.sweetblue.*;
/* Library obj. */
SweetBlue bt;
/* Related to the communication */
boolean initiated = false;
long timer = 0;
long timerdelay = 100; // minimum amount of milliseconds between each reading
/* Pin where the sensor is connected */
int PIN = 0;
/* UI Button */
int[] pos = new int[] {
50, 50
};
int[] dim = new int[] {
200, 400
};
boolean btnstate = false;
void setup() {
/* Connect to the ArduinoBT */
if ( bt == null ) {
bt = new SweetBlue( this );
bt.connect( /* ArduinoBT MAC */ );
}
/* Lock PORTRAIT view */
orientation( PORTRAIT );
/* Enable debug messages? */
//SweetBlue.DEBUG = true;
//timer = millis();
}
void draw() {
if ( bt.isConnected() && !initiated ) {
/* Once the board has established a connection, set the pin modes... */
bt.pinMode( PIN, SweetBlue.OUTPUT );
/* ...but only do it once! */
initiated = true;
}
/* Draw the background */
background( 160 );
/* Draw Button UI */
fill( (btnstate ? 0 : 255 ) );
rect( pos[0], pos[1], dim[0], dim[1] );
}
boolean surfaceTouchEvent( MotionEvent event ) {
/* Make sure to only interact with this element if bluetooth is connected */
if ( bt.isConnected() ) {
if ( event.getAction() == MotionEvent.ACTION_UP ) {
/* Change btn state */
btnstate = !btnstate;
/* Write BT */
if ( btnstate )
bt.digitalWrite( PIN, SweetBlue.HIGH );
else
bt.digitalWrite( PIN, SweetBlue.LOW );
}
}
return super.surfaceTouchEvent( event );
}
/* When processing stops, send the disconnect command to ArduinoBT */
void stop() {
bt.close();
}