/ Fluttering_Fairy_Wings / code.py
code.py
1 # SPDX-FileCopyrightText: 2021 Erin St Blaine for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """ 6 Fluttering Fairy Wings 7 Adafruit invests time and resources providing this open source code. 8 Please support Adafruit and open source hardware by purchasing 9 products from Adafruit! 10 Written by Erin St Blaine for Adafruit Industries 11 Copyright (c) 2020-2021 Adafruit Industries 12 Licensed under the MIT license. 13 All text above must be included in any redistribution. 14 """ 15 16 import time 17 import random 18 import board 19 from analogio import AnalogIn 20 from adafruit_servokit import ServoKit 21 22 analog_in = AnalogIn(board.A0) 23 kit = ServoKit(channels=8) 24 25 SERVO_MIN = 0 26 SERVO_MAX = 130 27 DELAY_MIN = 0.01 # In seconds, is the longest DELAY between servo moves 28 DELAY_MAX = 0.1 # In seconds, is the longest DELAY between servo moves 29 30 DELAY = DELAY_MAX 31 32 def set_delay(): 33 '''calibrate to potentiometer''' 34 global DELAY # pylint: disable=global-statement 35 DELAY = DELAY_MIN + DELAY_MAX * (65535 - analog_in.value) / 65535 36 #print(DELAY\ 37 38 while True: 39 num_flaps = random.randint(1, 4) 40 41 print("Flapping", num_flaps, "times") 42 for flap in range(num_flaps): 43 print("Open") 44 set_delay() 45 for angle in range(SERVO_MIN, SERVO_MAX, 2): # move 2 deg at a time 46 kit.servo[0].angle = angle 47 kit.servo[1].angle = SERVO_MAX-angle 48 time.sleep(DELAY) 49 print("Close") 50 set_delay() 51 for angle in range(SERVO_MIN, SERVO_MAX, 2): # move 2 deg at a time 52 kit.servo[0].angle = SERVO_MAX-angle 53 kit.servo[1].angle = angle 54 time.sleep(DELAY*2) 55 print("Waiting...") 56 time.sleep(random.randint(2, 10)) # wait 2 to 10 seconds