TouchOsc D-Pad to Processing
in
Integration and Hardware
•
1 year ago
Hi,
I've created an omnibot hacked robot using an arduino, ardumoto shield, and a bluetooth mate shield. Everything is working just fine as is, however i'ld like to try to create an interface to control this with my iPhone. It seems that I should be able to get this to work using TouchOsc and Processing. I've found a few tutorials now and its gotten me halfway there.
Here's the code i'm using at the moment. And my touchOsc template has one toggle button labelled toggle1.
//-----------------Processing code-----------------
import oscP5.*; // Load OSC P5 library
import netP5.*; // Load net P5 library
import processing.serial.*; // Load serial library
Serial arduinoPort; // Set arduinoPort as serial connection
OscP5 oscP5; // Set oscP5 as OSC connection
int redLED = 0; // redLED lets us know if the LED is on or off
int [] led = new int [2]; // Array allows us to add more toggle buttons in TouchOSC
void setup() {
size(100,100); // Processing screen size
noStroke(); // We don’t want an outline or Stroke on our graphics
oscP5 = new OscP5(this,8000); // Start oscP5, listening for incoming messages at port 8000
arduinoPort = new Serial(this, Serial.list()[4], 9600); // Set arduino to 9600 baud
}
void oscEvent(OscMessage theOscMessage) { // This runs whenever there is a new OSC message
String addr = theOscMessage.addrPattern(); // Creates a string out of the OSC message
if(addr.indexOf("/1/toggle") !=-1){ // Filters out any toggle buttons
int i = int((addr.charAt(9) )) - 0x30; // returns the ASCII number so convert into a real number by subtracting 0x30
led[i] = int(theOscMessage.get(0).floatValue()); // Puts button value into led[i]
// Button values can be read by using led[0], led[1], led[2], etc.
}
}
void draw() {
background(50); // Sets the background to a dark grey, can be 0-255
if(led[1] == 0){ // If led button 1 if off do....
arduinoPort.write("3"); // Sends the character “3” to Arduino via bluetooth to turn off leds.
redLED = 0; // Sets redLED color to 0, can be 0-255
}
if(led[1] == 1){ // If led button 1 is ON do...
arduinoPort.write("1"); // Send the character “1” to Arduino via bluetooth to turn on leds.
redLED = 255; // Sets redLED color to 255, can be 0-255
}
fill(redLED,0,0); // Fill rectangle with redLED amount
ellipse(50, 50, 50, 50); // Created an ellipse at 50 pixels from the left...
// 50 pixels from the top and a width of 50 and height of 50 pixels
}
//----------------------------------end processing code------------------------------------
What i'ld like to do is have 4 push buttons instead of toggle buttons that are arranged in touchOsc like a d-pad. The idea being that i can then hold down one of the 4 buttons, and have the robot move in that direction.
Any help would be greatly appreciated.
1