Arduino <-> Processing serial communication problems

Hello, Arduino community!

I`m a new explorer in the Arduino world and my last attempt was to write a little program that was supposed to be able to stablish serial communication between Arduino and Processing via USB. I hope this is the proper section for this post, since my main problem is related to serial communication.

So, my objetive is to send RGB analog values that come from the mouse position in Processing window.
The codes:
Arduino:

int outR = 7;
int outG = 8;
int outB = 9;
int serialCount = 0;
int in[4];
int Rv = 0;
int Gv = 0;
int Bv = 0;
boolean firstContact = false;

void setup(){
  Serial.begin(9600);
  pinMode(outR, OUTPUT);
  pinMode(outG, OUTPUT);
  pinMode(outB, OUTPUT);
  
  /* This is a "synchronization"part of the program.
  I did this because I believe the information sent and 
  received is not being well understood when getting 
  from one dispositive to another. I`m not sure if this is
  necessary, but the "establishContact" function found at 
  SerialCallResponse example doesn`t seems to be enough and,
  in a similar example, the "Dimmer", it made the example work for me.*/
  
Sync1:  Serial.write('O');
        delay(10);
        while(!Serial.available()){
          delay(1);
          goto Sync1;
        }
        while(Serial.read() != 'O')goto Sync1;
Sync2:  Serial.write('k');
        delay(10);
        while(!Serial.available()){
          delay(1);
          goto Sync2;
        }
        while(Serial.read() != 'k')goto Sync2;
        
        Serial.flush();
}

void loop(){
}

void serialEvent(){
   int inByte = Serial.read();
   Serial.write(inByte);
   
   if (firstContact == false) {
     if (inByte == 'k') { 
       Serial.flush();          // clear the serial port buffer
       firstContact = true;     // you've had first contact from the microcontroller
       Serial.write('k');       // ask for more
     } 
   } 
   
   else{
     in[serialCount] = inByte;
     serialCount++;
   }
   if(serialCount > 2){
     Rv = in[0];
     Gv = in[1];
     Bv = in[2];
     analogWrite(outR, Rv);
     analogWrite(outG, Gv);
     analogWrite(outB, Bv);
     //debug purpose prints
     Serial.print(Rv);
     Serial.print(" ");
     Serial.print(Gv);  
     Serial.print(" ");
     Serial.println(Bv);
     Serial.write('k');
     //
     serialCount = 0;
  }
}

Processing:

import processing.serial.*;

Serial port;
boolean ready = false;
int read;
char k = 'k';

void setup(){
//  //set Serial
  println(Serial.list());
  port = new Serial(this, Serial.list()[0] , 9600);

//  //end set Serial
  
// //Sync

  char rightAnswer = 'O';
  while(ready == false){
    if (port.available() != 0){
      if(port.read() == rightAnswer) {
        port.write(rightAnswer);
        if(rightAnswer == 'k'){
          ready = true;
          println("Ok");
        }
        rightAnswer = 'k';
      }
    }
  }
  port.clear();
// //End Sync

//  //Set Screen
  size(256, 256);
  for(int x = 0; x < 256; x++){
    for (int y = 0; y < 256; y++){
      stroke(x,y,127);
      point(x,y);
    }
  }
  establishContact();
}

void draw(){
  while (port.available() > 0){
    int a = port.read();
    //Write Red 
    port.write(mouseX); 
    //Write Green
    port.write(mouseY);
    //Write Blue
    port.write(127);
    //wait for a while
    delay(100);
  }
}

void establishContact(){
  while(port.available() <= 0){
    port.write('k');
  }
}

My problem is that, using Serial Monitor, the arduino program runs fine, but by sending the same information through the Procesing program, it doesnt work! It first stucks at establishContact()(keeps sending the 'k', so I believe arduino is not reading 'k' as 'k'), and then stucks in a white window in processing, when it was supposed to be a colored window due to "set Screen"part( the RX and TX keeps blinking during this part, so I believe its a mismatch of information being sent/received).

Thanks in advance,
Lucas Bortoto Rocha

void draw(){
  while (port.available() > 0){
    int a = port.read();
    //Write Red 
    port.write(mouseX); 
    //Write Green
    port.write(mouseY);
    //Write Blue
    port.write(127);
    //wait for a while
    delay(100);
  }
}

Suppose the mouse is at 200, 145. What is the Arduino supposed to do with "200145127"?

Sync1:  Serial.write('O');
        delay(10);
        while(!Serial.available()){
          delay(1);
          goto Sync1;
        }

Try again, when you can think of a way to do this without using goto. You don't think that the while statement uses goto do you?

        Serial.flush();

Now, you want to wait until all pending outgoing serial data has been sent? Why?

Get rid of all the useless calls to Serial.flush().

Thanks for the response, PaulS!
I believe that I understood your points, so I did the following changes:

PaulS:
Suppose the mouse is at 200, 145. What is the Arduino supposed to do with "200145127"?

I changed the sending code (Processing part) to:

while (port.available() > 0){
    int a = port.read();
    //Wr 
    port.write(mouseX);
    [b]port.write(',');[/b]
    //Wg
    port.write(mouseY);
    [b]port.write(',');[/b]
    //Wb
    port.write(127);
   [b] port.write(',');[/b]
    //wait for a while
    delay(100);
  }

And the receiving code to:

void serialEvent(){
   if (firstContact == false) {
    [b] inByte = Serial.read);[/b]
     if (inByte == 'k') { 
       while(Serial.available()) Serial.read();         // clear the serial port buffer
       firstContact = true;     // you've had first contact from the microcontroller
       Serial.write('k');       // ask for more
     } 
   } 
   
   else{
     [b]inByte = Serial.parseInt();[/b]
     in[serialCount] = inByte;
     serialCount++;
   }
  [b] if(serialCount > 5){[/b]
     Rv = in[0];
     Gv = in[1];
     Bv = in[2];
     analogWrite(outR, Rv);
     analogWrite(outG, Gv);
     analogWrite(outB, Bv);
     serialCount = 0;
  }

So, now I send separated values and read only the integers(I thought the read() function would automatically separate the received info).

Try again, when you can think of a way to do this without using goto. You don't think that the while statement uses goto do you?

I replaced that part with this(seems really better):

    while(Serial.read() != 'O'){
      Serial.write('O');
      delay(10);
    }
    while(Serial.read() != 'k'){
      Serial.write('k');
      delay(10);
    }

Now, you want to wait until all pending outgoing serial data has been sent? Why?

I misunderstood the concept of Serial.flush(), I believed it was a buffer cleaner function(I used that because, after debbugging, I noticed that there was a lot of garbage on the buffer after the sync phase), and now I replaced it with:

while(Serial.available()) Serial.read();

I`ll try to find other mistakes, thanks for your time and expertise,
Lucas Bortoto Rocha

Now I am stuck in another problem, the Serial.parseInt() is not reading the values sent by Processing.

Whats is going wrong? When I send "200,200,200"on the arduino Serial Monitor, it works perfectly, but when I use the following Processing code, it just doesn't work! Serial.parseInt() is not detecting 200 as a number! Why not?

    port.write(200);
    port.write(',');
    //Wg
    port.write(200);
    port.write(',');
    //Wb
    port.write(200);
    port.write(',');
    //wait for a while
    delay(100);
    println(port.available());
  }

Problem solved!
I used the following code:

void draw(){
  while (port.available() > 0){
    int a = port.read();
    //Wr
    send(mouseX/2);
    //Wg
    send(mouseY/2);
    //Wb
    send(100);
    //wait for a while
    //delay(100);
//    println(port.read());
//    println(port.read());
//    println(port.read());
  }
}

void send(int value){
    int C = value/100;
    int D = (value - 100*C)/10;
    int U = (value - 100*C - 10*D);
    C = C + '0';
    D = D + '0'; 
    U = U + '0';
    port.write(C);
    port.write(D);
    port.write(U);
    port.write(',');
}