How to make a shape flash colour every X amount a second?

edited December 2013 in How To...

Hi there,

In our Graphic Design class we have to make a design for a clock in code. My idea requires me to make a shape (in my case an ellipse) change colour (red) for a very short amount of time, so it will appear flashing.

Working with the if command will not work in my case, for I need to make the ellipse flash red every ±0,215 seconds (215 ms), which means I'd have to write 300 individual commands for 1 minute for 1 light. I feel that this can be done much easier, but I can't find the proper command in the References Page.

Any help would be greatly appreciated!

Tagged:

Answers

  • Hi Armand,

    there is a easier way to do this. with millis() you can get the number of milliseconds that passed since the programm started. in the draw loop you can then calculate the number of milliseconds that passed since the last time draw was called. with this information you could set the fill color to red for just the next frame (see first ellipse) or fill it with red according to the time span that has already passed (see second ellipse).

    here a simple example:

    int time = millis();
    
    void setup() {
      size(200, 200);
      background(102);
      noStroke();
    }
    
    void draw() {
      background(100);
    
      fill(255); // sets fill color to white
    
      int passedMillis = millis() - time; // calculates passed milliseconds
      if(passedMillis >= 215){
          time = millis();
          fill(255,0,0);  // if more than 215 milliseconds passed set fill color to red
      }
      ellipse(50,50,50,50); // draw first circle
    
    
      fill(255); // fill white
      ellipse(150,150,50,50); // draw second circle
    
      fill(255,0,0);  // fill red
      arc(150, 150, 50, 50, 0, TWO_PI / 215.0 * passedMillis,PIE); // draw red pie over second circle
    }
    
Sign In or Register to comment.