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