I need some help.
I am working on a project that is inspired by the Beatfly project, which is a blimp project with 3 motors, some sensors and a handful of RGB LEDs. The original project leveraged Quartz Composer and a few interfaces to control the blimp and I have modified that to leverage TouchOSC iPad application and Processing.
I have all of the communication between Processing and the iPad/TouchOSC working fine and able to read from and write to the iPad.
I am having problems sending data between Processing and the Arduino FIO. I will eventually be using the Xbee Series 1 modules to handle wireless communication, but for now I am using a USB/FDTI cable to upload sketches and debug the communication structure.
What I would like to do is send the data in an array to the arduino. It currently has up to 20 items in the array which are all less than 255 so I am thinking that sending encoded using byte() will be the best method.
The data structure is as follows: 0xF0, 0x51, 15, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, 0xF7 - where X is a value from 0-255
The 0xF0 opens the set and 0xF7 closes the set. 0x51 or 0x52 allow me to detect which type of command is being sent either 0x51 for LEDs and 0x52 for motor control.
I am able to construct and populate the array in processing, open the port and what I understand to be writing to the port.
I have tried a couple things to debug this over the last few evenings. First off I tried to just send a single byte, read it and then react to the data. I have had intermittent success with this, I tried to send multiple bytes and have had no success.
I put the my Arduino code for the project aside for the moment and wrote a very simple serial reading function which prints the value to the serial monitor and if it detects the byte 0xF0 turns on an LED connected to D2 on the FIO.
I have left the code as I am using it with various attempts commented out so you can see the logic and what I have tried to resolve this before posting to the forums.
Arduino Code:
int inByte = 0; // incoming serial byte
void setup()
{
Serial.begin(57600); // native speed of the FIO and Xbees - have tried 9600 as well with no discernable difference
Serial.print(255); // send an initial byte out to the serial buffer to test for receipt
}
void loop()
{
if (Serial.available()) {
inByte = Serial.read();
//Serial.flush(); - tried this earlier to clear the buffer after reading the result, but it did not seem to make any difference
Serial.println(inByte);
}
}
Processing Code snippet:
variables declared before setup:
boolean debug=false;
int [] motors = new int [7];
int redVal;
int greenVal;
int blueVal;
String [] colorLED = new String[5];
int sendComd = 0;
int testByte = -1;
declared in setup()
mySerial = new Serial(this, Serial.list()[0], 57600);
loop from draw()
if(sendComd!=0){
sendCommand(sendComd);
}
//function to send the data to Arduino which is called from the loop in draw()
void sendCommand(int ardCtrl){ // ardCtrl will be either 1 for LED or 2 for Motor
if(debug)println("send command function called");
int[] pass2ard = new int [19];
if(ardCtrl == 1){
pass2ard[0] = 0xF0; //start
pass2ard[1] = 0x51; //LED control
pass2ard[2] = 15; / length of data to read on the arduino
for(int l=0; l<=colorLED.length-1; l++){
color currColor = color(unhex(colorLED[l]));
if(debug)println(currColor);
int cPos = (l*3)+3;
int r = (currColor >> 16) & 0xFF; // Faster way of getting red
int g = (currColor >> 8) & 0xFF; // Faster way of getting green
int b = currColor & 0xFF; // Faster way of getting blue
pass2ard[cPos] = r;
pass2ard[cPos +1] = g;
pass2ard[cPos +2] = b;
}
pass2ard[18] = 0xF7; //end string
//the array that is sent to arduino should look like this "0xF0, 0x51, 15, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, 0xF7" - where X is a value from 0-255
} else if (ardCtrl == 2){
pass2ard[0] = 0xF0; //start
pass2ard[1] = 0x52; //motor control
pass2ard[2] = 7; // the number of bytes to control the motors 1st is speed, 2nd through 7th are directional HIGH/LOW for the H-Bridge
for(int l=0; l<=6; l++){
pass2ard[l+3] = motors[l];
}
pass2ard[10] = 0xF7; //end string
//the array that is sent to arduino should look like this "0xF0, 0x51, 6, X, X, X, X, X, X, 0xF7" - where X is a value
}
mySerial.clear(); //clear the output channel
//mySerial.write(byte(0)); //the first byte is the index.
//mySerial.write(byte(pass2ard[0])); //the second byte is the command.
//delay(40); // send the start string byte to arduino and wait until response come
//reinsert code to read the last byte, validate it was the one we just sent and then proceed to the following
for (int i=1;i<pass2ard.length;i++){
// for (int i=0;i<1;i++){ // temporary test to send only a couple bytes rather than all 19
// while (mySerial.available()>0){
// int last=mySerial.read();
// if(debug)println("last byte: " + last);
// if (int(last)==pass2ard[i-1]){ //confirm that the last element was received
//mySerial.clear();
mySerial.write(char(pass2ard*));*
_ if(debug)println(i + ": " + pass2ard*);_
_ delay(40);_
_// }_
_// }_
_// }_
_ }_
_ sendComd = 0; // set the trigger to zero if successful*_
* if(debug)println("end of function");*
}
Thanks in advance!
Oh and here is the eventual commands I would like to be able to execute on the Arduino:
void loop() {
* if (Serial.available()) {*
* //check to see if the end of the string has been reached - 0xF7*
* buff[0] = Serial.read();*
* delay(10);*
* if(debug) Serial.print("I received: ");*
* if(debug) Serial.println(buff[0], DEC);*
* if (buff[0] == 0xF0){ // wait for the start Sysex string command*
* if(debug) Serial.println("start of string found");*
* if(debug){*
* digitalWrite(2,1);*
* digitalWrite(4,1);*
* digitalWrite(5,1);*
* }*
* state=1;*
* }*
* if (state>0){*
* buff[1]=Serial.read();*
* buff[2]=Serial.read();*
* for (int i=0; i<buff[2]; i++) {*
* buff[i+3]=Serial.read();*
_ if(debug) Serial.println(i + ": " + buff*);
if(buff==0xF7){
if(debug) Serial.print("found end of string");
state=0;
}
if(debug) Serial.println(buff);
}_
if(buff[0] == TLC_MESSAGE){
_ if(debug) Serial.println("LED");
Tlc.set(0, buff[3]);
Tlc.set(1, buff[4]);
Tlc.set(2, buff[5]);
Tlc.set(3, buff[6]);
Tlc.set(4, buff[7]);
Tlc.set(5, buff[8]);
Tlc.set(6, buff[9]);
Tlc.set(7, buff[10]);
Tlc.set(8, buff[11]);
Tlc.set(9, buff[12]);
Tlc.set(10, buff[13]);
Tlc.set(11, buff[14]);
Tlc.set(12, buff[15]);
Tlc.set(13, buff[16]);
Tlc.set(14, buff[17]);_
} else if(buff[0] == MOTOR_MESSAGE){
_ if(debug) Serial.println("Motor");_
analogWrite(MOTOR_ENCODER, buff[3]);
digitalWrite(L_MOTOR_DIR, buff[4]);
digitalWrite(L_MOTOR_PWM, buff[5]);
digitalWrite(R_MOTOR_DIR, buff[6]);
digitalWrite(R_MOTOR_PWM, buff[7]);
digitalWrite(V_MOTOR_DIR, buff[8]);
digitalWrite(V_MOTOR_PWM, buff[9]);
_ }
}
}
if(debug) Serial.println(state);
colorSweep(512);
Tlc.update();
delay(100);
}*_