|
| 1 | +# SPDX-FileCopyrightText: 2018 Dano Wall for Adafruit Industries |
| 2 | +# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +# Stumble Bot, coded in CircuitPython |
| 7 | +# Using an Adafruit Circuit Playground Express, Crickit, and 2 servos |
| 8 | +# Dano Wall, Anne Barela for Adafruit Industries, MIT License, May, 2018 |
| 9 | +# |
| 10 | +import time |
| 11 | +from digitalio import DigitalInOut, Direction, Pull |
| 12 | +from adafruit_seesaw.seesaw import Seesaw |
| 13 | +from adafruit_seesaw.pwmout import PWMOut |
| 14 | + |
| 15 | +from adafruit_motor import servo |
| 16 | +from busio import I2C |
| 17 | +import board |
| 18 | + |
| 19 | +# Create seesaw object for Circuit Playground Express to talk to Crickit |
| 20 | +i2c = I2C(board.SCL, board.SDA) |
| 21 | +seesaw = Seesaw(i2c) |
| 22 | + |
| 23 | +led = DigitalInOut(board.D13) # Set up Red LED |
| 24 | +led.direction = Direction.OUTPUT |
| 25 | + |
| 26 | +button_A = DigitalInOut(board.BUTTON_A) # Set up switch A |
| 27 | +button_A.direction = Direction.INPUT |
| 28 | +button_A.pull = Pull.DOWN |
| 29 | + |
| 30 | +# Create servos list |
| 31 | +servos = [] |
| 32 | +for ss_pin in (17, 16): # Only use 2 servos, append , 15, 14 if using 4 |
| 33 | + pwm = PWMOut(seesaw, ss_pin) |
| 34 | + pwm.frequency = 50 |
| 35 | + _servo = servo.Servo(pwm, min_pulse=600, max_pulse=2500) |
| 36 | + _servo.angle = 90 # starting angle, middle |
| 37 | + servos.append(_servo) |
| 38 | + |
| 39 | +def servo_front(direction): |
| 40 | + if direction > 0: |
| 41 | + index = 50 |
| 42 | + while index <= 90: |
| 43 | + servos[1].angle = index |
| 44 | + time.sleep(0.040) |
| 45 | + index = index + 1 |
| 46 | + if direction < 0: |
| 47 | + index = 90 |
| 48 | + while index >= 50: |
| 49 | + servos[1].angle = index |
| 50 | + time.sleep(0.040) |
| 51 | + index = index - 1 |
| 52 | + time.sleep(0.002) |
| 53 | + |
| 54 | +def servo_back(direction): |
| 55 | + if direction > 0: |
| 56 | + index = 65 |
| 57 | + while index <= 95: |
| 58 | + servos[0].angle = index |
| 59 | + time.sleep(0.040) |
| 60 | + index = index + 3 |
| 61 | + if direction < 0: |
| 62 | + index = 95 |
| 63 | + while index >= 65: |
| 64 | + servos[0].angle = index |
| 65 | + time.sleep(0.040) |
| 66 | + index = index - 3 |
| 67 | + time.sleep(0.020) |
| 68 | + |
| 69 | +print("Its Stumble Bot Time") |
| 70 | + |
| 71 | +while True: |
| 72 | + if button_A.value: # If button A is pressed, start bot |
| 73 | + led.value = True # Turn on LED 13 to show we're gone! |
| 74 | + for i in range(5): |
| 75 | + print("back 1") |
| 76 | + servo_back(1) |
| 77 | + time.sleep(0.100) |
| 78 | + print("front 1") |
| 79 | + servo_front(1) |
| 80 | + time.sleep(0.100) |
| 81 | + print("back 2") |
| 82 | + servo_back(-1) |
| 83 | + time.sleep(0.100) |
| 84 | + print("front 2") |
| 85 | + servo_front(-1) |
| 86 | + time.sleep(0.100) |
| 87 | + led.value = False |
| 88 | + # end if |
| 89 | +# end while |
0 commit comments