ARDUINO TO PROCESSING HADNSHAKE PROBLEM, HUGE LAGS!

Hi,

I need to send X and Y coordinates from processing to Arduino in order to control some lights, I was able to send easily just the X position but when it came to reading a longer string with "x" and "y" coordinates, I started to have problems with synchronization and hand shakes, so I started to work on very easy scripts and now I realize that processing takes too long to react and listen. I have a two very small scripts that I took from sparkfun

https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/tips-and-tricks

The main problem is that when I press the mouse to send a "1" to arduino so it turns ON an LED, it takes ages to send the data to arduino. I do not know if it is a problem with my code or it's just the processing serial library, I have tried using a serial terminal sending the "1" to arduino and it reacts very quickly, so I guess Im doing something wrong in processing.

Both codes attached.

Thanks in advance

SPARKFUNCODE.ino (762 Bytes)

PROC_HANDSHAKE_F.pde (1.16 KB)

I don't know Processing and I don't see an obvious problem with your Arduino code although it is not very robust. You may be interested in the examples in Serial Input Basics

I wonder if your ELSE clause is in the correct place - it will send "Hello World" when the serial buffer is empty and that could be the cause of a problem as it probably sends "Hello World" very frequently.

...R

Whether or not you receive anything, you wait a tenth of a second, and you're whinging about responsiveness?

   if (val.equals("It's ON bitch")){
     myPort.clear();

After some crude comments are received, you dump random amounts of unread data. Stupid!

  else {//if we've already established contact, keep getting and parsing data
    println(val);
    
    if (mousePressed== true)
    {
     myPort.write('1');
     println("1");
    }
     //when you've parsed the data you have, ask for more:
     myPort.write("Give me more data");
    }  
   }

Send "Give me more data" if the mouse is not pressed. If it is, send "1Give me more data". That doesn't inspire confidence...

Where do you ACTUALLY "send X and Y coordinates from processing to Arduino"?

Plus what Groove said, except that I think he misspelled whining, pissing, and moaning.

Post the code that ACTUALLY does what you say you want to do.

Thanks Robin, Already checking it.

Groove, ok, I've already erased the delay in both functions, and it improved, thanks.

void loop()
{
 if (Serial.available() > 0) { // If data is available to read,
   val = Serial.read(); // read it and store it in val

   if(val == '1') //if we get a 1
   {
      ledState = !ledState; //flip the ledState
      digitalWrite(ledPin, ledState); 
   }
   //delay(100);
 } 
   else {
   Serial.println("Hello, world!"); //send back a hello world
   //delay(100);
   }
}

PaulS, hahahah, maan don't get to angry, i follow you, but before posting the other code, I really want to know why this is not workin', i'm still a newbie and I want to learn.
What do you mean by this:
"Send "Give me more data" if the mouse is not pressed. If it is, send "1Give me more data". That doesn't inspire confidence.."

Thanks guys

OK,

Made some changes, it improved a lot.

SPARKFUNCODE.ino (766 Bytes)

PROC_HANDSHAKE_F.pde (1.1 KB)