Experiencing lots of lag

I just started using Processing today and kind of just jumped right into it. I used it a lot on Khan Academy but never with an Arduino. I have a book that taught me a little bit about how to use processing and using it with an Arduino. All of my code works properly except for the enormous amount of lag. I have a joystick and a button that controls a rectangle in the canvas. When I move the joystick, it should instantly move the rectangle accordingly. I wait for a minimum of 40 seconds and the longer my code runs the more lag I experience. Is this something to do with my laptop or Processing? How do I fix it?
Arduino code:

int stickX, stickY, stickBtnState, btnState, stickBtnPin = 4, btnPin = 2, stickXPin = 0, stickYPin = 1;

void setup() {
  pinMode(btnPin, INPUT);
  pinMode(stickBtnPin, INPUT);

void loop() {
  stickX = analogRead(0) / 4;
  stickY = analogRead(1)  / 4;
  stickBtnState = digitalRead(stickBtnPin);
  btnState = digitalRead(btnPin);

Processing code:

import processing.serial.*;

Serial port;

int x = 10, y = 10;
float stickX, stickY, stickBtn, btn, colour = 0;
String readSerial;

void setup() {
  size(400, 400);
  port = new Serial(this, Serial.list()[0], 9600);

void draw() {
  rect(x, y, 10, 10);

void serialEvent() {
  String inString = port.readStringUntil('\n');
  if (inString != null) {
    if (inString.indexOf('X') == 0) {
      inString = inString.substring(2);
      stickX = float(inString);
      if (stickX > 135) {
      } else if (stickX < 125) {
    } else if (inString.indexOf('Y') == 0) {
      inString = inString.substring(2);
      stickY = float(inString);
      if (stickY > 135) {
      } else if (stickY < 125) {
    } else if (inString.indexOf('s') == 0) {
      inString = inString.substring(5);
      stickBtn = float(inString);
      if (stickBtn == 1) {
        colour = 100;
    } else if (inString.indexOf('b') == 0) {
      inString = inString.substring(4);
      btn = float(inString);
      if (stickBtn == 1) {
        colour = 255;

Any help is greatly appreciated by me. Thanks!

1 Like

please take a look here
do you notice that there the

serialEvent(Serial p)

is NOT called from draw()!

sending a line from arduino with a delay of 3 milli seconds means you try
to get >300 lines per second??
but use a serial transport of 9600bd
that does not fit.
start with a delay(1000) ( one line per second ) and change later down to optimize.

how you know what is received by processing ( if anything at all )

 if (inString != null) {
// use a 

( and possibly post the console print here )

after all that runs well,
you can start with the real work,
analyze the string you send to get back NUMBERS out of it


Thanks! I didn’t know that I had to add Serial p inside the parameters of serialEvent(). The book did not show this… The code was not working without me calling serialEvent inside of draw()
Also, I do print numbers with Serial.println()… What are you asking in the second part? The code works without me increasing the delay. The code prints X:(num) or Y:(num) where num is 0 - 255 and 0 or 1 for the buttons. After that, I use substring() to remove X: or Y: and others… So I do get numbers out of it. The part before the colon is just so that the code can figure out what each number is for.

good you get good lines and good numbers out of it,
and what was the question? LAG

means ? you get one line in 40 sec instead 300 line per sec?
you mean that a button ( arduino pin state change ) not come though?

There is no more lag, everything works now. Thanks

Serial.write() doesn’t end with a new line… I need for it to end with a new line to read the button states

I think I’m okay with my current code. Thank you though!