Loading...
Logo
Processing Forum

making blinking smoother

in Programming Questions  •  8 months ago  
Hello, I am new to Processing and I am having a minor problem with my code.  My problem is that the blinking is not consistent and is rather jerky.  What can I do to fix it?  The blinking in the code is set to a frequency of 3 hertz.  Here is my code:

Copy code
  1. // Adjustable flashing LED simulator
    // Can adjust to specific frequencies by use of millis()
    // Flashing is turned on/off by mouseclick


  2. boolean flag = false;  // false = not running, true = running
    int num = 1;
    int num1 = 0;

    void setup ()
    {
      frameRate(1000);  // 1000 frames per second
      background (175,252,255); // white background
      size(600,600);
      rect (300, 300, 5,5);  

    }            

    void mouseReleased ()
    {
      flag = !flag; // on or off at the press of mouse button
    }
       
    void draw ()
    {
    float m = millis() % 333.33; // 333.33 milli seconds == 3 hertz
     
      if ( flag )  // if true = on
      {
      // m counts up to ~333.33 and the resets.  This repeats
     
      println(m);

      while ( m < 300 )  // while loop wont exit until condition is false
        {
          fill (0); // fill the little box with black
          rect (300, 300, 5, 5 );
          m = millis() % 333.33; // 333.33 milli seconds == 3 hertz
        }
     
      if (m > 301)
        {
         fill(255) ;
         rect ( 300, 300, 5, 5 );
        }    

      
       else  // false = turn off the flashing
       {
         fill(0);  // go back to black
         rect ( 300,300,5,5);
       }


Replies(2)

I am not sure of what you do exactly, but the while loop is "smelly", ie. it is probably the wrong way to do what you try to do.
Usually, you check, at the start of draw(), the value of millis() against the time of the last even, and against a given delay.