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
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.
-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
}