Tracking time during a long-term installation

edited January 2014 in How To...

I normally use millis() to time animations, but now I'm working on a piece that will be installed in a gallery for three months. It will be powered on every morning and shut down at night, so it needs to run all day for about 12 hours without crashing.

In this context, could the very large value of millis() cause any problems? (For example, could this exceed the capacity of a standard int?) Are there other approaches to timing events that don't rely on measuring total running time, but simply relative time (e.g. trigger another event in 1 minute from now, as opposed to in 123 minutes from when the sketch started running)?

Would love to hear thoughts from anyone else using Processing for installation work.

Answers

  • edited January 2014 Answer ✓

    An int capacity is from -2^31 to 2^31 - 1. Of course millis() starts out at 0. So, let's ignore the negative range! :(|)
    But if you wanna know what happens after the highest positive int is extrapolated, it becomes -2^31! 8-X

    Anyways, I've made Processing give us the answer. Function millis() spits out positive values up to 24 days (3/4th months)! :-bd
    Since it's turned off at night, only 1/24th is used up. Function millis() is zeroed at each run anyways! :!!

    // forum.processing.org/two/discussion/2480/
    // tracking-time-during-a-long-term-installation
    
    final int mils = Integer.MAX_VALUE;
    println("Millis: \t" + mils);
    
    final int secs = mils/1000;
    println("Secs: \t" + secs);
    
    final int minutes = secs/60;
    println("Minutes: \t" + minutes);
    
    final int hours = minutes/60;
    println("Hours: \t" + hours);
    
    final int days = hours/24;
    println("Days: \t" + days);
    
    exit();
    
  • _vk_vk
    edited January 2014

    Are there other approaches to timing events that don't rely on measuring total running time, but simply relative time

    Sure any timer will do that, the general idea is:

    if(millis() - lastTime > interval){
    //do stuff
    //...
    //reset timer
    lastTime =millis() 
    }
    
  • edited January 2014

    @_vk Function millis() returns an int value. @alignedleft was just worried about how many milliseconds there are! >-)

  • About alternative approaches, there's some from this forum below:
    http://forum.processing.org/two/discussion/1725/millis-and-timer

    Here's my own take there:

    /** 
     * TimerTask (v1.22)
     * 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(float sec) {
      hasFinished = false;
    
      t.schedule(new TimerTask() {
        public void run() {
          hasFinished = true;
        }
      }
      , (long) (sec*1e3));
    }
    
  • edited January 2014

    @GoToLoop Perfect, thank you! So, this is only cause for worry if you were to have an installation running for 24+ days nonstop.

    If anyone has suggestions for how to handle that kind of long-term installation scenario, I'd love to hear about it. I suppose you'd just need to check for negative values (for when millis() wraps around to -2^31).

  • edited January 2014 Answer ✓

    If anyone has suggestions for how to handle that kind of long-term installation scenario, I'd love to hear about it.

    TimerTask doesn't depend on previous values and uses long rather than int for how many milliseconds to trigger! o->

    For a simpler approach, but w/ more preciseness, go w/ System.currentTimeMillis():
    docs.oracle.com/javase/7/docs/api/java/lang/System.html#currentTimeMillis%28%29

    Or even System.nanoTime(), which millis() idea is based upon. But the former uses long instead of int:
    docs.oracle.com/javase/7/docs/api/java/lang/System.html#nanoTime%28%29

  • Super, thanks!

  • sorry. : P

Sign In or Register to comment.