# Create Blinking Eyes

**URL:** <https://discourse.processing.org/t/create-blinking-eyes/4343>\
**Category:** Coding Questions\
**Created:** [October 10, 2018, 8:05pm UTC](https://discourse.processing.org/t/create-blinking-eyes/4343 "2018-10-10T20:05:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mick](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@Mick](https://discourse.processing.org/u/Mick)\
**Post date:** [October 10, 2018, 8:05pm UTC](https://discourse.processing.org/t/create-blinking-eyes/4343/1 "2018-10-10T20:05:13Z")

</div>

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!

```auto
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!!

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [October 10, 2018, 8:51pm UTC](https://discourse.processing.org/t/create-blinking-eyes/4343/2 "2018-10-10T20:51:40Z")

</div>

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:

```auto
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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Eeyorelife](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@Eeyorelife](https://discourse.processing.org/u/Eeyorelife)\
**Post date:** [October 10, 2018, 8:54pm UTC](https://discourse.processing.org/t/create-blinking-eyes/4343/3 "2018-10-10T20:54:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mick](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@Mick](https://discourse.processing.org/u/Mick)\
**Post date:** [October 11, 2018, 7:11am UTC](https://discourse.processing.org/t/create-blinking-eyes/4343/4 "2018-10-11T07:11:15Z")

</div>

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