# Setting different time variables

**URL:** <https://discourse.processing.org/t/setting-different-time-variables/16861>\
**Category:** Coding Questions\
**Created:** [January 4, 2020, 3:40pm UTC](https://discourse.processing.org/t/setting-different-time-variables/16861 "2020-01-04T15:40:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Salttt](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Salttt](https://discourse.processing.org/u/Salttt)\
**Post date:** [January 4, 2020, 3:40pm UTC](https://discourse.processing.org/t/setting-different-time-variables/16861/1 "2020-01-04T15:40:47Z")

</div>

I am trying to make two things happen in time but independently to each other. I want the time after each of them to reset, but not reset the time for the other thing. Here’s example code to show what I mean;

```auto
int time = millis();
int time1 = second();

void setup() {
  background(0);
}

void draw() {
  if (millis() > time + 200) {
    time = millis();
    println("test");
  }
  if (second() > time1 + 3) {
    time1 = second();
    println("oop");
  }
}

```

Let’s say I want to print test, but only every 200ms, and oop every 3 seconds. I don’t however want the time for oop to reset every time test does, because it will never print. How do I make the time measurements different from eachother? 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:** [January 4, 2020, 3:44pm UTC](https://discourse.processing.org/t/setting-different-time-variables/16861/2 "2020-01-04T15:44:41Z")

</div>

i play with this

```auto
int step = 0;
long ts, tr;
long[] animator={12000, 7000, 20000}; // ________ time schedule in msec

void timer() {
  tr = animator[step] - ( millis() - ts ) ; //_ for show remaining time only
  if ( millis() - ts > animator[step] ) {
    // do whatever you have to do
    println(timeString() + " end step "+step);
    step++;
    ts = millis();
    if ( step >= animator.length ) step = 0; //_ auto repeat
  }
}

void setup() {
  println( timeString() );
}

void draw() {
  background(200, 200, 0);
  fill(0);
  text( "step "+step+" tr "+(tr/1000), 10, height/2); //_ show remaining time
  timer();
}

String timeString() {
  return ""+year() + "-" + nf(month(), 2) + "-" + nf(day(), 2) + "_" +
    nf(hour(), 2) + ":" + nf(minute(), 2) + ":" + nf(second(), 2);
}

```

---

<div class="post-metadata">

**Author:** ![Salttt](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Salttt](https://discourse.processing.org/u/Salttt)\
**Post date:** [January 4, 2020, 9:02pm UTC](https://discourse.processing.org/t/setting-different-time-variables/16861/3 "2020-01-04T21:02:11Z")

</div>

Okay, I’m going to require some explaining here (I’m pretty new) 🙃

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 4, 2020, 9:30pm UTC](https://discourse.processing.org/t/setting-different-time-variables/16861/4 "2020-01-04T21:30:26Z")

</div>

Then ask please…

Chrisir

---

<div class="post-metadata">

**Author:** ![Salttt](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Salttt](https://discourse.processing.org/u/Salttt)\
**Post date:** [January 5, 2020, 12:16am UTC](https://discourse.processing.org/t/setting-different-time-variables/16861/5 "2020-01-05T00:16:56Z")

</div>

yeah, that’s what I just did above

---

<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:** [January 5, 2020, 2:07am UTC](https://discourse.processing.org/t/setting-different-time-variables/16861/6 "2020-01-05T02:07:16Z")

</div>

possibly the example of a SCHEDULER  
not fits to your idea,  
can try a MULTI TIMER

anyhow the answer to your topic question is  
[https://processing.org/reference/Array.html](https://processing.org/reference/Array.html)  
and make a timer function that uses that timer array setpoints  
( instead writing extra timer function for each )

```auto
// Multi timer ( JAVA )
// https://editor.p5js.org/kll/sketches/zenbBhwf7 ( p5.js )

float[] myTimer = { 1, 3, 0.5 }; // _____ timer setpoints in sec
float[] myTimer_start = {0, 0, 0};
boolean[] myTAction = {false, false, false};
boolean[] myTEnable = {true, true, true};
float tomillis = 1000;
float r = 20, dr=0.05;

void my_timer(int sel) {
  if ( myTEnable[sel] ) { 
    float dtime = myTimer[sel] * tomillis;
    if (millis() > myTimer_start[sel] + dtime) {
      println( nows()+"/ "+millis()+" timer_" + sel); // note millis are counted from program start
      myTimer_start[sel] += dtime; // auto restart
      myTAction[sel] = !myTAction[sel];
    }
  }
}

String nows() {
  return ""+year()+"_"+nf(month(), 2)+"_"+nf(day(), 2)+"_"+nf(hour(), 2)+"_"+nf(minute(), 2)+"_"+nf(second(), 2)+"_";
}

void setup() {
  size(400, 400);
  rectMode(CENTER);
  println(" multi timer template: start\n"+nows() );
}

void draw() {
  background(200, 200, 0);
  for (int i = 0; i < 3; i++) {
    my_timer(i);
    if (myTAction[i]) fill(200, 0, 200, 127);
    else fill(0, 200, 200, 127);
    int pos = 150 + i * 50;
    rect(pos,pos, r, r);
  }
  r += dr;
  if ( r > 300 ) r=20;
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [January 6, 2020, 7:38pm UTC](https://discourse.processing.org/t/setting-different-time-variables/16861/7 "2020-01-06T19:38:57Z")

</div>

> [@Salttt](#):
>
> two things happen in time but independently to each other. I want the time after each of them to reset, but not reset the time for the other thing

Note also that there are timing libraries specifically for this purpose. Many people end up writing their own timers, but if you want a large number of recurring timed events it might be worth looking at existing models – such as TimedEvents or CountdownTimer, or Timing Utilities.

> **[Libraries](https://processing.org/reference/libraries/)**
>
> Extend Processing beyond graphics and images into audio, video, and communication with other devices.

---

<div class="post-metadata">

**Author:** ![Salttt](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Salttt](https://discourse.processing.org/u/Salttt)\
**Post date:** [January 6, 2020, 8:28pm UTC](https://discourse.processing.org/t/setting-different-time-variables/16861/8 "2020-01-06T20:28:27Z")

</div>

Could you explain what exactly ts does?

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [January 6, 2020, 9:56pm UTC](https://discourse.processing.org/t/setting-different-time-variables/16861/9 "2020-01-06T21:56:27Z")

</div>

I think that

ts is the Time a Step will happen – the sketch clock-time it will step.  
tr is the Time Remaining – the stopwatch-time until the next step happens.

---

<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:** [January 7, 2020, 2:04am UTC](https://discourse.processing.org/t/setting-different-time-variables/16861/10 "2020-01-07T02:04:54Z")

</div>

it consists of 3 parts,

-a- a timer function

-b- a number of arrays to hold the variables need for a timer

-c- a show as a example how to use the multi timer…

* * *

for understanding first you should run it.

you could try to delete all what you not need ( like the show )

and also change the number of timers to your needs ( array length )

so a very basic version ( and hopefully better readable version ) of it would be:

```auto
float[] myTimer_set = { 1000, 5000 }; // ____ timer setpoints in msec
float[] myTimer_start = {0, 0}; // ____________ last event time memory 

void my_timer(int sel) {
  if (millis() > myTimer_start[sel] + myTimer_set[sel]) {
    myTimer_start[sel] += myTimer_set[sel]; //_ auto restart
    my_event_code(sel);
  }
}

void my_event_code(int sel) { // _____________ here your code whatever you need to happen at a timer event
    println( millis()+" timer_" + sel); // ____ note millis are counted from program start
}

void setup() {}

void draw() {
  for (int i = 0; i < 2; i++) my_timer(i); //_ the timer must be called
}

```
