/ Crickits / mag_neat_o / code.py
code.py
  1  # SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  import time
  6  from busio import I2C
  7  from adafruit_seesaw.seesaw import Seesaw
  8  from adafruit_seesaw.pwmout import PWMOut
  9  from adafruit_motor import motor, servo
 10  from digitalio import DigitalInOut, Direction, Pull
 11  import board
 12  
 13  print("Mag Neat-o!")
 14  
 15  # Create seesaw object
 16  i2c = I2C(board.SCL, board.SDA)
 17  seesaw = Seesaw(i2c)
 18  
 19  # Create one motor on seesaw PWM pins 22 & 23
 20  motor_a = motor.DCMotor(PWMOut(seesaw, 22), PWMOut(seesaw, 23))
 21  # Create another motor on seesaw PWM pins 19 & 18
 22  motor_b = motor.DCMotor(PWMOut(seesaw, 19), PWMOut(seesaw, 18))
 23  
 24  # Create servo object
 25  pwm = PWMOut(seesaw, 17)     # Servo 1 is on s.s. pin 17
 26  pwm.frequency = 50           # Servos like 50 Hz signals
 27  my_servo = servo.Servo(pwm)  # Create my_servo with pwm signa
 28  my_servo.angle = 90
 29  
 30  def smooth_move(start, stop, num_steps):
 31      return [(start + (stop-start)*i/num_steps) for i in range(num_steps)]
 32  
 33  buttona = DigitalInOut(board.BUTTON_A)
 34  buttona.direction = Direction.INPUT
 35  buttona.pull = Pull.DOWN
 36  
 37  buttonb = DigitalInOut(board.BUTTON_B)
 38  buttonb.direction = Direction.INPUT
 39  buttonb.pull = Pull.DOWN
 40  
 41  switch = DigitalInOut(board.SLIDE_SWITCH)
 42  switch.direction = Direction.INPUT
 43  switch.pull = Pull.UP
 44  
 45  last_buttona = buttona.value
 46  last_buttonb = buttonb.value
 47  last_switch = switch.value
 48  
 49  last_touch1 = False
 50  last_touch4 = False
 51  
 52  
 53  while True:
 54      touch_vals = (seesaw.touch_read(0), seesaw.touch_read(3))
 55      # print(touch_vals)
 56  
 57      touch1 = False
 58      if seesaw.touch_read(0) > 500:
 59          touch1 = True
 60  
 61      if touch1 != last_touch1:
 62          if touch1:
 63              start_angle = my_servo.angle
 64              end_angle = start_angle - 20
 65              end_angle = max(0, min(end_angle, 180))
 66              print("left from", start_angle, "to", end_angle)
 67              for a in smooth_move(start_angle, end_angle, 25):
 68                  my_servo.angle = a
 69                  time.sleep(0.03)
 70          last_touch1 = touch1
 71  
 72      touch4 = False
 73      if seesaw.touch_read(3) > 500:
 74          touch4 = True
 75  
 76      if touch4 != last_touch4:
 77          if touch4:
 78              start_angle = my_servo.angle
 79              end_angle = start_angle + 20
 80              end_angle = max(0, min(end_angle, 180))
 81              print("right from", start_angle, "to", end_angle)
 82              for a in smooth_move(start_angle, end_angle, 25):
 83                  my_servo.angle = a
 84                  time.sleep(0.03)
 85          last_touch4 = touch4
 86  
 87      if buttona.value != last_buttona:
 88          if buttona.value:
 89              print("down")
 90              if motor_a.throttle:
 91                  print("haulin!")
 92                  motor_b.throttle = -0.1
 93              else:
 94                  motor_b.throttle = -0.1
 95          else:
 96              motor_b.throttle = 0
 97          last_buttona = buttona.value
 98  
 99      if buttonb.value != last_buttonb:
100          if buttonb.value:
101              print("up")
102              if motor_a.throttle:
103                  print("haulin!")
104                  motor_b.throttle = 0.4
105              else:
106                  motor_b.throttle = 0.3
107          else:
108              motor_b.throttle = 0
109          last_buttonb = buttonb.value
110  
111      if switch.value != last_switch:
112          motor_a.throttle = switch.value
113      if motor_a.throttle:
114          print("GRAB")
115      else:
116          print("RELEASE")
117          last_switch = switch.value
118  
119      time.sleep(0.01)