IFD:PhysicalComp2011/code: Difference between revisions

From Medien Wiki
(XI Interval Timer)
Line 1,134: Line 1,134:
}
}


</source>
<br /> <br />
=== XI Interval Timer===
<source lang="java">
/* INTERVAL TIMER (PROCESSING)
*
* Often when animating things, we need
* to trigger events in a certain interval.
* For this the millis() funtion is very helpful.
* It returns the time in milliseconds
* since the program was started.
*
* Frederic Gmeiner, 2011
*/
// create a variable to store the last time
// our event was triggered. (the long datatype
// can store much larger numbers as an integer)
long lastMillis= 0;
// define the length of the interval
int interval = 1000;
void draw(){
  // check whether the interval time has passed:
  if(millis() - lastMillis > interval){
    // once the interval time has passed,
    // update the lastMillis so that we need to
    // wait again for 1000 milliseconds.
    lastMillis = millis();
    // trigger the event:
    println(millis()/1000);
  }
}
</source>
</source>
<br /> <br />
<br /> <br />