Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

milliseconds - How to add +1 to variable every 10 seconds in Processing?

Excuse my ignorance, but I ran into a problem that turned out to be challenging for my current knowledge in programming with Processing, even though the idea is quite simple. You see, I need to add 1 unit to a variable every 10 seconds. This is the code:

int i = 0;

void setup()
{
  frameRate(60);
}

void draw()
{

  int time = (millis() % 10000) / 1000;

  if (time == 9)
  {
    i++;
  } else {}

  System.out.println("-------------------------------
" +
                     "Timer: " + time + "
"
                   + "Adding 1 every 10 seconds: : " + i + "
"
                   + "-------------------------------");
}

The problem is that because draw() loops 60 times per second, as soon as time reaches 9 the second it last makes the if statement to be executed 60 times and it ends adding 60 to i every 10 seconds and I just need to be adding 1.

I tried to apply some kind of algorithm that subtracts the unnecessary numbers as they increase like so:

int i = 1;
int toSubstract = 0; //Variable for algorithm

void setup()
{
  frameRate(60);
}

void draw()
{

  int time = (millis() % 10000) / 1000;

  if (time == 9)
  {
    i++;
    algToSubstract();
  } else {}



  System.out.println("-------------------------------
" +
                     "Timer: " + time + "
"
                   + "Adding 1 every 10 seconds: : " + i + "
"
                   + "-------------------------------");
}

void algToSubstract() //<--- This is the algorithm
{
  i = i - toSubstract;
  toSubstract++;

  if (toSubstract > 59)
  {
    toSubstract = 0;
  } else {}
}

...but I couldn't make it work. The idea was something like this:

time reaches 9, if statement executes, i = 1 and toSubstract = 0.

i increases 1 so i = 2.

i = i - toSusbract (i = 2 - 0 so i = 2).

toSusbract increases 1 so toSusbract = 1.

i increases 1 so i = 3.

i = i - toSusbract (i = 3 - 1 so i = 2).

toSusbract increases 1 so toSusbract = 2.

... Process continues...

toSubstract gets bigger than 59 so it is restarted to 0.

time stops being 9.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The other answers are fine general approaches, but they don't take advantage of the features that Processing provides for you.

For example, you could use the frameCount variable to check how many frames have elapsed. Since draw() is called 60 times per second, 10 seconds is 600 frames. Something like this:

void draw(){
  if(frameCount % 600 == 0){
    // 10 seconds has elapsed
  }
}

Another way to do this is to store the last time 10 seconds elapsed, and then check that against the current time to see if 10 seconds has elapsed since then. Something like this:

int previousTime = 0;

void draw(){
   if(millis() > previousTime + 10*1000){
      // 10 seconds has elapsed
      previousTime = millis();
   }
}

More info can be found in the reference.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...