- [Instructor] So far we've focused
on controlling outputs
with the microcontroller
like the Onboard LED and NeoPixels.
In this video,
we'll start looking at inputs.
Inputs, allow interaction
with circuit Python.
A button is one of the most
common inputs and it's connected
to a microcontroller pin.
The microcontroller can detect
whether the button
is being pressed or not.
The circuit playground express
has two buttons, A and B.
There's also a switch
that works the same way
but while a button is momentary
and pops back to its
original position after release,
the switch stays in place.
In circuit Python,
manually setting up a button as input
is similar to setting up
an led for output.
As you can see from this example,
the main difference is using
switch to input
rather than switch to output
after specifying the pin
but like other components
on the circuit playground express
the CPX module provides an easier way.
You just refer to the button
with the CPX prefix.
On the circuit playground express
when the button is up,
circuit Python reads
the value as false.
When the button is down,
the value is true.
The big question though,
is how to write a program
that uses the button
to do something interesting.
One answer is to use an if statement
to do different things
depending on whether the button
is up or down.
This example turns the NeoPixels on
if button A is pressed
and off if it's not.
This button example
is a very simple case,
but if statements are used
in many, many programming situations
and here's the general format.
The conditions are the same
as ones used in the while loop.
Circuit Python will go through
all of the conditions
and follow code indented below
a matching condition before moving on.
That's the basic idea.
Now let's go to the desktop
to see this in action.
Okay, here I am on the desktop.
I'm going to start by opening Adam
and making it full screen.
Now I'm going to save this blank file
as our main.py file.
If you want to save,
what's currently
in your main.py file
now's a good time to duplicate it.
So I'm going to call this
I'll just click on main dot py,
save it and replace it.
Okay, now we're starting fresh.
Start by importing the CPX module.
We don't need time in this example
so I'll go on and set the pixel
to a comfortable brightness
then we simply want
a while loop while true
and then an if statement,
if CPX button A,
that's going to be either true or false.
If it's true
then I want to say CPX pixels fill
and I'll fill it to red
then else CPX pixels fill,
turn them off.
So I'll just fill them with zero
and that's all there is to it.
So let's see what happens
when we save this file, save.
Now you can see
the circuit playground express
isn't showing any lighted pixels.
If I press on button A.
There we go the pixels come on.
If I release it,
the pixels go off.
A button is one of the
most common types of interaction
for a device.
This example was pretty simple,
but it shows you
how easy buttons are
to use in your programs.
If you want some additional practice
before moving on,
here's some things
to try in your own.
Turn the NeoPixels on by default
and off with a button press.
Use the switch
in place of a button
or this one is a little advanced
use the switch to select
between two NeoPixel animations
when the program starts.