9

(Elliott) #1

Smart backpack with wireless charging pocket


TUTORIAL


your strip. pixpin identifies the pin we’ve connected
our NeoPixel strip to, which is D0. The strip variable
creates the NeoPixel strip as an object, and sets
the brightness to .5. You can change the brightness
level as needed for the look you want. strength and
direction are variables that are used to create the
pulse effect; leave them at 0 and 10.
numpix = 34 # Number of NeoPixels
pixpin = board.D0 # NeoPixels pin
strip = neopixel.NeoPixel(pixpin, numpix,
brightness=.5, auto_write=False)
strength = 0 # blue intensity
direction = 10 # direction of intensity

In the main loop, we’ll create one more variable
for the colour BLUE. The three values inside it are for
red, green, and blue values, from 0 (no colour) to 255
(maximum colour). Set red and green at zero, and use
the variable strength for blue. By changing the value
of strength in our main loop, we’ll be able to make the
blue glow change over time.
while True:
BLUE = (0, 0, strength)

We’ll send this colour formula to the strip with:
strip.fill(BLUE)
strip.show()
time.sleep(0.03)

At this point in the loop, the colour formula has been
sent to the strip, but we won’t see it yet because the
strength variable is still set to zero. To increase the blue
colour slightly over time, we’ll add:
strength += direction

This line increases the strength value by adding the
direction value to it. On the first pass through the loop,
strength = 0 and direction = 10, so the new value
of strength would be 10. On every additional pass
through the loop, the value will increase by 10, and
the strip will show a blue glow that intensifies slowly
over time.
Next, we need to add upper and lower limits to this
function. A conditional statement will work great for
this. First, let’s say that if the strength value ever gets
all the way down to zero, it should be reset to 10. At
that point we’ll also want the intensity to stop getting
lower and start going up, so we’ll flip the direction
value to its opposite:
if strength <= 0:
strength = 10
direction = -direction

you can change up the animation with new code at
any time.
Connect the Trinket to your computer using a
micro USB cable. The Trinket will appear as a drive on
your computer called ‘CIRCUITPY’. On the drive, locate
the file called main.py. This is a text file that contains
the code running on the Trinket – to reprogram the
Trinket, we’ll edit this text file. You can edit the file
in any text editor, or download a Python code editor
like Mu. Saving the main.py file will make it run
automatically on the Trinket M0.
Open the main.py file in your text editor. If your
Trinket M0 is fresh from the Adafruit factory, it will
have demo code in it. Select all of the existing code,
and delete it – we’re going to start fresh!
The first thing we need to do is import some
libraries. These libraries contain helpers that we’ll refer
to in our code.
import time
import board
import neopixel

Next, we’ll set up some variables. These
placeholders will be useful for modifying and
troubleshooting the code later. Start with numpix, and
change the number 34 to the number of NeoPixels in

Right
Keep your sewing
tidy and you’ll
end up with a great-
looking bag

Above
A small board like the
Trinket can be hidden
almost anywhere
Free download pdf