# How to loop faster than 60 times per second?

**URL:** <https://discourse.processing.org/t/how-to-loop-faster-than-60-times-per-second/12890>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [July 22, 2019, 3:18pm UTC](https://discourse.processing.org/t/how-to-loop-faster-than-60-times-per-second/12890 "2019-07-22T15:18:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@Luigi](https://discourse.processing.org/u/Luigi)\
**Post date:** [July 22, 2019, 3:18pm UTC](https://discourse.processing.org/t/how-to-loop-faster-than-60-times-per-second/12890/1 "2019-07-22T15:18:25Z")

</div>

Hi,  
I am creating a graphic user interface for a machine controled by an arduino  
The arduino sends serial data to processing at 120 SPS and my monitor displays 60 images per second. the only way to draw those values on a graph in real time is to draw two points at the same time.  
My problem is that the draw loop does not repeat more than 60 times per second to receive the serial data in real time.  
I know the best solution would be a thread but it’s too complex for a noob like me.

What is the easiest solution to make processing work faster ?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 22, 2019, 3:20pm UTC](https://discourse.processing.org/t/how-to-loop-faster-than-60-times-per-second/12890/2 "2019-07-22T15:20:54Z")

</div>

in setup put  
`frameRate(120);`

---

<div class="post-metadata">

**Author:** ![Luigi](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@Luigi](https://discourse.processing.org/u/Luigi)\
**Post date:** [July 22, 2019, 3:37pm UTC](https://discourse.processing.org/t/how-to-loop-faster-than-60-times-per-second/12890/3 "2019-07-22T15:37:38Z")

</div>

Hi quark,  
I saw on a forum that even if you write frameRate(120); ,the draw() function will still not be faster than the framerate of the monitor.  
I also saw that the serialEvent() works as a thread on processing but on another forum, someone said the opposite.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 22, 2019, 4:05pm UTC](https://discourse.processing.org/t/how-to-loop-faster-than-60-times-per-second/12890/4 "2019-07-22T16:05:16Z")

</div>

In Processing the `frameRate(float)` statement determines how often the `draw()` method is executed. At 120fps the rendered frame is made available to the graphics card every ~8ms

With modern displays the term is ‘_refresh rate_’ i.e. how often the display is updated, a 60Hz monitor will refresh the display 60 time a seconds, a 120Hz will refresh at 120 times a second so have a look at a faster monitor.

To be honest I do not believe the viewer can differentiate between

1. 2 points being added every 1/60 of a second and
2. 1 point being added every 1/120 of a second.

The human eye cannot detect changes that quickly, if you go to a movie the standard projection is 24fps and no one notices.

More info on monitor [refresh rates](https://www.digitaltrends.com/computing/do-you-need-a-120hz-or-240-hz-monitor/)

---

<div class="post-metadata">

**Author:** ![Luigi](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@Luigi](https://discourse.processing.org/u/Luigi)\
**Post date:** [July 22, 2019, 4:48pm UTC](https://discourse.processing.org/t/how-to-loop-faster-than-60-times-per-second/12890/5 "2019-07-22T16:48:56Z")

</div>

Thank you for your answer but that was not really my question.  
My goal is to make my program work on any 60hz monitor so the only option is 2 points being added every 1/60 of a second, but that is not a problem.

My problem is that I have to work with serial data in real time so there is no lag and this data arrives 120 times per second.

If the serialEvent() works as a thread, that would be the best solution. But if I understood correctly, it is triggerd each time after the draw function.

With the frameRate(120) method, I wrote the code below but there is no difference with 60 SPS from the arduino.

```auto
boolean draw_state = true ;

public void setup(){          
   frameRate(120);
}

public void draw(){
   Serial operations

   if (draw_state == true) {
       Drawing operations
       draw_state = false ;         
   }
   else { 
       draw_state = true ;
   }
}

```

---

<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:** [July 23, 2019, 2:35am UTC](https://discourse.processing.org/t/how-to-loop-faster-than-60-times-per-second/12890/6 "2019-07-23T02:35:47Z")

</div>

> [@Luigi](#):
>
> If the serialEvent() works as a thread, that would be the best solution. But if I understood correctly, it is triggerd each time after the draw function.

[https://processing.org/reference/libraries/serial/serialEvent\_.html](https://processing.org/reference/libraries/serial/serialEvent_.html)

> Called when data is available.

---

<div class="post-metadata">

**Author:** ![Luigi](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@Luigi](https://discourse.processing.org/u/Luigi)\
**Post date:** [July 23, 2019, 8:02am UTC](https://discourse.processing.org/t/how-to-loop-faster-than-60-times-per-second/12890/7 "2019-07-23T08:02:05Z")

</div>

I know but on the arduino reference of serialEvent(), it is also writen “Called when data is available”, yet it does not work as a thread. On the arduino, the availabilty of data is checked after each execution of the loop() and then serialEvent() can be triggered.

So I don’t know it is the same method on processing or not.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 23, 2019, 5:34pm UTC](https://discourse.processing.org/t/how-to-loop-faster-than-60-times-per-second/12890/8 "2019-07-23T17:34:53Z")

</div>

OK I understand what you are trying to do now.

AFAIK the `serialEvent()` is executed every time data is received by Processing so maybe executed several times every frame. Also AFAIK this occurs on a separate thread so Processing might execute `serialEvent()` while it is in the middle of the `draw()` method. This is a problem because the two threads are trying to share the same data store.

The solution is to use a thread safe collection e.g. java.util.concurrent.LinkedBlockingQueue

Since I don’t have an Arduino device I am using a separate thread to simulate data being generated and stored in a queue. This video shows it in action.

[![](https://img.youtube.com/vi/iYMBoadgcTY/hqdefault.jpg "Processing: data shared between threads") ](https://www.youtube.com/watch?v=iYMBoadgcTY)

Sketch Code:

```auto
import java.util.concurrent.*;

int[] c = new int[100];

BlockingQueue<Integer> bq = new LinkedBlockingQueue<Integer>();

int dataSent = 0;
int dataReceived = 0;

boolean createData = true;

public void setup() {
  size(600, 200);
  frameRate(60);
  strokeWeight(1);
  stroke(0, 0, 128);
  fill(0);
  textSize(16);
  thread("generateData");
}

void draw() {
  background(255, 255, 200);
  // Take any data off the queue and add it to the array
  Integer data;
  while (null != (data = bq.poll()) ) {
    c[data]++;
    dataReceived++;
  }
  // Display the array of data received
  for (int i = 0; i < c.length; i++) {
    line(0, i, c[i], i);
  }
  // Display stats - note we do not loose any data and the amount of data
  // processed every frame depends on how fast it is generated.
  text("Data sent " + dataSent, 20, 120, width - 40, 20);
  text("Data received " + dataReceived, 20, 140, width - 40, 20);
  float datarate = ((float) dataReceived ) / frameCount;
  text("Data processed per frame " + datarate, 20, 160, width - 40, 20);
}

/**
 This is to simulate the serialEvent method where the data 
 received will be placed on the queue.
 */
void generateData() {
  while (createData) {
    int d = (int)random(0, c.length);
    try {
      bq.put(d);
      dataSent++;
      delay(2); // 2 ms so we are creating 500 data elements per second
    }
    catch(InterruptedException e) {
      println("Interrupted");
    };
  }
}

```

---

<div class="post-metadata">

**Author:** ![Luigi](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@Luigi](https://discourse.processing.org/u/Luigi)\
**Post date:** [July 24, 2019, 8:25am UTC](https://discourse.processing.org/t/how-to-loop-faster-than-60-times-per-second/12890/9 "2019-07-24T08:25:58Z")

</div>

Thank you !  
That seems to work great.  
I will try to understand your code and integrate it.
