Processing can't write to Arduino

Hi, I'm a beginner in Arduino and I've been working my through some of the build-in guides on the Arduino website. I followed the guide here to create a sketch with Processing that turns an LED on and off, and I wasn't able to get the Arduino to recognize Processing writing to the port no matter what I did.
Initially I copied the code from the guide website, but after I couldn't get it to work I shortened it down to this:

import processing.serial.*;

void setup() {
  port = new Serial(this, "<location of port>", 9600);
}

void draw() {
  Serial.write("hello");
}

And the Arduino was still unable to receive the data. For reference, here's my code in Arduino:

const int ledPin = 2;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);
   }

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
    digitalWrite(ledPin, HIGH);
  }
  delay(1);
}

Manually sending data to the Serial port through the Arduino Serial Monitor or even through something like Command Prompt is received just fine, but for some reason it won't pick up on anything sent from Processing. Processing is able to read data sent by another program to the port, but it won't recognize something that it wrote on its own.

I've tried uninstalling and reinstalling both the Arduino IDE and Processing, checking for any driver updates for the Arduino, and restarting my PC, none of which have worked to fix it.

Does anyone know anything I may be missing that is resulting in the Arduino not reading the data?

Thanks in advance.

Just to make sure, you have replaced "" by the correct port (something like "COM3" or "/dev/ttyACM0").

How do you know it does not work? Are you saying that the LED on pin 2 does not light up when the Processing program is started?

Your Arduino code only switches the ledPin on (when serial data is received) and never switches it off. Further you don't read the serial port so the character will be stuck in there and Serial.available() will always return a non-zero value once the first character is received.

Which Arduino board are you using?

I have a tutorial in perpetration about communicating between Processing and the Arduino.

See it here:-

1 Like

Yes, I've replaced the code with the correct port number ("COM3") and it isn't received by Arduino.
Running the program using Serial list like this still doesn't work:

port = new Serial(this, Serial.list()[0], 9600);

Right now, I'm trying to ensure that the Arduino is receiving the data sent by Processing, so I'm only looking to have the LED on pin 2 turn on when the Processing program starts. Currently, it doesn't light up, and sending data either with the Arduino Serial Monitor or with Command Prompt does cause it to light up.

I'm using an Arduino MEGA 2560.

You know what the port number of the Mega is; there is no guarantee that Serial.list()[0] contains that port number. Replace Serial.list()[0] by COMx where x is the number that you e.g. see in the IDE or in your operating system.

My Mega is on COM5 so the statement would be

port = new Serial(this, "COM5", 9600);

Even after replacing Serial.list()[0] with COM3, the LED still doesn't turn on.

Below Processing code works as expected; I'm using a SparkFun RedBoard and the onboard LED instead of something on pin 2.

import processing.serial.*;
Serial port;

void setup() {
  port = new Serial(this, "COM3", 9600);
}

void draw() {
  port.write("hello");
}

The LED switches on when the first data is received and stays on. The Rx LED is constantly flashing indicating data received.

When I run it with my Arduino 2560, the Rx LED is constantly on, but even exactly copying your code does not turn on the LED, even with the onboard LED.
The problem persists regardless of what computer it runs on, and I can't seem to find an example of someone else having this same issue, so honestly I'm at a loss as to what to try next.

So you have not read post #4 then?

Tested and working on Processing 4 with Arduino Mega. Button from pin 2 to ground. Uses built in LED.

Arduino sketch:

String lastString;
int led = LED_BUILTIN;
const int button = 2;

int buttonPushCounter = 0;
int buttonState = 1;
int lastButtonState = 1;

void setup() {
  Serial.begin(115200);
  pinMode(button, INPUT_PULLUP);// reverses button logic, no external resistors required
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

void loop() {
  if (Serial.available() > 0) {
    char in = Serial.read();
    if ((in == 'h') || (in == 'H')) {
      digitalWrite(led, HIGH);
    } else if ((in == 'l') || (in == 'L')) {
      digitalWrite(led, LOW);
    }
  }
  buttonState = digitalRead(button);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is LOW then the button went from off to on
      buttonPushCounter++;
      if (buttonPushCounter % 2 == 0) {
        Serial.println("green");
      } else {
        Serial.println("red");
      }
      delay(500);
    }
  }
  lastButtonState = buttonState;
}

Processing sketch:

import processing.serial.*;
Serial myPort;
int rval, gval, bval;
int state;

String[] com = {"COM2", "COM3", "COM4",
  "COM5", "COM6", "COM7", "COM8", "COM9", "COM10", "COM11",
  "COM12", "COM13", "COM14", "COM15", "COM16", "COM17", "COM18",
  "COM19", "COM20", "COM21", "COM22", "COM23", "COM24", "COM25", "COM26"};

void settings() {
  fullScreen(P3D);
}

void setup() {
  background(0, 59, 0);
  noStroke();
  fill(204);
  connect(1, 24, 200);
}

void draw() {
  background(rval, gval, bval);
  switch(state) {
  case 0:
    rval = 128;
    gval = 128;
    bval = 128;
    break;
  case 1:
    rval = 0;
    gval = 255;
    bval = 0;
    break;
  case 2:
    rval = 255;
    gval = 0;
    bval = 0;
    break;
  case 3:
    rval = 255;
    gval = 165;
    bval = 0;
    myPort.write('H');
    break;
  case 4:
    rval = 0;
    gval = 60;
    bval = 0;
    myPort.write('L');
    break;
  case 5:
    rval = 0;
    gval = 255;
    bval = 0;
    myPort.write('h');
    break;
  case 6:
    rval = 0;
    gval = 0;
    bval = 255;
    myPort.write('l');
    break;
  default:
    rval = 128;
    gval = 128;
    bval = 128;
    break;
  }
  // use arrow keys to do stuff
  checkDirections();
}


void checkDirections() {
  if (keyPressed) {
    if (keyCode == UP) {
      state = 3;
    } else if (keyCode == DOWN) {
      state = 4;
    } else if (keyCode == LEFT) {
      state = 5;
    } else if (keyCode == RIGHT) {
      state = 6;
    }
    delay(160);
  }
}
// serial seek and connect handler
boolean connect(int portNumber, int numCom, int limit) {
  background (0, 14, 255);
  int tries = 0;
  while (tries < limit) {
    try {
      printArray (Serial.list()[portNumber]);
      myPort = new Serial(this, com[numCom], 115200);
    }
    catch(Exception e) {
      System.err.println("Retrying " + "Port " + com[numCom] + " on Port Number " + portNumber);
      numCom += 1;
      tries++;
      if (numCom < 0) {
        numCom = 24;
      } else if (numCom > 24) {
        numCom = 0;
      }
      if (tries % 25 == 0) {
        portNumber += 1;
        if (portNumber > 4) {
          portNumber = 0;
        }
      }
      delay(200);
      continue;
    }
    break;
  }
  if (tries < limit) {
    println("Connected in " + str(tries + 1) + " tries.");
    background(0, 255, 0);
    delay(200);
    background(0);
    return true;
  } else {
    System.err.println("Connection failed.");
    return false;
  }
}
void serialEvent (Serial myPort) {
  String inString = myPort.readStringUntil('\n');
  if (inString != null) {
    inString = trim(inString);
    if (inString.equals("green")) {
      state = 1;
    } else if (inString.equals("red")) {
      state = 2;
    }
  }
}

Good luck.