final project to crickitfy

This commit is contained in:
ladyada 2018-07-08 23:01:52 -04:00
parent e48eb89fc5
commit a6b2ad74b9
2 changed files with 30 additions and 50 deletions

View file

@ -3,15 +3,8 @@
import time import time
from digitalio import DigitalInOut, Direction, Pull from digitalio import DigitalInOut, Direction, Pull
import audioio 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 import board
from adafruit_crickit import crickit
# Create I2C and seesaw objuect
i2c = I2C(board.SCL, board.SDA)
ss = Seesaw(i2c)
#################### CPX switch #################### CPX switch
# use the CPX onboard switch to turn on/off (helps calibrate) # use the CPX onboard switch to turn on/off (helps calibrate)
@ -21,9 +14,9 @@ switch.pull = Pull.UP
#################### Audio setup #################### Audio setup
print("Let's count in binary.") 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", "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" introfile = "intro.wav"
cpx_audio = audioio.AudioOut(board.A0) cpx_audio = audioio.AudioOut(board.A0)
@ -34,17 +27,13 @@ def play_file(wavfile):
while cpx_audio.playing: while cpx_audio.playing:
pass pass
#################### 4 Servos #################### 4 Servos!
servos = [] servos = (crickit.servo_1, crickit.servo_2, crickit.servo_3, crickit.servo_4)
for ss_pin in (17, 16, 15, 14): for servo in servos:
pwm = PWMOut(ss, ss_pin) servo.angle = 180 # starting angle, open hand
pwm.frequency = 50
_servo = servo.Servo(pwm)
_servo.angle = 90 # starting angle, middle
servos.append(_servo)
# Which servos to actuate for each number # Which servos to actuate for each number
counting = [ counting = (
[3], [3],
[2], [2],
[3, 2], [3, 2],
@ -60,7 +49,7 @@ counting = [
[0, 3, 1], [0, 3, 1],
[0, 2, 1], [0, 2, 1],
[0, 3, 2, 1] [0, 3, 2, 1]
] )
play_file(introfile) play_file(introfile)
@ -69,26 +58,26 @@ while True:
continue continue
# the CPX switch is on, so do things # the CPX switch is on, so do things
for i in range(4): # close the fist for servo in servos: # close the fist
servos[i].angle = 0 # close the fingers servo.angle = 0 # close the fingers
print("Servo %s angle = 0" % i ) print("Servo %d angle = 0" % (servos.index(servo)+1) )
time.sleep(.2) time.sleep(.2)
time.sleep(1) # pause a moment time.sleep(1) # pause a moment
for i in range(len(counting)): for i in range(len(counting)):
# close all the counting fingers between numbers # close all the counting fingers between numbers
for k in range(4): for servo in servos:
servos[k].angle = 0 # close servo.angle = 0 # close
print("\t\tServo #%d angle 0" % k) print("\t\tServo #%d angle 0" % (servos.index(servo)+1))
time.sleep(0.3) time.sleep(0.3)
print("Number #%d \tfingers: %s" % (i+1, counting[i])) print("Number #%d \tfingers: %s" % (i+1, counting[i]))
# open just the indicated fingers when counting # open just the indicated fingers when counting
for j in range(len(counting[i])): for finger in counting[i]:
servos[counting[i][j]].angle = 180 # open servos[finger].angle = 180 # open
print("\t\tServo #%d angle 180" % counting[i][j]) print("\t\tServo #%d angle 180" % (finger+1))
time.sleep(0.3) time.sleep(0.3)
# say it! # say it!
play_file(wavfiles[i]) play_file(wavfiles[i])

View file

@ -2,15 +2,9 @@
# CPX with CRICKIT and four servos # CPX with CRICKIT and four servos
# touch four cap pads to close the fingers # 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 import board
from digitalio import DigitalInOut, Direction, Pull
i2c = I2C(board.SCL, board.SDA) from adafruit_crickit import crickit
ss = Seesaw(i2c)
#################### CPX switch #################### CPX switch
# use the CPX onboard switch to turn on/off (helps calibrate) # 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.direction = Direction.INPUT
switch.pull = Pull.UP switch.pull = Pull.UP
#################### 4 Servos #################### 4 Servos!
servos = [] servos = (crickit.servo_1, crickit.servo_2, crickit.servo_3, crickit.servo_4)
for ss_pin in (17, 16, 15, 14): for servo in servos:
pwm = PWMOut(ss, ss_pin) servo.angle = 180 # starting angle, open hand
pwm.frequency = 50
_servo = servo.Servo(pwm)
_servo.angle = 90 # starting angle, middle
servos.append(_servo)
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_state = [False, False, False, False]
cap_justtouched = [False, False, False, False] cap_justtouched = [False, False, False, False]
@ -41,16 +32,16 @@ while True:
continue continue
# Check the cap touch sensors to see if they're being touched # Check the cap touch sensors to see if they're being touched
for i in range(4): for i in range(4):
touch_val = ss.touch_read(i)
cap_justtouched[i] = False cap_justtouched[i] = False
cap_justreleased[i] = False cap_justreleased[i] = False
if touch_val > CAPTOUCH_THRESH: if touches[i].value:
# print("CT" + str(i + 1) + " touched! value: " + str(touch_val)) #print("CT" + str(i + 1) + " touched!")
if not cap_state[i]: if not cap_state[i]:
cap_justtouched[i] = True cap_justtouched[i] = True
print("%s finger bent." % finger_name[i]) print("%s finger bent." % finger_name[i])
servos[i].angle = 0 servos[i].angle = 0
# store the fact that this pad is touched
cap_state[i] = True cap_state[i] = True
else: else:
@ -59,7 +50,7 @@ while True:
print("%s finger straightened." % finger_name[i]) print("%s finger straightened." % finger_name[i])
servos[i].angle = 180 servos[i].angle = 180
# print("CT" + str(i + 1) + " released!") # print("CT" + str(i + 1) + " released!")
# store the fact that this pad is NOT touched
cap_state[i] = False cap_state[i] = False
if cap_justtouched[i]: if cap_justtouched[i]: