/ 3D_Printed_Bionic_Eye / code.py
code.py
1 # SPDX-FileCopyrightText: 2018 Bill Earl and Mikey Sklar for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 # 5 # Bionic Eye sketch for Adafruit Trinket. 6 # 7 # written by Bill Earl for Arduino 8 # ported to CircuitPython by Mikey Sklar 9 # for Adafruit Industries 10 # 11 # Required library is the Adafruit_SoftServo library 12 # available at https://github.com/adafruit/Adafruit_SoftServo 13 # The standard Arduino IDE servo library will not work with 8 bit 14 # AVR microcontrollers like Trinket and Gemma due to differences 15 # in available timer hardware and programming. We simply refresh 16 # by piggy-backing on the timer0 millis() counter 17 # 18 # Trinket: Bat+ Gnd Pin #0 Pin #2 19 # Connection: Servo+ Servo- Tilt Rotate 20 # (Red) (Black) Servo Servo 21 # (Orange)(Orange) 22 23 import time 24 import random 25 import board 26 import pwmio 27 from adafruit_motor import servo 28 29 # we are intentionally avoiding Trinket Pin #1 (board.A0) 30 # as it does not have PWM capability 31 tilt_servo_pin = board.A2 # servo control line (orange) Trinket Pin #0 32 rotate_servo_pin = board.A1 # servo control line (orange) Trinket Pin #2 33 34 # servo object setup for the M0 boards: 35 tilt_pwm = pwmio.PWMOut(tilt_servo_pin, duty_cycle=2 ** 15, frequency=50) 36 rotate_pwm = pwmio.PWMOut(rotate_servo_pin, duty_cycle=2 ** 15, frequency=50) 37 tilt_servo = servo.Servo(tilt_pwm) 38 rotate_servo = servo.Servo(rotate_pwm) 39 40 # servo timing and angle range 41 tilt_min = 120 # lower limit to tilt rotation range 42 max_rotate = 180 # rotation range limited to half circle 43 44 while True: 45 46 # servo tilt - on average move every 500ms 47 if random.randint(0,100) > 80: 48 tilt_servo.angle = random.randint(tilt_min, max_rotate) 49 time.sleep(.25) 50 51 # servo rotate - on average move every 500ms 52 if random.randint(0,100) > 90: 53 rotate_servo.angle = random.randint(0, max_rotate) 54 time.sleep(.25)