hi
my name is massimo banzi and i like to
make stuff so today we're here
to look at another project from our
arduino starter kit
today we're making the spaceship
interface this is a simple project
designed to teach you about simple
inputs and outputs with arduino
this circuit is going to show you how
simple it is to connect a small button
and a set of leds to the arduino board
and how you can control the leds through
the button
first i want to explain to you a few
concepts about arduino
first of all arduino it's a small
computer the size of a credit card as
you can see here
that we can program using the arduino
development environment we're going to
show you in a few minutes
so the idea is that we write
instructions in the development
environment
then we press a button that gets turned
into a program
that gets downloaded into the arduino
and then the arduino can interface with
the outside world
to implement any crazy project that you
can come up with so
this project is very simple we have a
button
connected to pin number two on the
arduino and then we have three leds
connected to pin
five four and three of the arduino when
i press the button
these two leds that are now blinking
will stop blinking
and the yellow led will turn on if i
release the button
the two leds will keep blinking so
this is a very simple circuit
that allows us to control the behavior
of these three leds
from this button let's have a look at
the code that we need to implement this
behavior
at the beginning of the code we have
this line that says const int
red led one equal five so this
creates a constant called red led1 that
contains the value five
this is actually a very clever technique
that allows us to give a meaningful name
to pin numbers
so throughout the code i don't have to
use numbers
but i can use red readily d1 to remember
that that particular pin is associated
with the first
red led and if you look there are
another couple of
lines where you would definitely define
a constant for readily d2
and green led then
later on we have another constant called
switch pin
equal to this specifies that the switch
or button that we're using is connected
to pin number two then let's look at the
setup
the setup is that part in your arduino
code that gets executed once
when the board is powered on or reset
or this means also right after you
upload some code
into the into the arduino and it so as i
said this gets executed only once
so we see the instruction pin mode pin
mode
basically tells arduino that we want to
make sure that
pins readily d1 ready led2 and green led
are all configured as outputs because
the inputs and output pins
of the arduino can be configured to
assume both configuration
then we have an instruction called pin
mode
switch pin input which is used to
specify that the pin number that pin
number two
is connected to a switch and then we
want to make sure that that's an input
so that we can read from it
now let's have a look at the loop
section the loop section
of in your code gets executed over and
over
as long as the board is powered on so
we create a variable called switch state
and then we say
switch state equal digital read switch
pin so basically what this
does reads the state of the pin
connected to the push button and returns
the value high
or low depending on the fact that the
button is pressed
high or released low then we're going to
use
a clever statement called if which is
very important whenever you write some
code because this is the statement
that allows you to make decisions so in
this particular case
we basically ask arduino if the switch
state
is equal equal low do something
so in this case we use the curly bracket
to group
lines of code together so you can see
that
there's if statement is followed by
a question a condition that needs to be
verified and then a curly bracket
that specifies which lines of code need
to be executed
when the condition is true in this case
switch state equal equal law basically
says if the button is not pressed
and then we follow that with a series of
digital right statements that are used
to turn
on and off the leds and to implement
this particular blinking behavior
after that we have a delay of 250
milliseconds
followed by a short blink cycle that
happens on the other
red leds so the instructions that you
see in this
section of the if statement are used to
implement this blinking behavior that
you see here
afterwards there's a statement called
else else
is a statement that allows you to
basically create a fork in the road
in your arduino code with if
you can say if something is true execute
this piece of code
and else says if that condition is not
true then execute
this other piece of code so you can have
two different parts of your code
that gets executed depending on the
condition if that condition is true or
false
in this case when the button is pressed
then we use two digital write to turn
off the red leds and we use one digital
right
green led high to turn on the green or
yellow led like we have here so if i
press the button
the led turns on if i release the button
the leds are blinking
so this is all the code that we need in
order to implement this behavior
i want to remind you that the code
that's inside the loop
statement will be executed over and over
so as you can see
the blinking pattern is executed then
arduino reads the
input checks if it's true or false and
depending on that decide which behavior
to implement and then loops back
to the beginning that's all for today i
hope you enjoyed the tutorial and
remember
build it hack it share it because
arduino is you
bye