Controlling an array of Blinking Eyes Independently of Each Other

Hi, I am very new to processing and I am making a program based on a Coding Train Video. I have created an Eye Class that display’s an eye and then makes it blink. At the moment I can populate the scene with an array of eyes and have them all blink in unison . My problem is I want them to all blink at separate times and at separate speeds. I have played around with using the random() and delay() functions, but wherever I put them they cause all of the eyes to delay blinking, not just one

Main function (Eyeballs):

Eye[] eyes = new Eye[10];

void setup(){
  size(1000,1000);
  background(255);
  //smooth();
  //eye1 = new Eye();
  for(int i=0;i<eyes.length;i++){
    eyes[i] = new Eye();
  }
}

void draw(){
  background(0);
  translate(width/2,height/2);
  
  for(int i=0;i<eyes.length;i++){
    //delay((int)random(10,100);
    eyes[i].create();
    eyes[i].blink();
  }
  
}

Eye Class (Eye)

class Eye{
 float x,y;
 float blinkCnt,counter;
 float RightBtm,LeftBtm;
 float RightTop, LeftTop;
 float delay;
 float eyeWidth,eyeHeight,pupilWidth;
 
 Eye(){
   eyeWidth = 125; //100, 40
   eyeHeight = 50;
   pupilWidth = 0.875*eyeHeight;
   x = random(-width/2 + eyeWidth, width/2 - eyeWidth);
   y = random(-height/2 + eyeHeight,height/2 - eyeHeight);
   delay = 30; //random(10,60);
   blinkCnt = 0;
 }
  
 void create(){
   strokeWeight(5);
   fill(255,255,255);
   ellipse(x,y,eyeWidth,eyeHeight); //Outer Eye
  
   fill(0);
   ellipse(x,y,pupilWidth,pupilWidth); //Pupil
   
   //Top Eyelid Mapping
   RightTop = map(blinkCnt,0, delay, PI+HALF_PI, PI+PI);
   LeftTop = map(blinkCnt,0,delay, PI+HALF_PI, PI);
   
   //Bottom Eyelid Mapping
   RightBtm = map(blinkCnt, 0, delay, HALF_PI, 0);
   LeftBtm = map(blinkCnt, 0, delay, HALF_PI, PI);
   
   //Displaying Eyelids
   strokeWeight(0);
   fill(255,155,155);
   arc(x,y,eyeWidth,eyeHeight,LeftTop,RightTop,CHORD);
   arc(x,y,eyeWidth,eyeHeight,RightBtm,LeftBtm,CHORD);  
   
   
 }
 
 void blink(){
   //delay((int)random(delay));
   if(blinkCnt==0){
     counter = 1;
   }
   if(blinkCnt==delay){
     counter = -1;
   }
   blinkCnt += counter;
 }

}

your concept is ok.
the problem is that you never set “counter” to a value.
only if an eye starts with blinkCount set to 0, counter will become 1.

   if(blinkCnt==0){
     counter = 1;
   }

in all other cases, whatever you set as a starting value for blinkCount other then 0, it can never grow because counter always stays 0.
that’s why your attempts to use random values for each eye doesn’t work, even though your idea is the right one.

Uncomment the delay = random(10,60); in the Eye class constructor.
Then try this as your blink() method:
if (blinkCnt==0) {
counter += 1;
}
if (blinkCnt>delay) {
counter -= 1;
}
blinkCnt += counter;

Hi @jcser,

Welcome to the forum! :slight_smile:

I just played around and the simplest solution that I have found was just adding the following lines in your code

float istoBlink = random(0,1);
if(istoBlink >= 0.5 ) eyes[i].blink();

So the draw method becomes:

void draw(){
  background(0);
  translate(width/2,height/2);
  
  for(int i=0;i<eyes.length;i++){
    //delay((int)random(10,100);
    eyes[i].create();
    float isToBlink = random(0,1);
    if(isToBlink >= 0.5 ) eyes[i].blink();
  }

What it does is simply generate a random probability for each eye to blink everytime is draw. So by playing with the probability the blink will occur with more or less frequency. You can even try different weights for the probability of each eye. So that some will blink more often than others

Hope it helps :slight_smile: