|
| 1 | +# SPDX-FileCopyrightText: 2018 John Edgar Park for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +# Animatronic Hand |
| 6 | +# Binary Counting on four fingers up to 15 |
| 7 | +import time |
| 8 | +from digitalio import DigitalInOut, Direction, Pull |
| 9 | +import audioio |
| 10 | +import audiocore |
| 11 | +import board |
| 12 | +from adafruit_crickit import crickit |
| 13 | + |
| 14 | +#################### CPX switch |
| 15 | +# use the CPX onboard switch to turn on/off (helps calibrate) |
| 16 | +switch = DigitalInOut(board.SLIDE_SWITCH) |
| 17 | +switch.direction = Direction.INPUT |
| 18 | +switch.pull = Pull.UP |
| 19 | + |
| 20 | +#################### Audio setup |
| 21 | +print("Let's count in binary.") |
| 22 | +wavfiles = ("one.wav", "two.wav", "three.wav", "four.wav", "five.wav", "six.wav", |
| 23 | + "seven.wav", "eight.wav", "nine.wav", "ten.wav", "eleven.wav", |
| 24 | + "twelve.wav", "thirteen.wav", "fourteen.wav", "fifteen.wav") |
| 25 | +introfile = "intro.wav" |
| 26 | + |
| 27 | +cpx_audio = audioio.AudioOut(board.A0) |
| 28 | +def play_file(wavfile): |
| 29 | + with open(wavfile, "rb") as f: |
| 30 | + wav = audiocore.WaveFile(f) |
| 31 | + cpx_audio.play(wav) |
| 32 | + while cpx_audio.playing: |
| 33 | + pass |
| 34 | + |
| 35 | +#################### 4 Servos! |
| 36 | +servos = (crickit.servo_1, crickit.servo_2, crickit.servo_3, crickit.servo_4) |
| 37 | +for servo in servos: |
| 38 | + servo.angle = 180 # starting angle, open hand |
| 39 | + |
| 40 | +# Which servos to actuate for each number |
| 41 | +counting = ( |
| 42 | + [3], |
| 43 | + [2], |
| 44 | + [3, 2], |
| 45 | + [1], |
| 46 | + [1, 3], |
| 47 | + [1, 2], |
| 48 | + [3, 2, 1], |
| 49 | + [0], |
| 50 | + [0, 3], |
| 51 | + [0, 2], |
| 52 | + [0, 3, 2], |
| 53 | + [0, 1], |
| 54 | + [0, 3, 1], |
| 55 | + [0, 2, 1], |
| 56 | + [0, 3, 2, 1] |
| 57 | +) |
| 58 | + |
| 59 | +play_file(introfile) |
| 60 | + |
| 61 | +while True: |
| 62 | + if not switch.value: |
| 63 | + continue |
| 64 | + |
| 65 | + # the CPX switch is on, so do things |
| 66 | + for servo in servos: # close the fist |
| 67 | + servo.angle = 0 # close the fingers |
| 68 | + print("Servo %d angle = 0" % (servos.index(servo)+1) ) |
| 69 | + time.sleep(.2) |
| 70 | + |
| 71 | + time.sleep(1) # pause a moment |
| 72 | + |
| 73 | + for i in range(len(counting)): |
| 74 | + # close all the counting fingers between numbers |
| 75 | + for servo in servos: |
| 76 | + servo.angle = 0 # close |
| 77 | + print("\t\tServo #%d angle 0" % (servos.index(servo)+1)) |
| 78 | + time.sleep(0.3) |
| 79 | + |
| 80 | + print("Number #%d \tfingers: %s" % (i+1, counting[i])) |
| 81 | + |
| 82 | + # open just the indicated fingers when counting |
| 83 | + for finger in counting[i]: |
| 84 | + servos[finger].angle = 180 # open |
| 85 | + print("\t\tServo #%d angle 180" % (finger+1)) |
| 86 | + time.sleep(0.3) |
| 87 | + # say it! |
| 88 | + play_file(wavfiles[i]) |
| 89 | + # hold for a bit of time |
| 90 | + time.sleep(0.3) |
| 91 | + print("...") |
0 commit comments