Hi guys, Im working on a project that takes OSC commands from my phone, parses them, then sends them to my arduino via 2 Xbees.
Im pretty sure I have everything set up right thus far but there seems to be some problem with the way my Processing sketch sends the serial data to the arduino.
The arduino sketch takes 6 characters, first 3 identify the command, second three are the value.
If I open up Arduinos serial monitor and type the command "led255" the led value is properly written. It works.
Using Putty I have to go to 'Terminal' settings and check the option 'Force On' for Local line ending. Then it works as well.
However when my Processing sketch sends the data its not being read. I feel like it has something to do with line endings or something.
The processing sketch first prints the values for debug before writing to terminal so Im pretty sure theyre right..
Here is the Processing sketch:
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
boolean sliderNeedsRedraw = true;
boolean square4NeedsRedraw = true;
int [] sliderStrip = new int [5];
float [] fader = new float [6];
int redbutton = 0;
int red2button = 0;// redbutton lets us know if the button is on or off
int greenbutton = 0;
int green2button = 0;
int [] button = new int [8]; // Array allows us to add more toggle buttons in TouchOSC
void setup() {
size(325,500); // 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()[0], 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
button[i] = int(theOscMessage.get(0).floatValue()); // Puts button value into button[i]
// Button values can be read by using button[0], button[1], button[2], etc.
}
if(addr.indexOf("/1/fader") !=-1){ // one of the faders
String list[] = split(addr,'/');
int i = int(list[2].charAt(5) - 0x30);
fader[i] = theOscMessage.get(0).floatValue();
int j = int(fader[i]*255);
int fadeval = j; // map(j,0,100,0,255);
String sendme = "led" +j;
println(sendme); // uncomment to see x values
arduinoPort.write(sendme +" ");
sliderNeedsRedraw = true;
}
}
void draw() {
background(50); // Sets the background to a dark grey, can be 0-255
if(button[1] == 0){ // If button button 1 if off do....
arduinoPort.write("r"); // Sends the character “r” to Arduino
redbutton = 0; // Sets redbutton color to 0, can be 0-255
}
if(button[1] == 1){ // If button button 1 is ON do...
arduinoPort.write("R"); // Send the character “R” to Arduino
redbutton = 255; // Sets redbutton color to 255, can be 0-255
}
if(button[2] == 0){ // If button button 1 if off do....
arduinoPort.write("w"); // Sends the character “r” to Arduino
red2button = 0; // Sets redbutton color to 0, can be 0-255
}
if(button[2] == 1){ // If button button 1 is ON do...
arduinoPort.write("s"); // Send the character “R” to Arduino
red2button = 255; // Sets redbutton color to 255, can be 0-255
}
if(button[3] == 0){ // If button button 1 if off do....
arduinoPort.write("r"); // Sends the character “r” to Arduino
greenbutton = 0; // Sets redbutton color to 0, can be 0-255
}
if(button[3] == 1){ // If button button 1 is ON do...
arduinoPort.write("R"); // Send the character “R” to Arduino
greenbutton = 255; // Sets redbutton color to 255, can be 0-255
}
if(button[4] == 0){ // If button button 1 if off do....
arduinoPort.write("w"); // Sends the character “r” to Arduino
green2button = 0; // Sets redbutton color to 0, can be 0-255
}
if(button[4] == 1){ // If button button 1 is ON do...
arduinoPort.write("s"); // Send the character “R” to Arduino
green2button = 255; // Sets redbutton color to 255, can be 0-255
}
fill(0,redbutton,0); // Fill rectangle with redbutton amount
rect(25, 450, 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
fill(0,red2button,0);
rect(100, 450, 50, 50);
fill(0,greenbutton,0);
rect(175, 450, 50, 50);
fill(0,green2button,0);
rect(250, 450, 50, 50);
}
And the Arduino sketch:
PLEASE IGNORE COMMENTS
String readString, val1, val2;
const int led = 13;
void setup() {
Serial.begin(9600);
Serial.println("servo-test-21"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(10);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //see what was received
// expect a string like 07002100 containing the two servo positions
val1 = readString.substring(0, 3); //get the first four characters
val2 = readString.substring(3, 6); //get the next four characters
Serial.println(val1); //print ot serial monitor to see results
Serial.println(val2);
int n1; //declare as number
int n2;
char carray1[6]; //magic needed to convert string to a number