# Experiencing lots of lag

**URL:** <https://discourse.processing.org/t/experiencing-lots-of-lag/13387>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [August 15, 2019, 12:57am UTC](https://discourse.processing.org/t/experiencing-lots-of-lag/13387 "2019-08-15T00:57:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![skrrt.exe](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/skrrt.exe/32/6153_2.png) [@skrrt.exe](https://discourse.processing.org/u/skrrt.exe)\
**Post date:** [August 15, 2019, 12:57am UTC](https://discourse.processing.org/t/experiencing-lots-of-lag/13387/1 "2019-08-15T00:57:47Z")

</div>

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:**

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

void setup() {
  Serial.begin(9600);
  pinMode(btnPin, INPUT);
  pinMode(stickBtnPin, INPUT);
}

void loop() {
  stickX = analogRead(0) / 4;
  stickY = analogRead(1) / 4;
  stickBtnState = digitalRead(stickBtnPin);
  btnState = digitalRead(btnPin);
  Serial.print("X:");
  Serial.println(stickX);
  Serial.print("Y:");
  Serial.println(stickY);
  Serial.print("sBtn:");
  Serial.println(stickBtnState);
  Serial.print("btn:");
  Serial.println(btnState);
  delay(3);
}

```

Processing code:

```auto
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() {
  background(255);
  fill(colour);
  rect(x, y, 10, 10);
  serialEvent();
}

void serialEvent() {
  String inString = port.readStringUntil('\n');
  if (inString != null) {
    if (inString.indexOf('X') == 0) {
      inString = inString.substring(2);
      stickX = float(inString);
      println(stickX);
      if (stickX > 135) {
        x++;
      } else if (stickX < 125) {
        x--;
      }
    } else if (inString.indexOf('Y') == 0) {
      inString = inString.substring(2);
      stickY = float(inString);
      if (stickY > 135) {
        y++;
      } else if (stickY < 125) {
        y--;
      }
    } 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!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [August 15, 2019, 1:13am UTC](https://discourse.processing.org/t/experiencing-lots-of-lag/13387/2 "2019-08-15T01:13:20Z")

</div>

please take a look here  
[https://processing.org/reference/libraries/serial/serialEvent\_.html](https://processing.org/reference/libraries/serial/serialEvent_.html)  
do you notice that there the

```auto
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.

* * *

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

```auto
 if (inString != null) {
// use a 
  println(inString);

```

( 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

---

<div class="post-metadata">

**Author:** ![skrrt.exe](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/skrrt.exe/32/6153_2.png) [@skrrt.exe](https://discourse.processing.org/u/skrrt.exe)\
**Post date:** [August 15, 2019, 1:25am UTC](https://discourse.processing.org/t/experiencing-lots-of-lag/13387/3 "2019-08-15T01:25:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [August 15, 2019, 1:34am UTC](https://discourse.processing.org/t/experiencing-lots-of-lag/13387/4 "2019-08-15T01:34:41Z")

</div>

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?  
OR  
you mean that a button ( arduino pin state change ) not come though?

---

<div class="post-metadata">

**Author:** ![skrrt.exe](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/skrrt.exe/32/6153_2.png) [@skrrt.exe](https://discourse.processing.org/u/skrrt.exe)\
**Post date:** [August 15, 2019, 1:35am UTC](https://discourse.processing.org/t/experiencing-lots-of-lag/13387/5 "2019-08-15T01:35:23Z")

</div>

There is no more lag, everything works now. Thanks

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 15, 2019, 1:40am UTC](https://discourse.processing.org/t/experiencing-lots-of-lag/13387/6 "2019-08-15T01:40:58Z")

</div>

> [@Using two different readStringUntil "characters"](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/23):
>
> Now that it seems all’s been sorted out, how about some optimization? The range [-10 to 10] is so small (5-bit) that it comfortably fits in a byte (8-bit). So, rather than send those 2 small values as TSV, w/ lotsa extra separator characters, we can simply send 2 bytes only using Serial.write(): [https://www.Arduino.cc/reference/en/language/functions/communication/serial/write/](https://www.Arduino.cc/reference/en/language/functions/communication/serial/write/) We just need to create a char array of length = 2: signed char xy[2]; #define BAUDS 115200 #define DELAY 10 #define…

---

<div class="post-metadata">

**Author:** ![skrrt.exe](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/skrrt.exe/32/6153_2.png) [@skrrt.exe](https://discourse.processing.org/u/skrrt.exe)\
**Post date:** [August 15, 2019, 1:48am UTC](https://discourse.processing.org/t/experiencing-lots-of-lag/13387/7 "2019-08-15T01:48:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 15, 2019, 1:55am UTC](https://discourse.processing.org/t/experiencing-lots-of-lag/13387/8 "2019-08-15T01:55:07Z")

</div>

> [@Using two different readStringUntil "characters"](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/17):
>
> Well, I don’t have any Arduino hardware and neither even seen 1 in front of me yet! woozy_face So I can only do blind coding in the dark for you. dark_sunglasses Theoretically, an Arduino C code like this 1: thinking void setup() { Serial.begin(115200); } void loop() { const int x = map(analogRead(A1), 0, 1023, -10, 10); const int y = map(analogRead(A0), 0, 1023, -10, 10); Serial.print(x); // "-5" Serial.write('\t'); // '\t' Serial.println(y); // "10" + "\r\n" delay…

---

<div class="post-metadata">

**Author:** ![skrrt.exe](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/skrrt.exe/32/6153_2.png) [@skrrt.exe](https://discourse.processing.org/u/skrrt.exe)\
**Post date:** [August 15, 2019, 2:06am UTC](https://discourse.processing.org/t/experiencing-lots-of-lag/13387/9 "2019-08-15T02:06:23Z")

</div>

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