Sending multiple data from processing.

Hi, I'm trying to move two stepper motors depending on the Xpos and Ypos sent from processing. The problem is that I have to send two parameters - I did some research and came across this code on github where the values are converted to bytes.

Here is my processing code.

int Xpos = 1;
int Ypos = 1;
int sizeX = 200;
int sizeY = 200;

import processing.serial.*;

Serial port;


void setup() {
  size(sizeX, sizeY);
  noStroke();

  String portName = Serial.list()[2];
  //println(Serial.list());
  port = new Serial(this, portName, 9600);
}

void draw() {
  int value1 = Xpos;
  int value2 = Ypos;
  
  byte out[] = new byte[2];
  out[0] = byte(value1);
  out[1] = byte(value2);
  port.write(out);
  println(out);



  background(0);
  ellipse(Xpos, Ypos, 20, 20);
  if ((keyPressed == true) && (key == 'r')) {
    if (Xpos > 0) {
      Xpos = Xpos  - 1;
      delay(50);
    }
    if (Ypos > 0) {
      Ypos = Ypos - 1;
      delay(50);
    }
  }
 // println(Ypos, Xpos);
}



void keyPressed() {
  if (key == CODED) {
    if (keyCode == RIGHT) {
      if (Xpos < sizeX) {
        Xpos = Xpos + 1;
      }
    }
    if (keyCode == LEFT) {
      if (Xpos >0) {
        Xpos = Xpos -1;
      }
    }
    if (keyCode == UP) {
      if (Ypos > 0) {
        Ypos = Ypos - 1;
      }
    }
    if (keyCode == DOWN) {
      if (Ypos < sizeY) {
        Ypos = Ypos  +1;
      }
    }
  }
}

What I had in my mind for eg:- if the currentVal for Xpos was greater than preVal for Xpos move the stepper in steps in the positive direction. But how to read the value of Xpos??

I'll include the code from github on reading the byte values, I don't however know what to do with it:(

int currentValue = 0;
int values[] = {0,0};

void setup()  {
  Serial.begin(9600);
} 


void loop() {   
  if(Serial.available()){
    int incomingValue = Serial.read();
    
    
    values[currentValue] = incomingValue;

    currentValue++;
    if(currentValue > 1){
      currentValue = 0;
    }
    
  
  } 
}

Here is a great thread on reading serial data. It will show how to form multiple pieces of data into packets, read the packets reliably, without blocking, and parse the data from the packets.

Thanks for the info. I was hoping that to realise how the ardiuno reads the value being sent to it from the above processing code. I mean how to differentiate [0] or[1]?

Your Processing app needs to built a packet that contains the data in a format readable by the Arduino sketch. For example <3,5'\n'. When that is sent, the Arduino knows that when it sees < that is the beginning of a packet and then the first number after the < is one of your values and the second value follows the comma (,) and the '\n' (line feed sent by println) says end of packet. Robin2, in the linked post, explains all of that much better than I can.

I have gone through that thread couple of times, still bit hard to wrap my head round the matter. With the code that I have pasted above - my understanding is that the int values will have {Xpos,Ypos} but I don't know how to access them.

my understanding is that the int values will have {Xpos,Ypos} but I don't know how to access them.

You are sending two bytes. They will NOT arrive simultaneously.

That would be OK, though, since you only read a byte when there is a byte to read. Why you store the byte in an int is a mystery, but it work.

Once you have received the second byte (when currentValue is greater than one), you should use the data for whatever purpose you send the data. You only know that there are two byte in values for a short period of time, because you reset currentValue to 0 as soon as you discover that currentValue is 2. It is in that short period of time that you need to use the data.

Xpos = values[0] and Ypos = values[1]. Arrays.