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