Create Blinking Eyes

Hello fellow programmers,

I need some help with my code. I’m trying to create eyes in processing that can blink. I want to be able to control each individual eyelid (left eye upper and lower lid and right eye upper and lower lid) of the eyes I’m creating. In order to achieve this, I started with just one eyelid (right eye upper eyelid). Unfortunately I didn’t succeed so far, because if I press the key (“b” in this case) it keeps on blinking.

How I want it to work:
If I press for example the key “b” I want the upper right eyelid to only blink ones, if I then push “b” again I want it to blink again. I tried using a while loop to track when the eyelid is back at the top again, but this doesn’t work.

I don’t know which thinking mistake I’m making, but hopefully there is someone who can help me!

int centerPointEye = 250;
int eyeDiameter = 150;
int radiusEye = eyeDiameter /2;
int rightEyeLidTop = centerPointEye - radiusEye; // highest possible point of eye lid right eye

int blinkSpeed = 3;
int posRightEyeLid = 1; //position of right eye lid

void setup() {
  background(#ADE3A5);
  size(500, 500);
  noStroke();
}

void draw() {
  background(#ADE3A5);
  
  //eyes shapes
  fill(255);
  ellipse(150, centerPointEye, eyeDiameter, eyeDiameter);
  ellipse(350, centerPointEye, eyeDiameter, eyeDiameter);

  //pupils
  fill(50); 
  ellipse(150, 250, 30, 30);
  ellipse(350, 250, 30, 30);
  if ((key == 'b') || (key == 'B')) {
    blinkRightEyeLid();
  }
}

void blinkRightEyeLid() {
  
  //while(posWink !=0){
  
  posRightEyeLid = posRightEyeLid + blinkSpeed;
  
  fill(50); //will get the same color as background: #ADE3A5
  rect(0, rightEyeLidTop, 250, posRightEyeLid);
  
  
  
  if(posRightEyeLid > radiusEye){
    blinkSpeed = -blinkSpeed;
  }
  
  if(posRightEyeLid < 0){
    blinkSpeed = 1;
    ;
  }
  
  if(posRightEyeLid == 0){
  }
  //}
}

Thanks in advance!!

You press the b key, so key is set to 'b'.

Your eye blinks.

What is the value of key when draw() runs again?

It’s still 'b'! So the eye will blink again and again…

You should use the keyPressed() function to remember in a global variable that a blink needs to happen.

Then, in draw(), if that variable is set to remember that a blink needs to happen, you need to update the size/position of the eyelid so that it is blinking. This means you will need a couple more variables - one to track how much the eye is closed, and one to track how much the amount of closure should change.

Then detect when the blink is done, and clear the value of “needs to blink” from your first variable.

Here is some example code:

boolean is_dipping = false;
float dipped_amt = 0;
float delta_dip = 4;

void setup() {
  size(400, 400);
}

void draw() {
  if ( is_dipping ) {
    dipped_amt+=delta_dip;
    if ( dipped_amt >= 200 ) {
      delta_dip = -4;
    }
    if ( dipped_amt <= 0 ) {
      dipped_amt = 0;
      delta_dip = 4;
      is_dipping = false;
    }
  }

  background(0);
  ellipse(200, 100+dipped_amt, 80, 80);
}


void keyPressed() {
  do_dip();
}

void mousePressed() {
  do_dip();
}

void do_dip() {
  if ( ! is_dipping ) {
    is_dipping = true;
  }
}
2 Likes

What a great idea! There are two ways to solve this. You can say if(keyPressed){ /the rest of the key-related code/}

Or you can put the code that detects the key in the built in keyPressed() function.

Because right now, you are just checking if the key is right. And it will be until you’ve pressed another button. You also have to check if a key is actually pushed.

1 Like

This was helpful! I now will be able to fix it, thanks a lot!!