We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Blinking box (Read 1812 times)
Blinking box
Dec 20th, 2009, 7:43am
 
OK, I'm brand new to this...

How do I create a box where the fill color blinks on and off? I'm trying to create a strobe effect. But, when I try to change the fill color after a delay in a loop, it only changes once after the loop has completed.

Mark
Re: Blinking box
Reply #1 - Dec 20th, 2009, 8:32am
 
hi
something like this?
Code:


import processing.opengl.*;

void setup() {
size(400,400,OPENGL);
stroke(255);
smooth();
}

boolean on=true;
void draw() {
background(0);
if(frameCount% 5== 0) {
if(on)
fill(0);
else
fill(255);
on = !on;
}
rect(100,100,200,200);
}



Re: Blinking box
Reply #2 - Dec 20th, 2009, 8:33am
 
oh of course it works also without opengl
Re: Blinking box
Reply #3 - Dec 20th, 2009, 9:39am
 
Thanks. Yes, it does work.

Is there a way to set the timing. For example to 12Hz (12 times per second)?

Mark
Re: Blinking box
Reply #4 - Dec 20th, 2009, 11:19am
 
This is a hack, for sure, but seems to do the trick. There is a 16 ms delay in the running the loop so, I adjust the ms value for the delay  - 16 to achieve the hz that I need.

Code:
import processing.opengl.*;
int i = 0;
float m = millis();
int s = second();
int[] delay = { 0, 1000, 500, 333, 250, 200, 167, 143, 125, 111, 100, 91, 83, 77, 71, 67, 63, 59, 56, 53, 50 };

void setup() {
 size(800,800,OPENGL);
 stroke(255);
 smooth();
}

boolean on=true;
void draw() {
 background(0);
//  if(frameCount% 0== 0) {
   if(on)
fill(0);
   else
fill(255);
   on = !on;
   delay(delay[12] - 16);
   i++;
   print(i + " : " + millis() + " : ");
   println(second());
//  }
 rect(1,1,800,800);
}
Re: Blinking box
Reply #5 - Dec 21st, 2009, 5:19pm
 
yeah or maybe setting the frameRate(int i);
but it varies so you should set it from time to time. like every loop.  Wink
Re: Blinking box
Reply #6 - Dec 22nd, 2009, 1:12am
 
That sounds like bad advice to me.  If you set the frameRate in setup Processing will do its best to comply with the requested rate and I'm fairly sure setting it every frame isn't going to make any difference whatsoever.  Fluctuations in framerate are usually caused when processor intensive activity is going on.  I'm not sure exactly how Processing handles this, but it's not likely that it simply 'changes' the frameRate; thereby necessitating your suggestion.  What's more likely is that it will continue to process every frame and things will slow down below the given frameRate (regardless of what it's set at).  At some point I suspect it will start dropping frames to try and keep up...  but once things calm down it will carry on at the originally set frameRate...  and that's why frameRate isn't reliable for accurate timing.

From what I've seen suggested elsewhere you're likely to need a separate thread and some Java to handle timing at such a small level.  Maybe do a search - this question has definitely come up before.  For instance this thread may be relevant.
Re: Blinking box
Reply #7 - Dec 22nd, 2009, 6:02am
 
ok. so you cant do more then set the FrameRate in your setup. and depending on what you do in your draw it setes a limit. like if you just get to the frameRate 61 tho you set it to 90 in your setup, you wont get mote then 61 in anyway, or not? 61 which is like a frame every 16 ms.
Re: Blinking box
Reply #8 - Dec 22nd, 2009, 7:58am
 
When in doubt have a look at the documentation:

"[frameRate] Specifies the number of frames to be displayed every second. If the processor is not fast enough to maintain the specified rate, it will not be achieved."

So if you set the frameRate to 90 it will certainly try and hit 90fps as long as you're not asking it to process too much.  The higher you set the frameRate the less of a time buffer it has to process stuff and the more likely things get slowed down.  However I suspect at some point it will start jumping frames in order to try and keep up:  e.g. I've seen it suggested that you set frameRate(999) to get the fastest possible framerate.

One thing is certain though and that's that you can't use frameRate for reliably accurate timing... especially when making heavy demands on the processor.  To test it I guess you could log millis() to get some sense of how reliable it is and whether this meets your needs.
Page Index Toggle Pages: 1