# HalloWing Googly Eye

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/065/226/medium800thumb/microcontrollers_googly-anim-wide.jpg?1541377124)

In the pantheon of corny gags — rubber chickens, whoopee cushions and so forth — there’s a special place for&nbsp;_ **googly eyes** ,_ that staple of childrens crafts and tacky “You’re HOW old?” birthday cards.

<sub><span class="cc-license-identifier">Photo credit: </span></sub>[<sub><span class="cc-license-identifier">Googly Punch by Lenore Edman on Flickr, CC-BY-2.0 license.</span></sub>](https://www.flickr.com/photos/lenore-m/5956271883/)

![microcontrollers_GooglyPunch.jpg](https://cdn-learn.adafruit.com/assets/assets/000/065/195/medium640/microcontrollers_GooglyPunch.jpg?1541275944)

We’ve done _realistic_ eyes before…it’s [the default program that ships on the HalloWing.](https://learn.adafruit.com/adafruit-hallowing/spooky-eyes) Suppose we desire something more…_zany?_

HalloWing has an accelerometer and can respond to motion…let’s make it into a googly eye!

![microcontrollers_hallowing-eye-small.gif](https://cdn-learn.adafruit.com/assets/assets/000/065/204/medium640thumb/microcontrollers_hallowing-eye-small.jpg?1541298392)

 **Everything needed for this project is built into the HalloWing board, no extra components are required.** (A battery can optionally be added to make it self-contained and portable.)

### Adafruit HalloWing M0 Express

[Adafruit HalloWing M0 Express](https://www.adafruit.com/product/3900)
[This is Hallowing..this is Hallowing... Hallowing! Hallowing!&nbsp;](https://www.youtube.com/watch?v=kGiYxCUAhks&t=39s)

Are you the kind of person who doesn't like taking down the skeletons and spiders until after January? Well, we've got the development board for...

In Stock
[Buy Now](https://www.adafruit.com/product/3900)
[Related Guides to the Product](https://learn.adafruit.com/products/3900/guides)
![Video of a blinking eye on a Adafruit HalloWing M0 Express.](https://cdn-shop.adafruit.com/product-videos/640x480/3900-07.jpg)

# HalloWing Googly Eye

## Software

## Easy Way

If you want to get started **quickly** , download the UF2 file linked below. Turn on HalloWing and connect a USB cable to your computer. Double-click HalloWing’s reset button, wait for the **HALLOWBOOT** drive to appear, then drag the UF2 file to this drive. After a few seconds, the code should be finished transferring and will run.

[HALLOWING_GOOGLY_EYE.UF2](https://cdn-learn.adafruit.com/assets/assets/000/065/247/original/HALLOWING_GOOGLY_EYE.UF2?1541453079)
Here’s a color variant if you’d prefer, modeled after a popular sports mascot:

[HALLOWING_GOOGLY_COLOR.UF2](https://cdn-learn.adafruit.com/assets/assets/000/066/227/original/HALLOWING_GOOGLY_COLOR.UF2?1542675210)
 **This will overwrite CircuitPython if it’s currently installed on your board** (but your CircuitPython code and any libraries are safe).

You can **restore CircuitPython** easily by [**following the directions here**](https://learn.adafruit.com/adafruit-hallowing/circuitpython).

## Build From Source

Building the project from source gives you the opportunity to **customize** the physics a bit.

This requires the **Arduino IDE** software for your computer and **Adafruit SAMD board support** , [as **explained in this guide**](https://learn.adafruit.com/adafruit-hallowing/setup).

**Several libraries** are also required, which can be installed through the **Arduino&nbsp;Library Manager** (Sketch→Include Library→Manage Libraries…):

- Adafruit\_LIS3DH
- Adafruit\_GFX
- Adafruit\_BusIO
- Adafruit\_ST7735
- Adafruit\_ZeroDMA

[Googly eye source code on Github](https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/master/Hallowing_Googly_Eye)
A few definitions near the top of the code determine how things behave…

```
#define G_SCALE       40.0   // Accel scale; no science, just looks good
#define ELASTICITY     0.80  // Edge-bounce coefficient (MUST be &lt;1.0!)
#define DRAG           0.996 // Dampens motion slightly
```

`G_SCALE` influences the pull of gravity; higher numbers = stronger gravity.

`ELASTICITY` determines the “bounce” when the pupil hits an edge. 1.0 = no energy lost in the bounce, 0.0 = complete stop. This should be set somewhere _between_ these two values, not-inclusive.

`DRAG` slows the pupil movement from frame to frame, helping it come to a stop at the bottom of the eye. This too should be between 0.0 and 1.0.

The eye graphics are not easily customized, but the next page explains some of the program’s internals which experienced programmers might be able to work from.

# HalloWing Googly Eye

## How it Works

The code uses two grayscale images, nicely **antialiased** so they’re not visibly “jaggy.” The first, 128 by 128 pixels (matching the HalloWing screen size) represents the outside boundary of the googy eye:

![](https://cdn-learn.adafruit.com/assets/assets/000/065/136/medium800/microcontrollers_border.png?1541214901)

The second, 48 by 48 pixels, represents the pupil. It’s been drawn with a bit of a gleam because _specular highlights_ make everything at least three times funnier:

![](https://cdn-learn.adafruit.com/assets/assets/000/065/137/medium800/microcontrollers_pupil.png?1541214907)

The two images are embedded in the code (in the file `graphics.h`) as _uint8\_t_ (unsigned 8-bit) arrays. The conversion was done with some throwaway Python code. Each byte represents a brightness from 0 (black) to 255 (white).

Getting the pupil to “google” properly took some doing. Making something to bounce in a rectangular space is easy — just reverse horizontal or vertical motion where an edge collision occurs — but in this googly setting, a collision may occur at _any_ angle.

The fact that the eye and pupil are both perfect circles makes a **shortcut** possible…

Rather than calculating the intersections between two circles (the 128-pixels-across outside boundary, and the 48-pixels-across pupil), we can more easily process this as a _single point_ moving within an 80 pixel wide circle (the 128 pixel boundary minus the 48 pixel pupil).

You’ll see constants for these `#defined` in the code, though it’s using radii (half these measurements) rather than diameters (the aforementioned pixel dimensions). Using a Cartesian coordinate system with (0,0) at the screen center also helps.

![microcontrollers_phys1.png](https://cdn-learn.adafruit.com/assets/assets/000/065/199/medium640/microcontrollers_phys1.png?1541289153)

As each frame of animation is processed, the pupil’s vector of motion is modified by input from the accelerometer, plus a tiny bit of drag or resistance to keep things from sliding forever.

If that vector of motion would have the pupil’s center point crossing outside the 80-pixel threshold, the path of motion is reflected back into the eye at a suitable angle, and both the position and velocity are scaled back slightly to represent an inelastic collision (where some kinetic energy is lost due to internal friction), else it would never stop bouncing.

![microcontrollers_phys2.png](https://cdn-learn.adafruit.com/assets/assets/000/065/201/medium640/microcontrollers_phys2.png?1541289663)

The pull of gravity and these other coefficients can be changed near the top of the code:

```
#define G_SCALE       40.0   // Accel scale; no science, just looks good
#define ELASTICITY     0.80  // Edge-bounce coefficient (MUST be &lt;1.0!)
#define DRAG           0.996 // Dampens motion slightly
```

The `G_SCALE` value isn’t scientifically determined…it just seemed a reasonably “realistic” value with some trial and error. `ELASTICITY` and `DRAG` also aren’t based on any specific real-world material physical properties, again just trial and error with what felt “real.” You can fiddle around with these, but the latter two values should not be equal or greater than 1.0 (else the pupil will _pick up_ energy rather than settling at the bottom).

Each frame, the code updates the minimum **bounding rectangle** of the pupil’s old and new positions. The whole rectangle is **drawn in one pass** , rather than erasing the old pupil and then drawing the new one, which would exhibit a lot of **flicker** and destroy the illusion.

Though the HalloWing’s TFT display supports only 5 bits for red and blue, and 6 bits for green, the boundary and pupil images are stored using 8 bits per pixel. We’re not pressed for space and the math for overlaying the two images is just easier that way. The conversion to “565” color is performed as each 8-bit value is processed.

![microcontrollers_phys3.png](https://cdn-learn.adafruit.com/assets/assets/000/065/203/medium640/microcontrollers_phys3.png?1541294407)

Unlike the original “spooky eye” code, which allows [two HalloWings to communicate and stay in sync](https://learn.adafruit.com/synchronized-eyes-with-two-hallowings), this project _intentionally_ does not operate like that. The desynchronization of two eyes reacting to slightly different inputs adds to the hilarity — it’s exactly how googly eyes should be!

# Color Eye

The principle behind the color eye’s operation is exactly the same, only the graphics are different. The code is a bit more complicated in this case because it has to blend two RGB images rather than grayscale, but the code shares a lot in common.

The color eye is enabled near the top of the code, where it normally looks like this:

```
#include "graphics.h"
//#include "gritty.h"
```

Just switch out the comment characters (“//”) so the second line is enabled rather than the first:

```
//#include "graphics.h"
#include "gritty.h"
```

This tells the compiler to use a different graphics file, where a flag is also set to enable the color drawing code.

There’s the same 128x128 size background, but now in color:

![](https://cdn-learn.adafruit.com/assets/assets/000/066/228/medium800/microcontrollers_gritty-background.png?1542676206)

And the pupil is just slightly larger than the grayscale version, now 54 by 54 pixels:

![](https://cdn-learn.adafruit.com/assets/assets/000/066/229/medium800/microcontrollers_gritty-pupil.png?1542676243)


## Featured Products

### Adafruit HalloWing M0 Express

[Adafruit HalloWing M0 Express](https://www.adafruit.com/product/3900)
[This is Hallowing..this is Hallowing... Hallowing! Hallowing!&nbsp;](https://www.youtube.com/watch?v=kGiYxCUAhks&t=39s)

Are you the kind of person who doesn't like taking down the skeletons and spiders until after January? Well, we've got the development board for...

In Stock
[Buy Now](https://www.adafruit.com/product/3900)
[Related Guides to the Product](https://learn.adafruit.com/products/3900/guides)
### Lithium Ion Polymer Battery Ideal For Feathers - 3.7V 400mAh

[Lithium Ion Polymer Battery Ideal For Feathers - 3.7V 400mAh](https://www.adafruit.com/product/3898)
Lithium-ion polymer (also known as 'lipo' or 'lipoly') batteries are thin, light, and powerful. The output ranges from 4.2V when completely charged to 3.7V. This battery has a capacity of **400mAh** for a total of about 1.9 Wh. If you need a larger (or smaller!)...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3898)
[Related Guides to the Product](https://learn.adafruit.com/products/3898/guides)
### AdaBox009 - HalloWing

[AdaBox009 - HalloWing](https://www.adafruit.com/product/3956)
Are you the kind of person who doesn't like taking down the skeletons and spiders until after January? Well, we've got the ADABOX for you. This is electronics at its&nbsp;_most spooky!_&nbsp;ADABOX 009 is Halloween themed, for all of us who love scares, costumes,&nbsp;and (of...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/3956)
[Related Guides to the Product](https://learn.adafruit.com/products/3956/guides)
### AdaBox009 - HalloWing

[AdaBox009 - HalloWing](https://www.adafruit.com/product/3863)
Are you the kind of person who doesn't like taking down the skeletons and spiders until after January? Well, we've got the ADABOX for you. This is electronics at its&nbsp;_most spooky!_&nbsp;ADABOX 009 is Halloween themed, for all of us who love scares, costumes,&nbsp;and (of...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3863)
[Related Guides to the Product](https://learn.adafruit.com/products/3863/guides)

## Related Guides

- [Adafruit Hallowing M0](https://learn.adafruit.com/adafruit-hallowing.md)
- [HalloWing Light Paintstick](https://learn.adafruit.com/hallowing-light-paintstick.md)
- [Reindeer Mask with Animated Eyes](https://learn.adafruit.com/reindeer-mask-with-animated-eyes.md)
- [Milk Jug Glow Skull](https://learn.adafruit.com/milk-jug-glow-skull.md)
- [Last-Minute Halloween Accoutrements with HalloWing](https://learn.adafruit.com/last-minute-halloween-accoutrements-with-hallowing.md)
- [Hallowing Minotaur Maze](https://learn.adafruit.com/hallowing-minotaur-maze.md)
- [3D Printed Starro Face Mask](https://learn.adafruit.com/3d-printed-starro-face-mask.md)
- [Hallowing M0 Spirit Board](https://learn.adafruit.com/hallowing-spirit-board.md)
- [HalloWing Jump Scare Trap](https://learn.adafruit.com/hallowing-jump-scare-trap.md)
- [Tiny Museum Tour Device](https://learn.adafruit.com/tiny-museum-tour-device.md)
- [HalloWing Lightsabers](https://learn.adafruit.com/hallowing-lightsaber.md)
- [Introducing Adafruit Feather](https://learn.adafruit.com/adafruit-feather.md)
- [HalloWing Magic 9 Ball](https://learn.adafruit.com/hallowing-magic-9-ball.md)
- [The Great Seal Mask](https://learn.adafruit.com/the-great-seal-mask.md)
- [HalloWing Badge Image Viewer](https://learn.adafruit.com/hallowing-badge.md)
