|
| 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 | +import board |
| 12 | +from digitalio import DigitalInOut, Direction, Pull |
| 13 | +from adafruit_crickit import crickit |
| 14 | + |
| 15 | +led = DigitalInOut(board.D13) # Set up Red LED |
| 16 | +led.direction = Direction.OUTPUT |
| 17 | + |
| 18 | +button_A = DigitalInOut(board.BUTTON_A) # Set up switch A |
| 19 | +button_A.direction = Direction.INPUT |
| 20 | +button_A.pull = Pull.DOWN |
| 21 | + |
| 22 | +# Create servos list |
| 23 | +servos = [crickit.servo_1, crickit.servo_2] |
| 24 | + |
| 25 | +# TowerPro servos like 500/2500 pulsewidths |
| 26 | +servos[0].set_pulse_width_range(min_pulse=500, max_pulse=2500) |
| 27 | +servos[1].set_pulse_width_range(min_pulse=500, max_pulse=2500) |
| 28 | + |
| 29 | +# starting angle, middle |
| 30 | +servos[1].angle = 90 |
| 31 | +servos[0].angle = 90 |
| 32 | + |
| 33 | +def servo_front(direction): |
| 34 | + if direction > 0: |
| 35 | + index = 50 |
| 36 | + while index <= 100: |
| 37 | + servos[1].angle = index |
| 38 | + time.sleep(0.040) |
| 39 | + index = index + 2 |
| 40 | + if direction < 0: |
| 41 | + index = 100 |
| 42 | + while index >= 50: |
| 43 | + servos[1].angle = index |
| 44 | + time.sleep(0.040) |
| 45 | + index = index - 2 |
| 46 | + time.sleep(0.002) |
| 47 | + |
| 48 | +def servo_back(direction): |
| 49 | + if direction > 0: |
| 50 | + index = 60 |
| 51 | + while index <= 90: |
| 52 | + servos[0].angle = index |
| 53 | + time.sleep(0.040) |
| 54 | + index = index + 4 |
| 55 | + if direction < 0: |
| 56 | + index = 100 |
| 57 | + while index >= 50: |
| 58 | + servos[0].angle = index |
| 59 | + time.sleep(0.040) |
| 60 | + index = index - 4 |
| 61 | + time.sleep(0.020) |
| 62 | + |
| 63 | +print("Its Stumble Bot Time") |
| 64 | + |
| 65 | +while True: |
| 66 | + if button_A.value: # If button A is pressed, start bot |
| 67 | + led.value = True # Turn on LED 13 to show we're gone! |
| 68 | + for i in range(5): |
| 69 | + print("back 1") |
| 70 | + servo_back(1) |
| 71 | + time.sleep(0.100) |
| 72 | + print("front 1") |
| 73 | + servo_front(1) |
| 74 | + time.sleep(0.100) |
| 75 | + print("back 2") |
| 76 | + servo_back(-1) |
| 77 | + time.sleep(0.100) |
| 78 | + print("front 2") |
| 79 | + servo_front(-1) |
| 80 | + time.sleep(0.100) |
| 81 | + led.value = False |
| 82 | + # end if |
| 83 | +# end while |
0 commit comments