From a6b2ad74b9e980669d9f2b6b6cbfecfb4f8814c5 Mon Sep 17 00:00:00 2001 From: ladyada Date: Sun, 8 Jul 2018 23:01:52 -0400 Subject: [PATCH] final project to crickitfy --- Animatronic_Hand/binary_counting.py | 47 +++++++++++------------------ Animatronic_Hand/touch.py | 33 ++++++++------------ 2 files changed, 30 insertions(+), 50 deletions(-) diff --git a/Animatronic_Hand/binary_counting.py b/Animatronic_Hand/binary_counting.py index 71808300..72865fdb 100644 --- a/Animatronic_Hand/binary_counting.py +++ b/Animatronic_Hand/binary_counting.py @@ -3,15 +3,8 @@ import time from digitalio import DigitalInOut, Direction, Pull import audioio -from adafruit_seesaw.seesaw import Seesaw -from adafruit_seesaw.pwmout import PWMOut -from adafruit_motor import servo -from busio import I2C import board - -# Create I2C and seesaw objuect -i2c = I2C(board.SCL, board.SDA) -ss = Seesaw(i2c) +from adafruit_crickit import crickit #################### CPX switch # use the CPX onboard switch to turn on/off (helps calibrate) @@ -21,9 +14,9 @@ switch.pull = Pull.UP #################### Audio setup print("Let's count in binary.") -wavfiles = ["one.wav", "two.wav", "three.wav", "four.wav", "five.wav", "six.wav", +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"] + "twelve.wav", "thirteen.wav", "fourteen.wav", "fifteen.wav") introfile = "intro.wav" cpx_audio = audioio.AudioOut(board.A0) @@ -34,17 +27,13 @@ def play_file(wavfile): while cpx_audio.playing: pass -#################### 4 Servos -servos = [] -for ss_pin in (17, 16, 15, 14): - pwm = PWMOut(ss, ss_pin) - pwm.frequency = 50 - _servo = servo.Servo(pwm) - _servo.angle = 90 # starting angle, middle - servos.append(_servo) +#################### 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 = [ +counting = ( [3], [2], [3, 2], @@ -60,7 +49,7 @@ counting = [ [0, 3, 1], [0, 2, 1], [0, 3, 2, 1] -] +) play_file(introfile) @@ -69,26 +58,26 @@ while True: continue # the CPX switch is on, so do things - for i in range(4): # close the fist - servos[i].angle = 0 # close the fingers - print("Servo %s angle = 0" % i ) + 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 for i in range(len(counting)): # close all the counting fingers between numbers - for k in range(4): - servos[k].angle = 0 # close - print("\t\tServo #%d angle 0" % k) + 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 j in range(len(counting[i])): - servos[counting[i][j]].angle = 180 # open - print("\t\tServo #%d angle 180" % counting[i][j]) + 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]) diff --git a/Animatronic_Hand/touch.py b/Animatronic_Hand/touch.py index 61ac960f..76b74591 100644 --- a/Animatronic_Hand/touch.py +++ b/Animatronic_Hand/touch.py @@ -2,15 +2,9 @@ # CPX with CRICKIT and four servos # touch four cap pads to close the fingers -from digitalio import DigitalInOut, Direction, Pull -from adafruit_seesaw.seesaw import Seesaw -from adafruit_seesaw.pwmout import PWMOut -from adafruit_motor import servo -from busio import I2C import board - -i2c = I2C(board.SCL, board.SDA) -ss = Seesaw(i2c) +from digitalio import DigitalInOut, Direction, Pull +from adafruit_crickit import crickit #################### CPX switch # use the CPX onboard switch to turn on/off (helps calibrate) @@ -18,16 +12,13 @@ switch = DigitalInOut(board.SLIDE_SWITCH) switch.direction = Direction.INPUT switch.pull = Pull.UP -#################### 4 Servos -servos = [] -for ss_pin in (17, 16, 15, 14): - pwm = PWMOut(ss, ss_pin) - pwm.frequency = 50 - _servo = servo.Servo(pwm) - _servo.angle = 90 # starting angle, middle - servos.append(_servo) +#################### 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 -CAPTOUCH_THRESH = 500 # threshold for touch detection +#################### 4 Touch sensors! +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] @@ -41,16 +32,16 @@ while True: continue # Check the cap touch sensors to see if they're being touched for i in range(4): - touch_val = ss.touch_read(i) cap_justtouched[i] = False cap_justreleased[i] = False - if touch_val > CAPTOUCH_THRESH: - # print("CT" + str(i + 1) + " touched! value: " + str(touch_val)) + 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: @@ -59,7 +50,7 @@ while 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]: