coding a nested timer

hi,

i am trying to make a nested timer in which the second timer starts after the first reaches its goal. then the first time starts again after the second reaches it goal. however, i can't seem to reset the second timer in the right condition statement. also, if i set a condition based on the timers reaching their limits, i only need a new limit to be generated once, but the time may not be exact, so setting an equality condition may yield no new limits because the time may not be exact. and if i set the condition in >, i get many new generations of a new limit.

thanks in advance, destro.

here is my code:

int testTime;
int nextTime;

int savedTime;
int savedNextTime;

int limit = 5000;
int newLimit;

void setup() {
  size(200, 200);

  savedTime = millis();
}

void draw() {
  testTime = millis() - savedTime;

  if(testTime < limit) println("testTime: " + testTime + ", " + "nextTime: " + nextTime);
//  else if(testTime == limit) {
//    newLimit = (int)random(5000, 11000);
//    println("newLimit: " + newLimit);
//  }
  else if(testTime >= limit) {
    newLimit = (int)random(5000, 11000);
    println("newLimit: " + newLimit);
    nextTime = millis() - savedNextTime;

    if(nextTime < newLimit) println("nextTime: " + nextTime);
    else if(nextTime >= newLimit) {
      limit = newLimit;
      savedTime = millis();
      savedNextTime = millis();
    }
  }
}
Tagged:

Comments

  • Rather than nested you seem to want a linear arrangement where a timer triggers the start of a new timer when its limit is reached.

    To prevent multiple timer restarts one solution would be to have a boolean that indicates when the limit has been reached. This boolean would initially (int setup) be set to false.

    This sketch demonstrates what I mean

    boolean triggered = false;
    int limit = 2000;
    int start;
    
    void setup() {
      size(200, 200);
      start = millis();
    }
    
    void draw() {
      int elapsedTime = millis() - start;
      if (elapsedTime > limit && !triggered) {
        println("Timer limit reached after " + elapsedTime + "  ms");
        triggered = true;
        start = millis();
        limit = (int) random(5000, 11000);
        println("     Next limit reached after another " + limit + "  ms");
      } else {
        triggered = false;
      }
    }
    

    Sample output from this sketch

    Timer limit reached after 2015  ms
         Next limit reached after another 10690  ms
    Timer limit reached after 10701  ms
         Next limit reached after another 10285  ms
    Timer limit reached after 10300  ms
         Next limit reached after another 8969  ms
    Timer limit reached after 8984  ms
         Next limit reached after another 9994  ms
    Timer limit reached after 9999  ms
         Next limit reached after another 8810  ms
    Timer limit reached after 8817  ms
         Next limit reached after another 5537  ms
    Timer limit reached after 5549  ms
         Next limit reached after another 8556  ms
    
  • edited January 2015

    /** 
     * TimerTask (v1.3)
     * by GoToLoop (2013/Dec)
     * 
     * forum.processing.org/two/discussion/1725/millis-and-timer
     */
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    final Timer t = new Timer();
    boolean hasFinished = true;
    
    void draw() {
      if (hasFinished) {
        final float waitTime = random(10);
        createScheduleTimer(waitTime);
    
        println("\n\nTimer scheduled for "
          + nf(waitTime, 0, 2) + " secs.\n");
      }
    
      if ((frameCount & 0xF) == 0)   print('.');
    }
    
    void createScheduleTimer(final float sec) {
      hasFinished = false;
    
      t.schedule(new TimerTask() {
        public void run() {
          print(" " + nf(sec, 0, 2));
          hasFinished = true;
        }
      }
      , (long) (sec*1e3));
    }
    
  • edited January 2015

    hi gotoloop and quark,

    thanks for your suggestions. here is the sample code:

    boolean triggered = false;
    int limit = 2000;
    int start;
    int chooseC = 3;
    int [] randomChoice = {3, 3, 3, 3, 3, 3, 0, 1, 2};
    
    void setup() {
      size(200, 200);
      start = millis();
    }
    
    void draw() {
      int elapsedTime = millis() - start;
      if (elapsedTime > limit && !triggered) {
        println("Timer limit reached after " + elapsedTime + "  ms");
        triggered = true;
        start = millis();   
        chooseC = (int)(randomChoice[(int)random(randomChoice.length - 1)]);
        println(chooseC);
        limit = (int) random(5000, 11000);
        println("     Next limit reached after another " + limit + "  ms");
      } else {
        triggered = false;
      }
    
      switch(chooseC) {
        case(0):
        fill(255, 0, 0); //red
        break;
        case(1):
        fill(0, 255, 0); //green
        break;
        case(2):
        fill(0, 0, 255); //blue
        break;
        case(3):
        fill(255, 0, 255); //purple
        break;
      }
      rect(0, 0, width, height);
    }
    
Sign In or Register to comment.