Setting different time variables

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;

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

1 Like

i play with this

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);
}

2 Likes

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

Then ask please…

Chrisir

yeah, that’s what I just did above

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
and make a timer function that uses that timer array setpoints
( instead writing extra timer function for each )

// 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;
}

2 Likes

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.

1 Like

Could you explain what exactly ts does?

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.

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:

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
}

1 Like