Take your Feather and plug it into your computer via a known good data + power USB cable. Have your Feather handy as you'll be performing most of the same steps for each. Your operating system will show a drive named CIRCUITPY when a board is plugged in. If you get a drive named FEATHERBOOT you'll likely need to install CircuitPython.

Installing Project Code

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.

Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the directory Fluttering_Fairy_Wings/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.

Your CIRCUITPY drive should now look similar to the following image:

CIRCUITPY
# 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

Calibrating Your Wings

Servos and potentiometers have some variation between them. It's easy to adjust the code so the wings move only as far as you'd like them to move. For my wings, the optimum range was between 0-130 degrees. 

If yours are not flapping far enough, or if they're hitting the end of your servo range, look for this line in the code:

SERVO_MIN = 0
SERVO_MAX = 130

Adjust the max and min values until your wings are in the range you want.

Potentiometers also have some variation. If your wings are moving too slowly at the "slow" end of your range, or too quickly at the "fast" end, you can set the max and min speeds in the code as well. Look for this line:

DELAY_MIN = 0.01  # In seconds, is the shortest DELAY between servo moves
DELAY_MAX = 0.1   # In seconds, is the longest DELAY between servo moves

Adjust the min and max delay until you're happy with your wing motion speed.

You can also adjust the number of flaps and the time between flaps. 

num_flaps = random.randint(1, 4)

This line chooses a random interger between 1 and 4 and sets the wings to flap that many times. You can change the intergers to anything you'd like to make the wings flappier or stiller.

time.sleep(random.randint(2, 10))  # wait 2 to 10 seconds

The last line in the code chooses a random number of seconds to wait between flapping cycles. Change this to make the pauses shorter or longer.

This guide was first published on Feb 24, 2021. It was last updated on Jun 15, 2024.

This page (Code) was last updated on Jun 15, 2024.

Text editor powered by tinymce.