[Processing - Arduino] Send multiple data with Serial from Processing to Arduino

Hey guys,

I know that the subject about this problem is recurrent but I didn't find a solution to my problem.
I want to "stream" picture to my Rainbowduino which is link with my Arduino UNO. For that, I use Processing to analyse et getPixel RGB value for each 8x8 pixel of my image. Till this, there is no matter.

Now I have to send all these datas to my Arduino and there is the matter. I didn't manage to send well my data one per one to Arduino. It's like Arduino didn't understand which Processing data corresponding to Arduino Data.

Here is my code, maybe you could help me to this very newbie problem :slight_smile:

Arduino Code :

i#include <Rainbowduino.h>

int xWidth;
int yHeight;
int color;

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

void loop(){
  if (Serial.available() >= 3){
    xWidth = Serial.read();
    yHeight = Serial.read();
    color = Serial.read();
    //Serial.println (xWidth);
    //Serial.println(yHeight);
    Serial.println(color);
    Rb.setPixelXY(xWidth, yHeight, color, color, color);   
  } else {
    Serial.println("Nothing is detected");
    for (int i = 0; i < 8; i++){
      for(int j = 0; j < 8; j++){
        Rb.setPixelXY(i, j, 0, 0, 0);
      }
    }
  }

   delay(2000);
}

Processing Code :

import processing.serial.*;

Serial myPort;
PImage myImage; 
int loc = 0;
int xWidth = 0;
int yHeight = 0;

void setup(){
  size(8, 8);
  myImage = loadImage("FLV.png");
  println(Serial.list());
  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);
}

void draw(){
  image(myImage, 0, 0); 

  for(int x = 0; x < myImage.width; x++){
    for(int y = 0; y < myImage.height; y ++){
      loc = y + x*height;
      xWidth = x;
      yHeight = y;
      String red = hex((int)red(myImage.get(x,y)));
      String green = hex((int)green(myImage.get(x,y)));
      String blue = hex((int)blue(myImage.get(x,y)));
      //println("A la position ", loc, " R = ", red, ", G : ", green, ", B : ", blue);
      
      myPort.write(xWidth);
      println("x: ", xWidth, "sent");
      myPort.write(yHeight);
      println("y : ", yHeight, "sent");
      myPort.write(red);
      println("color : ", red, "sent");
      delay(2000);
      
      if (loc == 63){
        println("--- FINISH ---");
      }
    }
  }
}

Thank you so much :slight_smile: !

Edit : I fixed the code problem. Thanks Robin !

Neither of those pieces of code looks like Arduino code.

...R

Yep, sorry about that, I've fix it

up :slight_smile:

Well you haven't said what 3 numbers you are expecting to receive and what 3 numbers you actually get?

Personally I would write a Processing program that just sends the same 3 numbers until I was happy that the communication to the Arduino works OK and the Arduino can do the right stuff with the numbers it receives.

Then I think I would write another Processing program that breaks the image as needed and prove to myself (without an Arduino connection) that it is doing what I want.

...R

Thank you for you quick answer.
I'm expecting to receive the position x and y of the pixel of my image and his rgb hexadecimal value. I'm working with grey picture this is why I just need one color information (greyscale : r = g = b).
And indeed, I don't receive the good values. In Processing for example, in the console "color : 0000001D sent" but when I checked it on the Arduino monitor, I received a value like "434345"... Maybe it's a delay problem...

I'll try your method Robin and I'will kepp you informed.

Thank you so much !

I don't understand... I'm trying just a basic code and it doesn't work.

Arduino code :

int value;

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

void loop(){
  if(Serial.available() > 0){
    i++;
    value = Serial.read();
    Serial.println(value);
    delay(500);
  }else{
      Serial.println("Nothing is detected");
      delay(500);
  }
}

Processing code :

import processing.serial.*;

Serial myPort;

void setup(){
  size (100, 200);
  println(Serial.list());
  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);
}

void draw(){
  for(int i = 0; i < 10; i++){
    myPort.write(i);
    println(i, " sent");
    delay(500);
  }
}

In my Arduino console, I obtained this :

1
23
5
678
9
0

Why did the "4" is missing ? And why do I have 2 and 3 stick together and 6, 7 and 8 ?

Thanks for help !

Is that the Arduino code you are actually using? I suspect it won't compile because it has "i++" without "i" being defined.

I would reduce the delay in the Arduino code to (say) 100 so it reads more often than the PC sends - at least for testing.

For a practical project you should really enclose the stuff you are sending between start and end markers. Then the Arduino discards anything it receives until it gets a start marker. And then saves all the characters until it detects the end marker.

I wrote a demo here using Python. The principles apply to any programming language. If you only need to send printable characters you could probably use "<" and ">" as the start and end markers.

...R