Problem with Processing Communication

Hello,

I am using this guide to learn basics about communication between Arduino and Processing.

First part from the tutorial where I'm sending message from Arduino to Processing is working fine. However the second part of the tutorial where message should be send from Processing to Arduino to turn on onBoardLed connected with pin13 doesn't work.
I'm using Arduino Mega 2560.

Description of the problem:
Arduino code is uploaded successfully. Arduino LEDs blink couple of times and then blow out as it is normal during uploading. Arduino is waiting for message from Processing to turn on the onBoardLed. But when I run Processing something weird happens. OnBoardLed and RX Led are on all the time no matter if I'm sending message or not.

I'm doing everything the same as it is in this guide.
Please give me advice what may cause this issue.

Arduino Code:

#define onBoardLed 13

char val;

void setup() {
  Serial.begin(9600);
  pinMode(onBoardLed, OUTPUT);
}

void loop() {
  if(Serial.available() > 0) {
    val = Serial.read();
  }
  if(val == '1') {
      digitalWrite(onBoardLed, HIGH);
    }
    else {
      digitalWrite(onBoardLed, LOW);
    }
delay(50);
}

Processing Code:

import processing.serial.*;

Serial myPort;
String portName;

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

void draw() {
  if(mousePressed == true) {
    myPort.write('1');
    println('1');
  }
  else {
    myPort.write('0'); 
  }
}

I don't know Processing but I suggest you do whatever is needed to put the equivalent of delay(1000) in the draw() function and see what happens.

As things are the Processing program is sending '0's at a ferocious rate.

...R

Thanks for advice but it should work without delay.
I added delay but it didn't solve the problem.

Raadley:
Thanks for advice but it should work without delay.
I added delay but it didn't solve the problem.

Post the revised code so we can see exactly what you did.

What makes you say "it should work without delay" ?

...R

Remove the delay from the Arduino code.

There is no delay needed for the draw function, if you are happy with the 60 transmissions a second.
Unlike the Arduino, the processing draw function does not run withh full speed,
but at fixed intervals which is adjustable.

I've made a GUI in Processing that interfaces with an Arduino (Pro Micro). Check my github for the source if that's of any help. :wink: