Once you've finished setting up your Feather M4 Express with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.
To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries # SPDX-License-Identifier: MIT from adafruit_crickit import crickit # crickit setup ss = crickit.seesaw # pin for photo interrupter photo = crickit.SIGNAL1 ss.pin_mode(photo, ss.INPUT_PULLUP) # dc motor setup motor = crickit.dc_motor_1 # party parrot colors for the NeoPixel parrot_0 = (255, 75, 0) parrot_1 = (255, 200, 0) parrot_2 = (90, 255, 90) parrot_3 = (0, 255, 255) parrot_4 = (0, 160, 255) parrot_5 = (90, 0, 255) parrot_6 = (175, 0, 255) parrot_7 = (255, 0, 200) parrot_8 = (255, 0, 125) parrot_9 = (255, 0, 0) colors = (parrot_0, parrot_1, parrot_2, parrot_3, parrot_4, parrot_5, parrot_6, parrot_7, parrot_8, parrot_9) # setup using crickit neopixel library crickit.init_neopixel(1) crickit.neopixel.fill((parrot_0)) # counter for party parrot colors z = 0 # speed for the dc motor speed = 0.3 while True: # begin the dc motor # will run throughout the loop motor.throttle = speed # read the input from the photo interrupter data = ss.digital_read(photo) # if the photo interrupter detects a break: if data is True: # debug print print(z) # change the neopixel's color to the z index of the colors array crickit.neopixel.fill((colors[z])) # increase z by 1 z += 1 # if z reaches the end of the colors array... if z > 9: # index is reset z = 0
After downloading the Project Bundle, plug your Feather M4 Express into the computer's USB port. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the Feather M4 Express's CIRCUITPY drive.
- lib folder
- code.py
Your Feather M4 Express CIRCUITPY drive should look like this after copying the lib folder and the code.py file.
The use of the adafruit_crickit
library makes this code very straight forward.
First, the Crickit's Seesaw peripheral is setup, followed by the photo interrupter and dc motor's pins.
from adafruit_crickit import crickit # crickit setup ss = crickit.seesaw # pin for photo interrupter photo = crickit.SIGNAL1 ss.pin_mode(photo, ss.INPUT_PULLUP) # dc motor setup motor = crickit.dc_motor_1
Party Colors
Next, the colors for the party parrot are created and inserted into the colors
array. These will be accessed in the loop for the NeoPixel.
# party parrot colors for the NeoPixel parrot_0 = (255, 75, 0) parrot_1 = (255, 200, 0) parrot_2 = (90, 255, 90) parrot_3 = (0, 255, 255) parrot_4 = (0, 160, 255) parrot_5 = (90, 0, 255) parrot_6 = (175, 0, 255) parrot_7 = (255, 0, 200) parrot_8 = (255, 0, 125) parrot_9 = (255, 0, 0) colors = (parrot_0, parrot_1, parrot_2, parrot_3, parrot_4, parrot_5, parrot_6, parrot_7, parrot_8, parrot_9)
The adafruit_crickit
library has its own unique way of setting up NeoPixels when you connect using the NeoPixel terminal on the board.
# setup using crickit neopixel library crickit.init_neopixel(1) crickit.neopixel.fill((parrot_0))
Variables
Finally, two variables are setup, z
and speed
. z
will act as the counter for the NeoPixel's color and speed
will affect the speed of the DC motor.
# counter for party parrot colors z = 0 # speed for the dc motor speed = 0.3
The Loop
The loop begins by starting the DC motor. It will spin throughout the loop. data
is also setup to hold the input from the photo interrupter.
# begin the dc motor # will run throughout the loop motor.throttle = speed # read the input from the photo interrupter data = ss.digital_read(photo)
The main event is in the for
statement. When the photo interrupter detects a break, then the NeoPixel's color is updated to the next color in the colors
array. The index value is held by the variable z
.
Once z
overflows in value for the colors
array, it is reset to 0
to loop through the array continuously.
# if the photo interrupter detects a break: if data is True: # debug print print(z) # change the neopixel's color to the z index of the colors array crickit.neopixel.fill((colors[z])) # increase z by 1 z += 1 # if z reaches the end of the colors array... if z > 9: # index is reset z = 0
Text editor powered by tinymce.