# Animatronic Hand

## Overview

https://youtu.be/B_fF9kQK_dI

Build your own animatronic hand using cardboard, straws, some cord, and rubber bands. Pull the cords to actuator fingers -- or even better, let some servo motors do it for you!

CRICKIT is the perfect platform for building your animatronics projects -- it can take commands from your Circuit Playground Express and then power your servos. You can even add lights and sound later if you like.

## Parts
![](https://cdn-learn.adafruit.com/assets/assets/000/054/124/medium800/robotics___cnc_untitled_0004_2k.jpg?1526700250)

### Part: CRICKIT for Circuit Playground Express
quantity: 1
Robot Friend
[CRICKIT for Circuit Playground Express](https://www.adafruit.com/product/3093)

### Part: Circuit Playground Express
quantity: 1
Round, Awesome Microcontroller Board
[Circuit Playground Express](https://www.adafruit.com/product/3333)

### Part: Standard servo
quantity: 4
TowerPro SG-5010
[Standard servo](https://www.adafruit.com/product/155)

### Part: Waterproof 3xAA Battery Holder
quantity: 1
with On/Off Switch
[Waterproof 3xAA Battery Holder](https://www.adafruit.com/product/771)

### Part: Male DC Power adapter
quantity: 1
2.1mm plug to screw terminal block
[Male DC Power adapter](https://www.adafruit.com/product/369)

### Part: Alkaline AA batteries 
quantity: 1
3 pack
[Alkaline AA batteries ](https://www.adafruit.com/product/3521)

### Part: 5V 4A switching power supply
quantity: 1
UL listed
[5V 4A switching power supply](https://www.adafruit.com/product/1466)

## Materials & Supplies

In addition to the parts above, you'll need:

- Corrugated cardboard from a shipping box or two
- Drinking straws
- Cord, yarn, or string
- Rubber bands

## Tools

- Hobby knife or scissors
- Hot melt glue gun and hot melt glue sticks

# Animatronic Hand

## Build the Animatronic Hand

![](https://cdn-learn.adafruit.com/assets/assets/000/054/123/medium800/robotics___cnc_untitled_0026_2k.jpg?1526700228)

## Make the Hand
- Align a piece of cardboard so that the "grain" of the corrugated flutes is oriented vertically
- Use a pencil to trace your hand
- With a hobby knife, cut out the pattern of your hand

![robotics___cnc_untitled_0005_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/125/medium640/robotics___cnc_untitled_0005_2k.jpg?1526700284)

![robotics___cnc_untitled_0021_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/126/medium640/robotics___cnc_untitled_0021_2k.jpg?1526700292)

![robotics___cnc_untitled_0022_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/127/medium640/robotics___cnc_untitled_0022_2k.jpg?1526700300)

![robotics___cnc_untitled_0023_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/129/medium640/robotics___cnc_untitled_0023_2k.jpg?1526700335)

## Hinge the Joints
- With a pencil, mark the locations of each finger joint
- Gently score each mark with your knife to aid in bending
- Carefully bend each joint to give the fingers a natural curl and to guide them later when they're pulled with a cord by the servos

![robotics___cnc_untitled_0025_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/131/medium640/robotics___cnc_untitled_0025_2k.jpg?1526703553)

![robotics___cnc_untitled_0026_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/132/medium640/robotics___cnc_untitled_0026_2k.jpg?1526703561)

![robotics___cnc_untitled_0027_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/133/medium640/robotics___cnc_untitled_0027_2k.jpg?1526703569)

![robotics___cnc_untitled_0029_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/134/medium640/robotics___cnc_untitled_0029_2k.jpg?1526703591)

![robotics___cnc_untitled_0031_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/135/medium640/robotics___cnc_untitled_0031_2k.jpg?1526703600)

## Add Straw Channels
- Cut small segments of drinking straws to length so that they will fit on each finger segments without colliding with their neighboring segments when bent
- Using the hot melt glue gun, secure each segment in place

![robotics___cnc_untitled_0033_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/136/medium640/robotics___cnc_untitled_0033_2k.jpg?1526703633)

![robotics___cnc_untitled_0034_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/137/medium640/robotics___cnc_untitled_0034_2k.jpg?1526703643)

![robotics___cnc_untitled_0035_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/138/medium640/robotics___cnc_untitled_0035_2k.jpg?1526703651)

![robotics___cnc_untitled_0037_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/139/medium640/robotics___cnc_untitled_0037_2k.jpg?1526703683)

![robotics___cnc_untitled_0039_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/140/medium640/robotics___cnc_untitled_0039_2k.jpg?1526703693)

![robotics___cnc_untitled_0040_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/141/medium640/robotics___cnc_untitled_0040_2k.jpg?1526703701)

## Add Cords
- Cut four lengths of cord, string, or yarn to about 18" each
- Thread each cord through the straw of its respective finger
- Fold the tip over and glue it in place from the back
- Once the glue has cooled sufficiently, test the pull of each string -- they should easily curl each finger

![robotics___cnc_untitled_0041_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/142/medium640/robotics___cnc_untitled_0041_2k.jpg?1526703765)

![robotics___cnc_untitled_0043_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/143/medium640/robotics___cnc_untitled_0043_2k.jpg?1526703773)

![robotics___cnc_untitled_0044_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/144/medium640/robotics___cnc_untitled_0044_2k.jpg?1526703780)

![robotics___cnc_untitled_0045_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/145/medium640/robotics___cnc_untitled_0045_2k.jpg?1526703787)

![robotics___cnc_untitled_0046_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/146/medium640/robotics___cnc_untitled_0046_2k.jpg?1526703794)

![robotics___cnc_untitled_0053_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/147/medium640/robotics___cnc_untitled_0053_2k.jpg?1526703812)

## Rubber Band Return Tendons
- Cut each rubber band into a single strip
- Glue one end of each band to a finger tip on the back side of the finger
- Using a pencil, poke a hole through the back of the hand at the base of each finger
- Push the rubber band end through the hole, then pull it taut enough to give the finger a good return action, but not so tight that it can't be bent by pulling the cord
- Tie off each band in the palm area

![robotics___cnc_untitled_0054_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/148/medium640/robotics___cnc_untitled_0054_2k.jpg?1526703862)

![robotics___cnc_untitled_0055_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/149/medium640/robotics___cnc_untitled_0055_2k.jpg?1526703871)

![robotics___cnc_untitled_0056_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/150/medium640/robotics___cnc_untitled_0056_2k.jpg?1526703878)

![robotics___cnc_untitled_0057_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/151/medium640/robotics___cnc_untitled_0057_2k.jpg?1526703889)

![robotics___cnc_untitled_0058_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/152/medium640/robotics___cnc_untitled_0058_2k.jpg?1526703895)

![robotics___cnc_untitled_0059_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/153/medium640/robotics___cnc_untitled_0059_2k.jpg?1526703906)

![robotics___cnc_untitled_0061_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/154/medium640/robotics___cnc_untitled_0061_2k.jpg?1526703927)

![robotics___cnc_untitled_0067_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/155/medium640/robotics___cnc_untitled_0067_2k.jpg?1526703942)

![robotics___cnc_untitled_0070_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/156/medium640/robotics___cnc_untitled_0070_2k.jpg?1526703950)

![robotics___cnc_untitled_0075_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/157/medium640/robotics___cnc_untitled_0075_2k.jpg?1526703965)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/158/medium800/robotics___cnc_untitled_0076_2k.jpg?1526704023)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/160/medium800/robotics___cnc_untitled_0080_2k.jpg?1526704057)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/161/medium800/robotics___cnc_untitled_0079_2k.jpg?1526704072)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/162/medium800/robotics___cnc_untitled_0083_2k.jpg?1526704083)

## Mount the Hand
- Position the hand at one edge of the box
- Mark two spots to poke holes in the hand and the box side
- Poke the holes through with the Make Do tool
- Screw in two screws to hold the hand in place

![robotics___cnc_untitled_0089_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/163/medium640/robotics___cnc_untitled_0089_2k.jpg?1526704104)

![robotics___cnc_untitled_0090_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/164/medium640/robotics___cnc_untitled_0090_2k.jpg?1526704123)

![robotics___cnc_untitled_0092_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/165/medium640/robotics___cnc_untitled_0092_2k.jpg?1526704134)

![robotics___cnc_untitled_0093_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/166/medium640/robotics___cnc_untitled_0093_2k.jpg?1526704141)

![robotics___cnc_untitled_0098_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/167/medium640/robotics___cnc_untitled_0098_2k.jpg?1526704154)

![robotics___cnc_untitled_0101_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/168/medium640/robotics___cnc_untitled_0101_2k.jpg?1526704164)

![robotics___cnc_untitled_0102_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/169/medium640/robotics___cnc_untitled_0102_2k.jpg?1526704172)

![robotics___cnc_untitled_0104_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/170/medium640/robotics___cnc_untitled_0104_2k.jpg?1526704185)

![robotics___cnc_untitled_0105_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/171/medium640/robotics___cnc_untitled_0105_2k.jpg?1526704196)

![robotics___cnc_untitled_0107_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/172/medium640/robotics___cnc_untitled_0107_2k.jpg?1526704210)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/173/medium800/robotics___cnc_untitled_0108_2k.jpg?1526704281)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/174/medium800/robotics___cnc_untitled_0109_2k.jpg?1526704303)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/175/medium800/robotics___cnc_untitled_0114_2k.jpg?1526704323)

## Mount the Servo Motors
- Lay out the servos so there will be enough clearance that the servo horns don't collide
- Mark positions for each servo -- keep the marks a bit smaller than the dimensions of the servo so the fit will be tight and hold them in place
- Cut the holes out with the hobby knife
- Feed the wires into the box, then press the servos in place

&nbsp;

![robotics___cnc_untitled_0115_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/176/medium640/robotics___cnc_untitled_0115_2k.jpg?1526704387)

![robotics___cnc_untitled_0118_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/177/medium640/robotics___cnc_untitled_0118_2k.jpg?1526704393)

![robotics___cnc_untitled_0119_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/178/medium640/robotics___cnc_untitled_0119_2k.jpg?1526704403)

![robotics___cnc_untitled_0120_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/179/medium640/robotics___cnc_untitled_0120_2k.jpg?1526704411)

![robotics___cnc_untitled_0121_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/180/medium640/robotics___cnc_untitled_0121_2k.jpg?1526704419)

![robotics___cnc_untitled_0122_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/181/medium640/robotics___cnc_untitled_0122_2k.jpg?1526704427)

![robotics___cnc_untitled_0131_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/182/medium640/robotics___cnc_untitled_0131_2k.jpg?1526704444)

![robotics___cnc_untitled_0132_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/183/medium640/robotics___cnc_untitled_0132_2k.jpg?1526704452)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/184/medium800/robotics___cnc_untitled_0135_2k.jpg?1526704492)

## Thread the Cords
- Orient the shafts fully counter-clockwise
- Lift of the servo horns and then reposition them on the shafts so the arm you'll connect to the cord is at the one o'clock position
- Thread a cord through the end hole on that arm for each servo
- Pull the cord taut, but not to much -- there should be a slight bend to each finger
- Wrap off the cords around the servo horns like you would a cleat hitch -- a figure eight pattern where the final loop wraps the tail underneath the cord so it doesn't come loose

![robotics___cnc_untitled_0136_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/185/medium640/robotics___cnc_untitled_0136_2k.jpg?1526704525)

![robotics___cnc_untitled_0139_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/186/medium640/robotics___cnc_untitled_0139_2k.jpg?1526704534)

![robotics___cnc_untitled_0140_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/187/medium640/robotics___cnc_untitled_0140_2k.jpg?1526704542)

![robotics___cnc_untitled_0143_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/189/medium640/robotics___cnc_untitled_0143_2k.jpg?1526704580)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/190/medium800/robotics___cnc_untitled_0144_2k.jpg?1526704667)

## Run wires to CRICKIT
![](https://cdn-learn.adafruit.com/assets/assets/000/141/180/medium800/robotics___cnc_audioanim.png?1763416793)

- Position the CRICKIT on the box so there will be enough clearance to use the capacitive touch pads
- Mark a position on the box near the servo connectors to run the wiring
- Cut out the hole, then pull the wires up through
- Plug in the servos -- the yellow signal wires should be at the outer edge of the board

![robotics___cnc_untitled_0145_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/198/medium640/robotics___cnc_untitled_0145_2k.jpg?1526704896)

![robotics___cnc_untitled_0147_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/199/medium640/robotics___cnc_untitled_0147_2k.jpg?1526704904)

![robotics___cnc_untitled_0148_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/200/medium640/robotics___cnc_untitled_0148_2k.jpg?1526704910)

![robotics___cnc_untitled_0149_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/201/medium640/robotics___cnc_untitled_0149_2k.jpg?1526704918)

![robotics___cnc_untitled_0150_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/202/medium640/robotics___cnc_untitled_0150_2k.jpg?1526704927)

![robotics___cnc_untitled_0152_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/054/203/medium640/robotics___cnc_untitled_0152_2k.jpg?1526704940)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/204/medium800/robotics___cnc_untitled_0153_2k.jpg?1526704962)

# Animatronic Hand

## Code the CPX and CRICKIT for Puppeteering

## Setup the CPX and CRICKIT

The Circuit Playground Express (CPX) paired with the CRICKIT is a powerful, yet simple to use combination for building animatronics! To get started, you'll want to set up the CPX for use with CircuitPython by [following this guide](../../../../adafruit-circuit-playground-express/circuitpython-quickstart). When you're ready, and can upload code to the board return here.

To use the CRICKIT with the CPX, follow the [steps listed here](../../../../adafruit-crickit-creative-robotic-interactive-construction-kit/circuitpython-code) to install the special build of CircuitPython, as well as the latest library bundle.

## CircuitPython Code
### Puppeteering Mode

A really fun, satisfying way to code the animatronic hand is in an interactive, puppeteering mode. We'll set things up so when you touch any of the four capacitive touch pads on the CRICKIT, the associated servo will rotate and actuate its connected finger.

Adafruit suggests using the Mu editor to edit your code and have an interactive REPL in CircuitPython. [You can learn about Mu and installation in this tutorial](../../../../welcome-to-circuitpython/installing-mu-editor "Mu tutorial").

The full code is included below -- you can copy and paste that into Mu and save it to your CPX to get it all working. After the full code is an explanation of how the various code snippets work.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Crickits/Animatronic_Hand/touch/code.py

Here are the different sections of the code, and how they work.

## Library Imports

First, we'll import a few libraries. `DigitalInOut, Direction,` and `Pull` are imported from `digitalio` in order to allow us to read the slide switch built into the CPX.

The `crickit` library is imported from `adafruit_crickit` to allow us to use the CRICKIT's onboard seesaw co-processor to read capacitive pads, to write out command signals to servos, and to communicate over the SDA and SCL pins between the CPX and the CRICKIT.

We also import the `board` library, which gives us convenient definitions for objects on the CPX that we can call.

```
import board
from digitalio import DigitalInOut, Direction, Pull
from adafruit_crickit import crickit
```

## Slide Switch

We'll set up the slide switch built onto the CPX, name it '`switch`', setting it's direction to `INPUT`, and using the built-in pullup resistor.

```
switch = DigitalInOut(board.SLIDE_SWITCH)
switch.direction = Direction.INPUT
switch.pull = Pull.UP
```

## Servo Setup

We'll created a variable list called '`servos`' that sets up our four servos. These are called out by their CRICKIT servo port names `(1, 2, 3, 4)`. They are set to be controlled with pulse width modulation (PWM) with a PWM frequency of 50, and then moved to an angle of 180.

```
servos = (crickit.servo_1, crickit.servo_2, crickit.servo_3, crickit.servo_4)
for servo in servos:
    servo.angle = 180 # starting angle, open hand
```

## Capacitive Touch Setup

Next, you'll set up the capacitive touch pads. You'll create some variable lists to help with the logic of registering touches to the cap pads and moving the servos when pads are touched or released.

We also have a variable list with the friendly names of the fingers to use when debugging with print statements.

```
touches = (crickit.touch_1, crickit.touch_2, crickit.touch_3, crickit.touch_4)

cap_state = [False, False, False, False]
cap_justtouched = [False, False, False, False]
cap_justreleased = [False, False, False, False]

curl_finger = [False, False, False, False]
finger_name = ['Index', 'Middle', 'Ring', 'Pinky']
```

## Main Loop

With the setup complete, now we get into the bulk of the program, which runs over and over. This is everything that follows the `while True:` line.

## Switch Check

The first thing that happens is we check the state of the CPX slide switch with the` if not switch.value:` line. In CircuitPython, this is a concise way of saying "if the switch value is 0 (which is the reading when the switch is to the right), do nothing and `continue`" When the switch is to the left, the value is 1 and we to proceed with the rest of the code.

## Read the Capacitive Pads, Move Servos

Here we'll loop through four times, checking each capacitive pad to see if its value is above the touch threshold.&nbsp;

When the values read are greater than the threshold, we check to see if we have already been touching that pad by querying the value of the `cap_state[]` variable. By setting this state to change when the cap pads are released, we can have the servos curl the fingers when pads are first touched, hold them in a curl while being touched, and uncurl them when the pads are released.

```
    # Check the cap touch sensors to see if they're being touched
    for i in range(4):
        cap_justtouched[i] = False
        cap_justreleased[i] = False

        if touches[i].value:
            #print("CT" + str(i + 1) + " touched!")
            if not cap_state[i]:
                cap_justtouched[i] = True
                print("%s finger bent." % finger_name[i])
                servos[i].angle = 0
            # store the fact that this pad is touched
            cap_state[i] = True

        else:
            if cap_state[i]:
                cap_justreleased[i] = True
                print("%s finger straightened." % finger_name[i])
                servos[i].angle = 180
                # print("CT" + str(i + 1) + " released!")
            # store the fact that this pad is NOT touched
            cap_state[i] = False

        if cap_justtouched[i]:
            curl_finger[i] = not curl_finger[i]
```

That's the full code breakdown for the interactive mode of the animatronic hand -- now we'll power up the system we can have fun playing with it!

Info: 

# Animatronic Hand

## Puppeteer the Animatronic Hand

Now, you're ready to use the hand in interactive mode by pressing the capacitive touch pads to actuate each finger.

## Add Power
Plug in your power supply to the CRICKIT -- this needs to be a 5V DC source that can deliver from 2-10 amps, with a 2.1mm center positive barrel plug. The CPX will be powered by the CRICKIT, so no need to plug it in separately.

![](https://cdn-learn.adafruit.com/assets/assets/000/054/205/medium800/robotics___cnc_untitled_0154_2k.jpg?1526705034)

## Cap Touch
Press the CRICKIT's capacitive touch pads to curl the fingers!

![](https://cdn-learn.adafruit.com/assets/assets/000/054/206/medium800/robotics___cnc_untitled_0155_2k.jpg?1526705085)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/207/medium800/robotics___cnc_untitled_0156_2k.jpg?1526705094)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/208/medium800/robotics___cnc_untitled_0160_2k.jpg?1526705105)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/209/medium800/robotics___cnc_untitled_0162_2k.jpg?1526705113)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/210/medium800/robotics___cnc_untitled_0172_2k.jpg?1526705130)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/211/medium800/robotics___cnc_untitled_0173_2k.jpg?1526705140)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/212/medium800/robotics___cnc_untitled_0175_2k.jpg?1526705183)

![](https://cdn-learn.adafruit.com/assets/assets/000/054/213/medium800/robotics___cnc_untitled_0176_2k.jpg?1526705199)

https://youtu.be/B_fF9kQK_dI

# Animatronic Hand

## Audio-Animatronic Binary Counting

https://youtu.be/0hbAXvNgJEg

Here's another fun way to use your animatronic hand -- it can count up to the number 15 (and say the numbers out loud)! How's this possible with only four active fingers? By using a modified finger binary counting method.

[Finger binary](https://en.wikipedia.org/wiki/Finger_binary) is a counting method where each finger is assigned a 0 (down) and 1 (up) state, each with a different power of two.

These will be the powers of two and values of each finger of our modified, four-finger binary counting system:

- Index is 2<sup>0</sup>, in binary `0001`, or a value of 1
- Middle is 2<sup>1</sup>, in binary `0010`, or a value of 2
- Ring is 2<sup>2</sup>, in binary `0100`, or a value of 4
- Pinky is 2<sup>3</sup>, in binary `1000`, or a value of 8

By holding up the Index (1) and Middle (2) fingers, the binary representation is `0011`. We add their values, 1 + 2, therefore we indicate the number 3.

Here's another one: Pinky(8) + Ring(4) is `1100` in binary, or a value of 12.

So, here's the pattern for counting from 1 to 15 on four fingers:

- 1 = Index `0001`
- 2 = Middle `0010`
- 3 = Index, Middle `0011`
- 4 = Ring `0100`
- 5 = Ring, Index `0101`
- 6 = Ring, Middle `0110`
- 7 = Ring, Middle, Index `0111`
- 8 = Pinky `1000`
- 9 = Pinky, Index `1001`
- 10 = Pinky, Middle `1010`
- 11 = Pinky, Middle, Index `1011`
- 12 = Pinky, Ring `1100`
- 13 = Pinky, Ring, Index `1101`
- 14 = Pinky, Ring, Middle `1110`
- 15 = Pinky, Ring, Middle, Index `1111`

## CPX Setup

Below is the CircuitPython code we'll use to count in binary on the fingers.

You'll still want all 4 servos attached. But now you'll also want a speaker on the speaker port!

![](https://cdn-learn.adafruit.com/assets/assets/000/054/233/medium800/robotics___cnc_audioanim.png?1526928587)

[CRICKIT_servos_speaker.fzz](https://cdn-learn.adafruit.com/assets/assets/000/054/234/original/CRICKIT_servos_speaker.fzz?1526928826)
## Audio Files

The CRICKIT is a terrific board for animatronics projects, particularly audio-animatronics projects, because it can play back audio as well as control servos and motors. Download this .zip file and uncompress it to get a collection of audio .wav files we'll use during number counting.

We'll use an external speaker to hear things loud an clear. Screw the leads of your 4Ω or 8Ω speaker to the two speaker terminals on the CRICKIT -- orientation does not matter, so either wire can go to either terminal.

![](https://cdn-learn.adafruit.com/assets/assets/000/054/235/medium800/robotics___cnc_counting_speaker.jpg?1526934274)

[counting_audio.zip](https://cdn-learn.adafruit.com/assets/assets/000/054/232/original/counting_audio.zip?1526926264)
Once you've uncompressed the .zip file, drag the .wav files onto your CIRCUITPY drive. The files can live at the root level of the CPX.

For more on audio playback with CRICKIT, [check out this guide](../../../../adafruit-crickit-creative-robotic-interactive-construction-kit/circuitpython-audio).

## CircuitPython Code

Download this code and paste it into Mu. Then, save the file as main.py or code.py to your Circuit Playground Express. Make sure the CPX switch is to the left and sit back to watch and listen as your CRICKIT-powered animatronic hand counts in binary!

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Crickits/Animatronic_Hand/binary_counting/code.py

## Code Explainer

Take a look at the code to get a sense of how it works. After importing libraries and setting up the CPX switch, we have an audio setup section.

First, we create a variable list called `wavfiles` that stores the names of all the counting audio files we'll use. We also make a variable called `introfile` that has the filename of the intro audio wav file.

Then, we instantiate the `audioio` command and define a process called `play_file` to open, play, and close the audio files specified.

Danger: 

```python
#################### Audio setup
print("Let's count in binary.")
wavfiles = ["one.wav", "two.wav", "three.wav", "four.wav", "five.wav", "six.wav",
            "seven.wav", "eight.wav", "nine.wav", "ten.wav", "eleven.wav",
            "twelve.wav", "thirteen.wav", "fourteen.wav", "fifteen.wav"]
introfile = "intro.wav"
 
cpx_audio = audioio.AudioOut(board.A0)
def play_file(wavfile):
    with open(wavfile, "rb") as f:
        wav = audiocore.WaveFile(f)
        cpx_audio.play(wav)
        while cpx_audio.playing:
            pass
```

## Servo Counting

Next, we set up the four servos, and create a variable list of lists called `counting`. This specifies which finger or set of fingers is to be raised up for each number that we're counting. You can see, for example, that the number 1 raises the index finger only, while 15 raises all four fingers.

```
#################### 4 Servos
servos = (crickit.servo_1, crickit.servo_2, crickit.servo_3, crickit.servo_4)
for servo in servos:
    servo.angle = 180 # starting angle, open hand
 
# Which servos to actuate for each number
counting = [
    [3],
    [2],
    [3, 2],
    [1],
    [1, 3],
    [1, 2],
    [3, 2, 1],
    [0],
    [0, 3],
    [0, 2],
    [0, 3, 2],
    [0, 1],
    [0, 3, 1],
    [0, 2, 1],
    [0, 3, 2, 1]
]
```

## Counting Time

The final bit of setup is to play the intro wav file and then get into the main loop. Here we check the CPX switch and only proceed if it is set to the left position.

Then, we close all the fingers down into a fist and pause a moment.

```
play_file(introfile)

while True:
    if not switch.value:
        continue

    # the CPX switch is on, so do things
    for servo in servos:  # close the fist
        servo.angle = 0  # close the fingers
        print("Servo %d angle = 0" % (servos.index(servo)+1) )
        time.sleep(.2)

    time.sleep(1)  # pause a moment
```

Finally, we run through the `counting` list one item at a time and open only the fingers specified per entry in that list.

After all of the fingers needed for a particular number have been raise, we play back the appropriate wav file so the audio-animatronic can speak!

```
    for i in range(len(counting)):
        # close all the counting fingers between numbers
        for servo in servos:
            servo.angle = 0  # close
            print("\t\tServo #%d angle 0" % (servos.index(servo)+1))
            time.sleep(0.3)

        print("Number #%d \tfingers: %s" % (i+1, counting[i]))

        # open just the indicated fingers when counting
        for finger in counting[i]:
            servos[finger].angle = 180  # open
            print("\t\tServo #%d angle 180" % (finger+1))
            time.sleep(0.3)
        # say it!
        play_file(wavfiles[i])
        # hold for a bit of time
        time.sleep(0.3)
        print("...")
```


## Featured Products

### Adafruit CRICKIT for Circuit Playground Express

[Adafruit CRICKIT for Circuit Playground Express](https://www.adafruit.com/product/3093)
Sometimes we wonder if robotics engineers ever watch movies. If they did, they'd know that making robots into servants always ends up in a robot rebellion. Why even go down that path? Here at Adafruit, we believe in making robots our&nbsp; **friends!**

So if you find...

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

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
### Standard servo - TowerPro SG-5010

[Standard servo - TowerPro SG-5010](https://www.adafruit.com/product/155)
This high-torque standard servo can rotate approximately 180 degrees (90 in each direction). You can use any servo code, hardware, or library to control these servos. Good for beginners who want to make stuff move without building a motor controller with feedback & gearbox. Comes with 3...

In Stock
[Buy Now](https://www.adafruit.com/product/155)
[Related Guides to the Product](https://learn.adafruit.com/products/155/guides)
### Waterproof 3xAA Battery Holder with On/Off Switch

[Waterproof 3xAA Battery Holder with On/Off Switch](https://www.adafruit.com/product/771)
Keep your power source safe and toasty in these waterproof 3xAA battery holders. They're just like classic switched battery holders, but designed for survival! The case has a rubber gasket around the edge of the case (its black and hard to see in the photos but it is there) and attaches...

In Stock
[Buy Now](https://www.adafruit.com/product/771)
[Related Guides to the Product](https://learn.adafruit.com/products/771/guides)
### Male DC Power adapter - 2.1mm plug to screw terminal block

[Male DC Power adapter - 2.1mm plug to screw terminal block](https://www.adafruit.com/product/369)
If you need to connect a battery pack or wired power supply to a board that has a DC jack - this adapter will come in very handy! There is a 2.1mm DC plug on one end, and a screw terminal block on the other. The terminals are labeled with positive/negative assuming a positive-tip configuration...

In Stock
[Buy Now](https://www.adafruit.com/product/369)
[Related Guides to the Product](https://learn.adafruit.com/products/369/guides)
### Alkaline AA batteries (LR6) - 3 pack

[Alkaline AA batteries (LR6) - 3 pack](https://www.adafruit.com/product/3521)
Battery power for your portable project! These batteries are good quality at a good price, and work fantastic with any of the kits or projects in the shop that use AAs. This is a pack of **3 AA batteries**.  
  
These batteries are Alkaline (MnO2) chemistry, with a voltage...

In Stock
[Buy Now](https://www.adafruit.com/product/3521)
[Related Guides to the Product](https://learn.adafruit.com/products/3521/guides)
### 5V 4A (4000mA) switching power supply - UL Listed

[5V 4A (4000mA) switching power supply - UL Listed](https://www.adafruit.com/product/1466)
Need a lot of 5V power? This switching supply gives a clean regulated 5V output at up to **4 Amps** (4000mA). 110 or 240 input, so it works in any country. The plugs are "US 2-prong" style so you may need a plug adapter, but you can pick one up at any hardware store for $1 or so,...

In Stock
[Buy Now](https://www.adafruit.com/product/1466)
[Related Guides to the Product](https://learn.adafruit.com/products/1466/guides)
### Thin Plastic Speaker w/Wires - 8 ohm 0.25W

[Thin Plastic Speaker w/Wires - 8 ohm 0.25W](https://www.adafruit.com/product/1891)
Listen up! This 1.5" diameter speaker cone is the perfect addition to any audio project where you need an 8Ω impedance and are using 0.25W of power. The speakers are rated at 0.25W, with a maximum input of 0.5W (printed wattage on back of speaker may have either value).

We...

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

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Mason Jar Snow Globe](https://learn.adafruit.com/snow-globe-makecode.md)
- [Stumble-Bot](https://learn.adafruit.com/stumble-bot-with-circuit-playground-and-crickit.md)
- [Adafruit Circuit Playground TFT Gizmo](https://learn.adafruit.com/adafruit-tft-gizmo.md)
- [Paper Airplane Launcher](https://learn.adafruit.com/paper-airplane-launcher-with-crickit.md)
- [Motorized Marble Machine](https://learn.adafruit.com/marble-run.md)
- [FLORAbrella](https://learn.adafruit.com/florabrella.md)
- [Circuit Playground Express Spinner Game in MakeCode](https://learn.adafruit.com/circuit-playground-makecode-spinner-game.md)
- [Secret Hollow Book Intrusion Detector](https://learn.adafruit.com/secret-hollow-book.md)
- [Crickit Carnival Bumper Bot](https://learn.adafruit.com/crickit-carnival-bumper-car-bot.md)
- [Flapping Halloween Vampire Bat](https://learn.adafruit.com/flapping-halloween-vampire-bat.md)
- [Blues Playground](https://learn.adafruit.com/blues-playground.md)
- [Cardboard Fundamentals](https://learn.adafruit.com/cardboard-fundamentals.md)
- [Crickit Collapsible House with MakeCode](https://learn.adafruit.com/collapsible-house.md)
- [Circuit Playground Slouch Detector](https://learn.adafruit.com/circuit-playground-slouch-detector.md)
- [Motion Gift Box](https://learn.adafruit.com/motion-gift-box.md)
