Makeing a Timer Function Without Global Variables

edited February 2018 in Questions about Code

Hello Everyone,

I am working on a project where I need a function that counts up to a certain number and stops. I understand how to do that using global variables but I am having difficulty creating a function where the data can just be localized. Here's what I have:

int testnumber = 0;
int starttime = 0;
int interval = 1000;
int countingto = 15;

void setup() {
  size(1600, 900);
  background(125);
}

void draw() {
  if (Timer(starttime, interval)) {
    if (testnumber < countingto) {
      testnumber++;
      starttime = millis();
      println(testnumber);
    }
  }
}
boolean Timer(float starttime, int interval) {
  return millis() - starttime > interval;
}

The above code works, but requires that I change the global variables somehow to get a different result. What I ideally want is a function that looks like

Counter(float starttime, float interval, int testnumber, int countingto)

that would run the above function. However each time I build it the function breaks down somehow, either it stops iterating or stops counting the numbers. What should my desired Counter() look like?

Thanks in advance for any help.

Answers

  • Thank you GoToLoop for the link. I'm still a little confused by the example on the GitHub page though.

    I'm not trying to create a stopwatch type of timer. I understand how that works. What I'm trying to create is something where every "x" amount of milliseconds a counter goes up from "n" to "n+1" and will stop at some predetermined number, say "n+100".

    I am using this as part of a larger function that will pull data from somewhere else. For example, when the function runs, rather than arrayofimages[n] being displayed on the screen you will see arrayofimages[n+1]. Then after so many milliseconds arrayofimages[n+2] will appear.

    My apologies if my question was not clear. I'm still getting my sea legs with regards to programming in general.

  • edited February 2018 Answer ✓

    Although my Countdown library can fit in the task of awaiting some amount of time so your sketch knows when it's time to increase the index variable for your PImage[] array, it's kinda awkward to keep invoking its start() method over & over for each single increase.

    That's b/c it's an action which needs to repeat itself at some fixed time.

    There are many approaches for it btW.
    For Processing, most famous is via millis(), as you're attempting it above.

    But I'm gonna describe another 1 which relies on frameCount instead:
    https://Processing.org/reference/frameCount.html

    By default, a Processing sketch callbacks draw() at about 60 FPS:
    https://Processing.org/reference/frameRate_.html

    It means that each 60 draw() frames, about 1 second (or 1000 milliseconds) had transpired.

    Let's say you wanna increase your index variable each 2 seconds interval.
    Translating that to frameCount, we've got a 120 frames interval (2 * 60 FPS).

    static final int IMAGES = 10, FPS = 60, INTERVAL = 2 * FPS;
    final PImage[] pics = new PImage[IMAGES];
    int idx;
    

    Easiest shortcut to know that each 120 frames had passed is using the modulo/remaining % operator:
    https://Processing.org/reference/modulo.html

    That is, we just need to check whether current frameCount is exactly dividable by 120: *-:)
    if (frameCount % INTERVAL == 0) idx = (idx + 1) % IMAGES;

    As you can above, the same % operator can also be used to ensure current index never reaches array's length! It loops back to 0 once it reaches its length! \m/

Sign In or Register to comment.