sending 2 values from Processing to Arduino

Dear all

I thought it would be an easy task but I am wrong! I am interested in sending two int values from Processing to Arduino, and I have the following code:

The idea is very simple. I just want to turn on the LED when the mouse is down. I know I could have just used 1 value, but I want to experiment with 2 values serial communication.....

Any help is much appreciated!

Arduino:

int led = 13;

int incomingByte1, incomingByte2;

void setup() {
  // initialize serial communication
  Serial.begin(9600);
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
  digitalWrite(led, LOW); 

}

void loop() {
  if (Serial.available() >= 2) {
      incomingByte1 = Serial.read();
      incomingByte2 = Serial.read();
  }     
  if (incomingByte1 == 123 && incomingByte2 == 10)
  {  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  } else {
      digitalWrite(led, LOW);   // turn the LED on (HIGH is the voltage level)
  } 
}

Processing

 // import the serial library
 import processing.serial.*;
 
 // create a serial port variable
 Serial myPort;
 
 void setup(){
   size(300,300);
  println("Available serial ports:");
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[2], 9600);
 }


void draw(){

     int value1=123;
     int value2=10;
         
    if (mousePressed){
      value2=10;       
   }
       else {
       value2=11;
       }
       
       myPort.write(value1);
       myPort.write(value2);
}

Any help is much appreciated!

OK. What do you need help with?

Is Processing sending data in ASCII format or binary format? That is crucial to how you read data.

The codes I posted did not work! Can anyone tell me what went wrong?

I am only sending two integers from Processing...

I am only sending two integers from Processing...

Are you? Or, are you sending the string representations of two integers? In other words, are you sending 123 and 10 or "12310"?

I am not so sure how myPort.write() actually works.

as you can see in the code. I tried to write value1 and value2 in myPort. Both values1 and values2 are interget.... Did they get converted into other format when using write()? I would really appreciate it if anyone can clarify this...

       myPort.write(value1);
       myPort.write(value2);

I would really appreciate it if anyone can clarify this...

Why don't you do it? Have the Arduino read a value from the serial port, and print it back.

while(Serial.available() > 0)
{
    int inVal = Serial.read();
    Serial.print("I got: ");
    Serial.println(inVal, DEC);
}

Add a serialEvent callback to the Processing side, along with a call to myPort.bufferUntil() (to wait to call the callback until the \n arrives). Use print() in the serialEvent() function to echo to the Processing IDE window what came in the serial port.

If you get back "I got: 123" and "I got: 10", then the values are not converted. If you get back 5 strings ("I got: 31", etc.) then the values are converted to strings.

And you'll remember it far better then if we tell you the answer. In addition, you'll have learned something about how to debug your own programs.