|
| 1 | +# CircuitPython 3.0 CRICKIT demo |
| 2 | + |
| 3 | +from adafruit_seesaw.seesaw import Seesaw |
| 4 | +from adafruit_seesaw.pwmout import PWMOut |
| 5 | +from adafruit_motor import servo, motor |
| 6 | +import audioio |
| 7 | +from busio import I2C |
| 8 | +import random |
| 9 | +import board |
| 10 | +import time |
| 11 | +import gc |
| 12 | + |
| 13 | +i2c = I2C(board.SCL, board.SDA) |
| 14 | +ss = Seesaw(i2c) |
| 15 | + |
| 16 | +print("Feynbot demo!") |
| 17 | + |
| 18 | +#################### 1 Servo |
| 19 | +pwm = PWMOut(ss, 17) |
| 20 | +pwm.frequency = 50 |
| 21 | +myservo = servo.Servo(pwm) |
| 22 | +myservo.angle = 180 # starting angle, highest |
| 23 | + |
| 24 | +#################### 2 Drivers |
| 25 | +drives = [] |
| 26 | +for ss_pin in (13, 12): |
| 27 | + _pwm = PWMOut(ss, ss_pin) |
| 28 | + _pwm.frequency = 1000 |
| 29 | + drives.append(_pwm) |
| 30 | + |
| 31 | +#################### Audio files |
| 32 | +wavfiles = ["01.wav", "02.wav", "03.wav", "04.wav", "05.wav"] |
| 33 | +a = audioio.AudioOut(board.A0) |
| 34 | + |
| 35 | +# Start playing the file (in the background) |
| 36 | +def play_file(wavfile): |
| 37 | + f = open(wavfile, "rb") |
| 38 | + wav = audioio.WaveFile(f) |
| 39 | + a.play(wav) |
| 40 | + |
| 41 | +# Tap the solenoids back and forth |
| 42 | +def bongo(t): |
| 43 | + for _ in range(t): |
| 44 | + drives[0].duty_cycle = 0xFFFF |
| 45 | + time.sleep(0.1) |
| 46 | + drives[0].duty_cycle = 0 |
| 47 | + time.sleep(0.1) |
| 48 | + drives[1].duty_cycle = 0xFFFF |
| 49 | + time.sleep(0.1) |
| 50 | + drives[1].duty_cycle = 0 |
| 51 | + time.sleep(0.1) |
| 52 | + |
| 53 | +# Move mouth back and forth |
| 54 | +def talk(t): |
| 55 | + for _ in range(t): |
| 56 | + myservo.angle = 150 |
| 57 | + time.sleep(0.1) |
| 58 | + myservo.angle = 180 |
| 59 | + time.sleep(0.1) |
| 60 | + |
| 61 | + |
| 62 | +filenum = 0 # counter to play all files |
| 63 | + |
| 64 | +while True: |
| 65 | + gc.collect() |
| 66 | + print(gc.mem_free()) |
| 67 | + |
| 68 | + # time to play the bongos! |
| 69 | + bongo(5) |
| 70 | + time.sleep(1) |
| 71 | + |
| 72 | + # OK say something insightful |
| 73 | + play_file(wavfiles[filenum]) |
| 74 | + # and move the mouth while it does |
| 75 | + while a.playing: |
| 76 | + talk(1) |
| 77 | + |
| 78 | + # Done being insightful, take a break |
| 79 | + time.sleep(1) |
| 80 | + |
| 81 | + # If we went thru all the files, JAM OUT! |
| 82 | + filenum += 1 |
| 83 | + if filenum >= len(wavfiles): |
| 84 | + bongo(20) |
| 85 | + filenum = 0 |
0 commit comments