|
| 1 | +# CircuitPython 3.0 CRICKIT demo |
| 2 | +from digitalio import DigitalInOut, Direction, Pull |
| 3 | +from adafruit_seesaw.seesaw import Seesaw |
| 4 | +from adafruit_seesaw.pwmout import PWMOut |
| 5 | +from adafruit_motor import servo, motor |
| 6 | +from busio import I2C |
| 7 | +import board |
| 8 | +import time |
| 9 | +import gc |
| 10 | + |
| 11 | +i2c = I2C(board.SCL, board.SDA) |
| 12 | +ss = Seesaw(i2c) |
| 13 | + |
| 14 | +print("Crickit demo!") |
| 15 | + |
| 16 | +# use the CPX onboard switch to turn on/off (helps calibrate) |
| 17 | +switch = DigitalInOut(board.SLIDE_SWITCH) |
| 18 | +switch.direction = Direction.INPUT |
| 19 | +switch.pull = Pull.UP |
| 20 | + |
| 21 | +#################### 4 Servos |
| 22 | +servos = [] |
| 23 | +for ss_pin in (17, 16, 15, 14): |
| 24 | + pwm = PWMOut(ss, ss_pin) |
| 25 | + pwm.frequency = 50 |
| 26 | + _servo = servo.Servo(pwm) |
| 27 | + _servo.angle = 90 # starting angle, middle |
| 28 | + servos.append(_servo) |
| 29 | + |
| 30 | +#################### 2 DC motors |
| 31 | +motors = [] |
| 32 | +for ss_pin in ((22, 23), (18, 19)): |
| 33 | + pwm0 = PWMOut(ss, ss_pin[0]) |
| 34 | + pwm1 = PWMOut(ss, ss_pin[1]) |
| 35 | + _motor = motor.DCMotor(pwm0, pwm1) |
| 36 | + motors.append(_motor) |
| 37 | + |
| 38 | +servos[0].angle = 180 |
| 39 | + |
| 40 | +while True: |
| 41 | + if switch.value: |
| 42 | + # Switch is on, activate MUSIC POWER! |
| 43 | + |
| 44 | + # motor forward slowly |
| 45 | + motors[0].throttle = 0.2 |
| 46 | + # mote the head forward slowly, over 0.9 seconds |
| 47 | + for a in range(180, 90, -1): |
| 48 | + servos[0].angle = a |
| 49 | + time.sleep(0.01) |
| 50 | + |
| 51 | + # motor stop |
| 52 | + motors[0].throttle = 0 |
| 53 | + time.sleep(1) |
| 54 | + |
| 55 | + # motor backwards slowly |
| 56 | + motors[0].throttle = -0.2 |
| 57 | + # move the head back slowly too, over 0.9 seconds |
| 58 | + for a in range(90, 180): |
| 59 | + servos[0].angle = a |
| 60 | + time.sleep(0.01) |
| 61 | + # calibration! its a *tiny* bit slower going back so give it a few ms |
| 62 | + time.sleep(0.007) |
| 63 | + |
| 64 | + # motor stop |
| 65 | + motors[0].throttle = 0 |
| 66 | + time.sleep(1) |
| 67 | + else: |
| 68 | + # switch is 'off' so dont do anything! |
| 69 | + pass |
| 70 | + |
0 commit comments