[SOLVED] Problems sending data from Processing to Arduino through serial port

Thanks @glv and @RichardDL for your proposals.

I had applied your tips and I had gotten some improvements.

If I change Serial.list()[2] for “COM17”, Processing detect an error:

I had changed the name my Serial Port, to no confuse anyone ;p

Working the SimpleWrite example, it seems that there is a fast connection (I receive 4 chars (LLLL) at the begin of the connection, but any char more when I move the mouse over/out of the square). If I add a delay(1000); in the setup(), I don’t receive nothing,

These are my sketches now:

_PROCESSING_Tx (SimpleWrite)

/**
 * Simple Write. 
 * 
 * Check if the mouse is over a rectangle and writes the status to the serial port. 
 * This example works with the Wiring / Arduino program that follows below.
 */


import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;        // Data received from the serial port

void setup() 
{
  size(200, 200);
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, Serial.list()[2], 9600);
}

void draw() {
  background(255);
  if (mouseOverRect() == true) {  // If mouse is over square,
    fill(204);                    // change color and
    myPort.write('H');              // send an H to indicate mouse is over square
  } 
  else {                        // If mouse is not over square,
    fill(0);                      // change color and
    myPort.write('L');              // send an L otherwise
  }
  rect(50, 50, 100, 100);         // Draw a square
}

boolean mouseOverRect() { // Test if mouse is over square
  return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}

_ARDUINO_Rx


char serialData;

void setup() 
{
  Serial.begin(9600);
}
 
void loop() 
{  
  if (Serial.available()) 
  {
    // Recieve Serial from the PC
    serialData = Serial.read();

    if (serialData == '0') {Serial.println("HAS REBUT UN 0");}
    if (serialData == '1') {Serial.println("HAS REBUT UN 1");}
    if (serialData == '2') {Serial.println("HAS REBUT UN 2");}      
    if (serialData == '3') {Serial.println("HAS REBUT UN 3");}      
  }
}

Thanks for your help! :wink:

1 Like