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.
You'll need a few CircuitPython libraries in the lib folder on the Feather CIRCUITPY drive for the code to work. Head to https://circuitpython.org/libraries to download the latest library bundle matching the major version of CircuitPython now on your board (6 for CircuitPython 6.x, etc.).
Once you've downloaded the libraries bundle, add these libraries to the lib folder on the Feather:
- adafruit_bus_device
- adafruit_monitor
- adafruit_pca9685.mpy
- adafruit_register
- adafruit_servokit.mpy
Your Feather M4 Express CIRCUITPY drive should look like this after you load the code below.:
Once your Feather M4 Express is all setup with CircuitPython and the necessary libraries, you can click on the Download: Project Zip link above in the code to get the file.
# 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.