# Program a simple strobe effect

HI! i’m trying to program a simple strobe effect and handle the speed using knob midi here is my logic:

ofDrawRectangle(0, 0, ofGetWidth(), ofGetHeight());

``````ofSleepMillis(knobValue);

ofSetColor(255);

ofDrawRectangle(0, 0, ofGetWidth(), ofGetHeight());

ofSetColor(255);
``````

I only get the first first color what i’m doing wrong? in the draw function , draw function suppose to loop right?

Hi aliva,

I’m assuming that code is in your draw() loop correct?

First of all it’s worth learning how the draw() loop actually works. Draw is called once per frame and at the end of the draw loop anything you’ve drawn is displayed on the screen. This means that if I draw a red rectangle across the whole screen and a then a green rectangle across the whole screen after that I will only see the green rectangle as the red one is drawn over.

the ofSleepMillis() method will cause your whole application to sleep so basically what you’re doing with your code is:

1. Drawing a rectangle across the whole frame buffer
2. Pausing everything
3. Setting the color to white
4. Drawing a rectangle across the whole frame buffer
5. setting the color to white
6. Drawing everything to the screen

Instead I would recommend using a timer rather than an ofSleepMillis() method.

To do this you can use ofGetElapsedTimef() to get the current time and swap between the two colors each x millis based on the knob value.

So something like this should work:

in ofApp.h:

``````// Define a variable for timing
float lastTimeSwapped = 0;
// Define a variable for keeping track of your color
bool colorOneOn= true;
``````

if ofApp.cpp:

``````update() {
if(ofGetElapsedTimef() - lastTimeSwapped > knobValue) {
colorOneOn = !colorOneOn;
lastTimeSwapped = ofGetElapsedTimef();
}
}
draw() {
if(colorOneOn) {
ofSetColor(255);
} else {
ofSetColor(0);
}
ofDrawRectangle(0, 0, ofGetWidth(), ofGetHeight());
}``````
1 Like

Awesome! make’s sense.
Thanks

1 Like