# Incrementing frameRate() based on time

**URL:** <https://discourse.processing.org/t/incrementing-framerate-based-on-time/2897>\
**Category:** Processing\
**Created:** [August 23, 2018, 8:04pm UTC](https://discourse.processing.org/t/incrementing-framerate-based-on-time/2897 "2018-08-23T20:04:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![digitaldetritus](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@digitaldetritus](https://discourse.processing.org/u/digitaldetritus)\
**Post date:** [August 23, 2018, 8:04pm UTC](https://discourse.processing.org/t/incrementing-framerate-based-on-time/2897/1 "2018-08-23T20:04:28Z")

</div>

I am trying to increment the framerate of my animation from 16fps to 1fps over a 4 second period of time with 4 second (4000 millis) delay in between. Is frameRate() the best way to do this? The current playback seems glitchy and inconsistent. It seems to get out sync after a while. Any ideas would be much appreciated.

```auto
PFont myFont; 
String[] adjective = {"first", "second", "third"};
String[] gerund = {"first", "second", "third"};
String[] noun = {"first", "second", "third"};
int probability;
String sentence; // you don't need brackets b/c not an array

String temp1 = " ";
String temp2 = " ";
String temp3 = " ";

boolean start = true;

int framerate = 16;

void setup() {
  size(displayWidth, displayHeight);
  myFont = loadFont("Courier-Bold-66.vlw");
  textAlign(CENTER);
  frameRate(framerate);
}

void draw() {
  background(255);
  fill(0);
  textFont(myFont);

  int timer = second();

  if (start == true) {
    temp1 = adjective[int(random(0, adjective.length))];
    temp2 = gerund[int(random(0, gerund.length))];
    temp3 = noun[int(random(0, noun.length))];
    start = false;
  }  

  if (timer % 8 == 0) {
    delay(4000);
    probability = int(random(0, 3)); 
  } 
  

  if (timer % 8 > 4) {
    if (timer % 8 == 5) {
      frameRate(framerate/2);
    }
    if (timer % 8 == 6) {
      frameRate(framerate/4);
    }
    if (timer % 8 == 7) {
      frameRate(framerate/framerate);
    }
  } else {
    frameRate(framerate);
  }

  if (probability == 0) {
    temp1 = adjective[int(random(0, adjective.length))];
  }

  if (probability == 1) {
    temp2 = gerund[int(random(0, gerund.length))];
  }

  if (probability == 2) {
    temp3 = noun[int(random(0, noun.length))];
  }

  text(temp1, width/2, height/2-66);
  text(temp2, width/2, height/2);
  text(temp3, width/2, height/2+66);

}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [August 23, 2018, 9:06pm UTC](https://discourse.processing.org/t/incrementing-framerate-based-on-time/2897/2 "2018-08-23T21:06:50Z")

</div>

You cna maybe play with the millis() function.

Something like this:

```auto
int lastElapsedTime, duration;

void setup() {
 duration = 0;
 lastElapsedTime = 0;
}

void draw() {
  if(millis() - lastElapsedTime > duration) {
    duration += 100;
    background(random(255), random(255), random(255));
    lastElapsedTime = millis();
  }
}

```

---

<div class="post-metadata">

**Author:** ![digitaldetritus](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@digitaldetritus](https://discourse.processing.org/u/digitaldetritus)\
**Post date:** [August 24, 2018, 12:12am UTC](https://discourse.processing.org/t/incrementing-framerate-based-on-time/2897/3 "2018-08-24T00:12:51Z")

</div>

thanks! i’ll give a try and let you know if it works.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [August 24, 2018, 4:28am UTC](https://discourse.processing.org/t/incrementing-framerate-based-on-time/2897/4 "2018-08-24T04:28:42Z")

</div>

Yes, do not use `delay()` in `draw()`. You should be able to use `frameRate()` to adapt your draw rate or take the slightly longer way and use `millis()`.

Kf

---

<div class="post-metadata">

**Author:** ![digitaldetritus](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@digitaldetritus](https://discourse.processing.org/u/digitaldetritus)\
**Post date:** [August 24, 2018, 11:09am UTC](https://discourse.processing.org/t/incrementing-framerate-based-on-time/2897/5 "2018-08-24T11:09:15Z")

</div>

Thanks! I’ll give a try and share the code when I get it working.
