no guys and I am palma quarter and i am
here to show you are doing is number
three
hopefully you enjoyed the first two
lessons if you haven't had a chance to
look at those please go back and review
the material there because the lesson
today we'll be building on the first two
in the last lesson we learned is we
learned how to use the arduino to
control we learned how to use the
arduino to control an LED and then also
we learned to start writing a little bit
of code in the arduino so we learned a
little bit about circuits we learned a
little bit about code we're going to
build on that and we're going to do a
couple of things were going to make a
circuit a little bit more complicated
this time and then we're also going to
learn some new constructs in programming
and so we're just going to continue to
build and every lesson is going to build
on the previous lesson and when you look
at this lesson we should have a high
comfort level because a lot of it is
just reinforcing things that we learned
last time plus we're going to learn to
do and powerful techniques that will
help you write better code and help you
write more more complicated programs the
basic assignment this time is rather
than blinking one LED what we're gonna
be doing this time is we're going to be
blinking to LED we're going to have a
red LED and we're going to have a yellow
yellow LED for the equipment that you're
going to need for this project again
that if you if you've gotten a spark
planned dinner kit you'll have
everything we need in that if you don't
have it what you need is a minimum is
you need an Arduino microcontroller i'm
using the arduino uno version 3 will
need a red LED and a yellow LEP you'll
need 2 334 sisters remember every time
we led we need a resistor in series with
it needs some of these jumper wires and
you need a breadboard and it's pretty
easy as far as the hardware goes and we
will jump right into it guys on this
video I am going to forgo step-by-step
with you and what lets you watch me
build the circuit so that you can
continue to become more comfortable with
the breadboard or the prototyping board
but in future videos i'll just show you
the schematic of the same condition and
in by the next video I think you guys
should be confident I'm not to sort of
look at the schematic and build it
yourself
but just in case you're not going to go
ahead and go through the build with you
on this one so again what we're going to
do is we're gonna readily to you were
gonna have a yellow LED and we're going
to control them from the arduino and we
want to control them independently so in
the first and most in the first two
lessons we basically just use one pen of
our digital pins down Europeans 03 13 on
the Arduino so far we just used one at a
time we've got 13 pins we might as well
start branching out and not being afraid
to start working with more than 110 and
it's hot in this video we will be
working with two tens because we want to
control both LEDs independently if we
connected both LEDs 21 pen or one
voltage supplies and they will all be on
together off together and we want maybe
12 beyond and the other to be we want
them to work independently and in order
to do that we're going to need to
connect to two of the pin on the Arduino
okay I want you to kind of think about
how you might be hooking this up and
maybe this would be a good time disorder
for to pause the video and come up with
your own strategy of how you would hook
this stuff together to get the
connections that are indicated in the
circuit diagram so one of the things we
need to become comfortable with in
engineering and comfortable with in
computers and programming is going from
a very abstract concept like whiskey
circuit schematic and to go to something
that is more concrete which is this
that's more of a graphical
representation of how we would look to
circuit together now I've gone ahead and
I've kind of done one here you don't
have to make yours look exactly like
this you can kind of look it up like you
like you like but our but it should
accomplish the same function now there
are things that are lootin couple of new
things and I'm doing here as far as the
hardware goes that in the last circuit
we were just connecting up to one led
the ground 10 of the LED that shorter
leg on the LED i connected directly over
here to the ground 10 on the Arduino
well the problem is that is you start
getting more and more components
all of them are going to want to be
grounded and so you're gonna run out of
ground since I think there's three
ground pretend very quickly you're going
to run out of ground pins on the Arduino
everything always wants to be grounded
every component is going to want to be
grounded and so what you need to do is
you need to establish a ground rail you
need to establish a ground real on your
circuit board and i have done this year
in this diagram by bringing a wire from
the ground 10 to this top row and for
this circuit board shown here it makes
everything in that top row connected to
that wire so now anyone of these holes
if I put a wire into it i have access to
the ground to this connection and so
usually you want to establish this
ground rail for most projects and so we
might as well do it
starting now even though we would have
enough grounds to run both of these LEDs
on arduino it's a good practice just to
start by establishing a ground rail also
if you have more than one pc born all of
your grounds always need to be hooked
together and as I look at students
projects and there's problems in the
circuitry is often is not it's that they
have different components that they are
not looking the ground rails together on
so pretty much in a circuit all your
grounds need to be hooked together
ok so we've established our ground rail
here and then also what you see is that
on the last project we had simply use
10-9 to control the LED going to current
limiting resistor well this time we want
to control two LEDs and so we're going
to need another ten i just use 10 it's
convenient it's right next to it and so
10 is going to come down and drive the
yellow LED through this 330 ohm current
limiting resistor and then we've got
this green wire that comes from the
short leg of the LED over to this ground
rail that we have established now one of
the things that I want you to be to God
take away from this is that is you're
doing these things you want to keep your
wires as neatly as you can and you want
to keep your leads as straight as you
can and just try to keep this
need an organized because with this fuel
components it's not a big deal but as
your circuits get more complicated they
need to be really meet in really precise
thoughtful in the way that you lay
things out there's many different ways
that can work but laying them out so
that they're easy to see easy to debug
easy to keep track of your large as a
pretty is a pretty important thing one
other reminder is that the direction on
these LEDs does matter the long leg
needs to be connected to the AH path
that is going to the positive voltage
and so the voltage will be coming from
10-9 coming to the resistor and so
therefore the long leg needs to be the
one connected to the resistor not
because it's a resistor but just because
the long leg leg needs to be pointing
towards the voltage supply not pointing
towards the ground the lower leg is what
we point towards the ground
ok so just remember a lot of times if
URL if your circuit doesn't work a lot
of times it's because you put your LED
and backward if you do put your LED and
backwards it doesn't hurt it doesn't
burn it out but it won't work until you
turn it around
ok so that's going to be our circuit
there and I think what I'd like to do is
I'd like to just go ahead and try to
kind of hook this up along with you and
please feel free to work along with me
you might want to even are you might
even want to pause the video as we go
through here and in order to keep trying
to keep track of what i'm doing but i
will try to keep both of these things on
the screen at the same time and I let me
even see ya i think this is going to the
Lord good
ok so i'll try to get this sort of in
the same orientation and I like to try
to work in the same orientation as my
schematics and so if you notice here
we've got the UH this is the connector
for the USB cable as is this inside kind
of got that oriented the same i'm going
to try to get them a light laid out
pretty much the same i'm cleaning out my
two resistors I got my LED
so I'm gonna I like to kind of start it
one into the circuit and so the first
thing I'm going to do is I'm going to
establish that ground real because that
nothing's going to work we're not
grounded so I'm gonna come over here put
that in come down and in this one its
side let's see this is opposite of that
struggle he didn't do it this way I'll
put it right there on the top rows so
I've got the ground real established on
the top rope
ok then I have another ground it's going
to come from that top rope
ok so this wire would be this one and
it's going to come around somewhere in
the vicinity
let's just stay right here okay and it a
little put it right there and matt needs
to connect to that short leg so this is
right here and remember the short leg
goes towards the ground in the long leg
goes towards the positive so that short
leg of the LED needs to go there let's
see if you can see long and short I
short is on the right and so that's
going to go exactly in that same column
and we'll go over a couple of more
columns how many you go over is not
exactly that important
you just need to kind of go for its
natural like you see the LED poked up
Popkin they're very naturally without
having to dig in the pins and just take
care of your components don't don't bend
your pins up put them in carefully and
don't let your lead to get all bent up
because you'll want to use these
components in the future and also
there'll be other people using the kids
in the future we want to keep them nice
and neat for them as well
ok so the next thing is from the other
leg that long leg of the LED we need to
connect to a resistor the resistor is a
330 ohm resistor remember we don't trust
that it's 330 homes just because that
was the label on the package we took it
out of we know that a 330 ohm resistor
has the color code why George Brown what
I'm saying there is orange orange brown
on this one
I don't think it will focus that close
to really show you in detail but i know
this is the right resistor now
so one leg goes in that same column
looks like I came down about this for
here one leg goes in that same column is
that led and the other comes over just a
natural amount three or four columns
over and now that top of that resistor
this leg of the resistor needs to
connect over here back to ten nine not
even like to use the same color wires
that I used on the schematic and by
using a red wire for i had a red wire is
not the color matters but using a red
wire where i had a red wire the thing
that happens then is that you can
ah you know it makes it easier to debug
it later if you have a problem in the
circuit and so I'm going to come up here
in that same column the same column is
that leg on the resistance so if you
notice here
those are in the same column so they'll
be connected together now over here i
have these in the same columns i'm going
to even see i can zoom in on that you
cannot see that a little better
we're working right here in that area a
little bit it's going to be pretty good
you can see right here is the lead and
then it connects right over there to the
wire
alright where does this wire go this
wire goes right back to 10 9
why do I use 10-9 just because I like
ten nine active use anything from 0 13
on this but I like 10 9 use that one and
you could use a different one but if you
want to do it like me you'll use tonight
because it's real easy to get these in
the one things you wouldn't really want
to kind of eyeball it
make sure you're right above it because
it's very off to get it's very easy to
get those tens off by you know it's very
easy to get them off by one
ah you know one notch if you're not
careful out on that lets you see it
there ok you can see there that I am in
10 9 in
in 10 9 00 to i think i can just a
little bit in 10 9 does go to that
resistor is connected in the flame
called so we got that hooked up we're
well on our way
I think we pretty much got that first
led plugged in
let's go over a little bit let's start
working on the next one I usually just
like to scoot over just a little bit
myself a little bit of room but you
don't want to space it out too far as
your space on bigger projects but i'll
start by putting this resistor right
here in and I could do a sister right
here and right here again just sort of
hopping over kind of a natural number of
spaces were sitting nice and neatly and
then it looks like i'm using a blue wire
to go from the top of that resistor so
we're going to connect up here right
above it from the top of the resistor
the same column so that's hooked up and
we're going to use 1010
ok so come over the first was nine
that's one can know about my yellow LED
and the long leg again goes back towards
the voltage to class for the long leg
would connect right there to the bottom
of the resistor plug that in
make sure you get in there nice and good
and then you can see in the schematic
there is a green wire that goes back to
our ground real and so I'm going to take
the green wire and I'm going to come
back to the ground real alright so now
hopefully you can kind of see that
circuit here is the same that's what
we've drawn here or at least what is
graphically represented in the circuit
is kind of what we achieved here now I
cannot promise you that I've not made a
mistake somewhere along the way of
looking it up i tried to be careful but
don't get discouraged if you have a
mistake in your circuit is called
debugging it's called troubleshooting
what you have to do and if you don't
have a problem on this one you will have
a problem somewhere along the way and so
you just got to get good at product we
going through and turning your problem
but at this point I think that my
circuit looks pretty good and so with
the hardware part of it done we're ready
to jump in and start doing some software
so i think i can go ahead and get that
out of the way and now we are going to
start writing our our Arduino code one
of the neat things is that pops up it
usually pops up with your voice setup
and void loop already set up for you but
you know is that every arduino program
needs voice setup every are going to
program needs a void loop the things
that you put in the setup for the things
that you want to do one time like
declaring your PIN modes and things like
that things that you just do once you
doing your setup and things that you
want to look through over and over you
doing your void loop
ok what we're going to do is we're going
to start by declaring our variable in
this case we're going to use global
variable that means we're going to
declare them at the top so that
everywhere in the program that will know
what these variables are four variables
that we would only use in one spot like
if we only needed them in the void loop
we can declare them down here and the
voice was mostly local variables but in
this case we're going to use global
variables
now let's kind of think about some of
the stuff that we had to do earlier and
I remember on our earlier projects one
of the things that we have to do is we
have to declare our variables and so
that's what we're going to do and in
declaring our variables we need to start
thinking about what variables were going
to need now in our last program we had
one led the red LED and we had it
connected to 10
so up here at the top we declared one
variable red LED 10 and we set it equal
to 9
well this time we're using how many tens
we're using two pens so how many pin
variables or how many variables
associated with the Pens we're going to
need we're gonna need to okay and we
need to name one of them like we did
last time and we need to say kind of
tell the arduino put a value there about
where we're going to hook it up so so
pins always coming around numbers you're
never going to have a pin
nine-and-a-half the pin is going to be 0
1 2 3 so we need to declare that
variable and in or integer later we
start dealing with numbers that we're
gonna be doing math with will use float
for floating-point numbers which is sort
of equivalent to in your math class real
numbers it's all the numbers that are in
between the integers and everything else
but these will use integers now why not
just make everything slowed I mean why
not make everything flowed well in the
computer memory integers take much less
space to save and so anytime you can
it's a good practice to clear something
is an integer so we're going to have a
pin or we're gonna have a variable that
we call red LED and we'll set that equal
to naught
ok we're going to set that equal to 9
because we have the red LED 1010 I'm not
now remember you could call this
variable anything that you wanted
okay you don't have to you don't have to
name my and there's nothing magic about
red LED 10 you can just call it red pen
or you could call it red LED to call it
what you want but what you name it you
got to keep using that same name and
remember that the capitalization matters
ok so i'm going to introduce one new
concept here is our program start
getting more complicated we need to
start keeping track of what we're doing
and we need to start documenting our
code and basically the arduino let us
could comments in that are only for
humans to read it's only for us to read
the computer doesn't leave it doesn't do
anything the arduino doesn't do anything
with it and in the way we get a comment
we just put two slashes like that
that's the forward slash two forward
slashes and that's telling arduino that
this is just a note to ourselves
it's not anything that the arduino needs
to user look at what I just I just
remind myself that what am I doing here
I'm just wearing red LED as so I'm just
saying this is what i did nine ok now
what would be the other thing that we've
got two LEDs what's the other one what
we need a variable for the other LED and
we're going to call that well and what
would this be what would make sense to
call it the yellow LED and what did we
hook the yellow led to over here we
hooked into penn remembering that we
always went in almost all lines and an
arduino program with a semicolon here
another comment declaring yellow and the
nice thing about documenting your code
is that if somebody else comes in and
looks at your code
I think your teacher looks at it or
something they can come in and their
friend looks that they can come in they
can understand what you're trying to do
if I just start throwing variables out
nobody's going to understand what I'm
doing but that put comments in there for
the humans that helps me understand what
you're doing now remember when we were
break linking the red LED there were two
parameters that we needed to define a
blink and that is sort of like in the
blank how long it on and how long it off
because if you're going to blinking LED
it's on par the time and it's all part
of the time so remember how we had an on
time and we had an off time we're gonna
need an on-time and an off time but if
you think about it we might want to
blink the yellow LED with the same
parameters at the red LED and so
probably I think we should use for
variables a red time on a red time off
the yellow time on yellow time off
solutions for variables this
and so the time is in milliseconds and
milliseconds work very well as integers
and so we will declare it an and i'm
going to read on people's let's say a
quarter of a second
so what number we want to push her for a
quarter of a second its measured in
milliseconds thousand milliseconds in a
second so it's worth of its second would
be a fourth of 1000 which would be 250
so we're going to leave the read on for
250 over here on your time we make that
250 it's going to be offered quarter
second offer quarter-second on the red
1i really be really careful with your
capitalization again I'm using this
bumpy font because it makes it easier to
read where the first letter of each new
word on a capitalized you can sort of
see how the words go together but the
capitalization matters if you don't use
exactly the same capitalization you
don't have to capitalize like I do but
whatever you're doing you got to do it
exactly the same way every time okay and
so we're going to say this often
okay slowly we're gonna have integer
yellow time that I want to make them the
same i can change later but unlike the
same right now don't forget your colon
slash slash yellow one time and then
candidature what off time and it's gonna
be 250 all right we be a little off
ok so these shot when we have six
variables to the variables film tell the
yards we're going to tell the arduino
which pins i'm connecting to
in the circuit and the other four
parameters how long the red should be on
how long the rest of the offer a blank
along the yellow should be on for a
blank how long the yellow should be off
for a bit
these parameters are going to go a long
way of being the variables that we meet
and the void setup you can see here this
is just a comment you put your setup
kosher to run once and we don't really
need that comment
so what do we need to do and are void
setup remember anytime we're going to
use a pin on the Arduino anytime we're
going to talk to one of these pins on
the Arduino we've gotta tell the arduino
whether we're going to be talking to it
we're going to be listening to it and in
this case the Arduino is going to be
sending out of voltage so it's going to
be an output because we're something
something out to it
okay and so what we're going to do is
we're going to make a load remember that
and mode is capitalized and kind of work
needed you see that it turns orange when
you do it right select was just imagine
I did it wrong i know'd you say I'd like
that put it in right it turns red if you
pay attention the colors it gives you a
mistake
we're going to red LED and then that is
what type of 10 wanted to be an output
output sort of turned blue right and
every line in a semicolon in nine now
we're going to do the mode and then
we're going to do the yellow LED 10 what
we have the yellow LED penhook to we
have that book 2 1010 like that right
no no no do not need constant in your
pen modes always use the variable that's
why we set it up here we're going to set
the pen mode yellow LED and it knows
what yellow LED pen is it knows it's 10
because we told it up here but don't put
10 in here when I great your code or
went around and look at it i'm going to
count off if you get lazy and start
putting constant here instead of using
your variable and just like the red this
is output wait a minute what's wrong it
didn't turn this orange oh look at that
time this belt ten smell that pie except
now look at that everybody's happy
so that's good
ok so now we're all set up and we are
ready to start writing code here okay so
how do we turn that pin on how do we
turn that nine on like if we looked at
only to turn on
oh yeah let me just kind of tell you
what we're trying to do here we're
trying to create i'm just going to make
something up or try to create a program
that will bring it blink the red LED 10
times and then Frank blink to le the
yellow LED one time I want 10 Reds and
one yellow
10 Reds and one yellow 10 Reds and one
yellow and so the void loop needs to
blink the red LED 10 times and then
blink the yellow LED one time you'll see
in a minute why where what we're doing
this is because it's a way to introduce
a new code but for right now let's just
do this the way that we know based on
what we know well how would i turn the
red LED panel on i would say did you
told like okay and i want to write which
10-4 red LED then i'm going to tell it
where I'm gonna lie i want to write to
the red LED pen which is ten nine and
then what I'm going to do I want to go
hike the word to turn on location and I
want to come in and I'm going to put my
comment and again / last shows a comment
turn the red LED on very good
ok now it's on before i turn it off i
gotta wait so what do i do i delay how
long
well I want it to be the red on top
ok ready on time and what that means is
it's going to stay on for however long i
delay here so i delay read on time it
will stay on this one
now I want to turn it off actually did
just told right red-bellied key and what
do i do look low will turn it off
ok now after i turn it off I better put
a delay is your turn right back on so i
gotta leave a delay and delay it I I
off and so it's going to turn on we have
a second turn off with half seconds
what's going to sit there and it's just
gonna just going to blink like that
ok now one of the things i want you to
see that I've done here that's I think a
good coding practice arduino does not
keep track of white spaces arduino
ignores watch places some programming
languages languages like Python they
care about the white spaces and you have
to do the white spaces perfectly but I
want to indent my code even though
arduino doesn't look at it because like
if i look at my voice setup I have all
this indented so it's easy for me to see
what's in the void setup it's easy for
me to see what's in the void loop by
kind of doing this indentation I suggest
that you do you do that one of the
things i would like to do is I like to
kind of run my clothes I go so I can
sort of find my problems early on and so
I don't want to just go ahead and run
this even know what we want to do is
we're gonna get 10 times the yellow 110
times I want to make sure that my code
is working so i don't get too far into
this before I figure out that I've made
mistakes so let's go ahead and do this
okay I saw a lot of green down here that
means everybody was happy didn't get any
orange and look at that that led to use
blanking on for a quarter-second off on
off on off on off quarter-second each
just exactly like we told it to ok I got
a little sloppy here and I was not going
to put my comments and let's go back
into our comments and and say turn the
red LED on
ok I wait and then what we do here we
only read really easy off and then again
we wait and so that's just this is just
good practice to put your comments in
here so people can see what you're doing
if you come back later you can see what
you're doing it's always good to put
those codes in there all right
now the thing is that the reason that
continues to blanket as it continues to
lose but if I wanted to bring ten times
and then blink the yellow one
I've got to put this in here 10 times to
blanket and I guess there's two ways we
could do that one way that we could do
it is that we could just go in and we
could retake this 10 times but i think
i'm going to try to cut and paste and
see if that will make it a little bit
quicker some of the copy it and I want
to blink 10 times already have one in
there so i only need to taste it nine
times
that's 123456 not okay and i wasn't
paying attention i didn't copy real good
and I just like meat codes I'm going to
take that extra space out before the d
just because I find if I keep my code
meet it's a lot easier to find problems
also like if you're a student and
teacher look at it or you have somebody
looking to help your code help you
figure out what's going on in your code
if your code is in a big mess they're
not going to be very eager to help you
get your toes last well-documented a lot
more likely to get some help from
someone so now if i run this it's not
going to look a lot different because we
haven't done anything different that who
i erase my D their feedback and erase
the in this place
ok here we go let's try everybody's
happy we got the green here and if we
look down here we are going to see a not
blinking red light of it i hope
little bit slow here to download i'm not
sure why
there it goes we did not like not
exactly sure what is happening here
alright in there it goes that point I'm
not sure just got a little glitch on the
download that time and that happens
sometimes but sometimes just tried again
ok so this is all working in the LED is
blinking 10 times and then repeating
blinking 10 pounds repeating well what
we wanted to do this after doing this
ten times we then wanted to link the
yellow LED so now we go link 10 from the
red LED now we need to place the yellow
leds would come in here did you write
ok and what can with the yellow LED
connected to it was connected to intense
we pretend they're right
no do not put constants in there
remember we made a variable called
yellow LED okay to tell that high when
we think about this like imagine now if
I wanted to change and connect the red
LED to pin at 8i would have to do is
just come up here and say eight and then
move it over if i put constants and here
i would have to come in on every single
one of these and change it and then we
kind of change it you increase the
chance you're going to make a problem as
you can see by using this variable it
makes it much easier to change
ok now we're going to come in here and
this is going to turn the yellow LED on
and then we're going to delay how much
by the yellow one time
ok this is basically what we're doing is
working here wait and then we're going
to teach you to write yellow LED and I
oh that's because why did this not turn
bloom here these other ones look at that
it needs to be capitalized in
capitalizes looks good
ok i try to make these mistakes just so
that your father had seen them when you
make them because believe me you will
make mistakes like that
ok now we were going to wait here we're
going to turn into love
so we turn it on wait turn it off off
ok and then okay and then that we are ok
now understand what we write out here
again i've said this before but you
really need to get what we write out
here past the hash mark slash slash
marks the computer doesn't read that
that's only for us to remind us what
we're trying to do
ok now if i have this hooked up right
this time it should blink the red LED 10
times and then it should lead to help
but the yellow LED one time if I hooked
it up right so let's see first of all
that we have any reason we got a error
in the code error in the code
what does it not like it doesn't like
yellow off time and so what did I don't
want their yellow off time
hello you guys might see it before i do
ye low-50s any here we have ye low-80s
at t-i-m-e watching my mistake was
trying to do yellow all time was not
declared in this scope and so what that
means is that there is something
different about how i wrote yellow off
timeshare vs hell i wrote yellow off
time here and
when that happens one of the things that
sometimes dishes come up here and copy
that
shrug it is exactly this name ah i see i
capitalized the why here why didn't
capitalize the initial contract you
notice when that happened that will
happen to you don't panic it's just she
made a mistake just find your mistake
something that isn't exactly right just
going to find it you see I didn't
capitalize with this time it is going to
work
scream down here everybody's happy
12345678 content to okay look at that
that looks good
you know i was just counting it is it's
a little hard to count but it is working
correctly so the red one blinks 10 times
and then the yellow one blinks to one
time and the neat thing is it's doing
exactly what we told it to do it is
blinking exactly in the sequence we told
it to do it is just going through this
program line by line and it is doing
exactly what we told to do and so this
is a great day we got our circuit hooked
up right we got our clothes written
everybody's happy everything's running
great
okay now this is the problem with what
we did it works but it's not the best
way to do it
imagine if i told you i wanted you to
blink the red LED 25 times or something
like that i wanted to just really going
there and do something crazy and the
problem is you see how it's kind of hard
to count these claims i try to make it
as easy as i can you see not only do i
put a little in debt here let's put a
space between the blog sockets or
account like 12345 but it's hard to
count scrolling code like this you
really need a better way of doing it
ok you need a better way of doing it and
so what I want to do is basically it's
this one thing here this one block of
code we want to repeat over and over and
over so i want to get rid of these other
nine times that I told it to do that and
i'm only going to keep the one
ok that's what i want to do is I want to
loop through this toad 10 times and then
I want to look through this code one
time
ok so rather than type this code ten
times there is something that we can do
in our Arduino in just about any
programming language or in all
programming languages is we can make a
loop just like this is a big void loop
it's always there we can make our only
the top of the loop is that we're going
to make is called a for loop in a for
loop will loop through the code the
number of times that you tell it to and
the way it works is you create the poor
loop thing for and you see how it
recognizes the word for because you put
the put in
orange making I know what that name
classing that cool that's what it means
when it turns out alright and then
inside here we're going to put some
condition and can parameterize don't
right conditions but that's just what
we're going to put in there I'll get to
that later
so the conditions going there and then
where does the forward again
a looper Claus always begins with a
curly bracket and it ends with a curly
bracket so we need to come at the end of
the loop in between those curly brackets
is the for loop now you gotta kind of
keep track here because now we're
getting loose inside of which and one of
the things I can do is if I click on
this close curly bracket it shows that
it goes with that probably bracket so
when i do this
that is the for loop from here to here
now the void loop business outside loop
which goes all the way through there but
we're not concerned about that we are
working on our for loop here so this for
loop is going to loop through the
segments of code that starts with the
open curly bracket and ends with the
close curly bracket alright and so you
got to keep track of your curly brackets
and a lot of times when i see errors in
code
it's because you aren't closing off your
curly brackets and so one of the things
i do is we take this out here for the
second he's going to cut that ok when I
make a for loop okay i just made the for
loop i open my curly bracket i come down
a few lines and I close my curly bracket
right there in all my business i do in
between this curly bracket and that
curly bracket if you start writing a
bunch of intense code and here I
polishing you're going to forget to
close your curly bracket so when you
make your for loop go ahead have it
start and and right there before you put
any coded now we can come in and we can
put that code back in okay and this is
the code that it would take to blink one
time and so how many times do I want to
do this loop i want to do it 10 times
because i want to blink 10 times so this
is the way you do it
you've got to create a culture
now I could declare the counter and
usually what you use for a counter is
the letter I the letter j for some
reason it just started way back in the
day and so a lot of times the variables
that you use for a counter or I or J but
you can use anything that you wanted now
if I were going to do that I could come
up here and I could declare a variable i
could say this will be my counter okay
but this is a case where you really
don't need a global variable it might
make more sense to make that a local
variable so we're going to make that a
local variable by declaring it down here
inside the void loop in fact we can
actually just clear it inside the for
loop and we would put it right here
we're going to have the variable and i
think im gonna make it Jay integer j and
so inside this condition of the for loop
between those two parentheses i'm going
to say j equals 1 that means the first
time that I go through the loop jay is
going to be equal to 1 because I tell it
start with j equal to one and then the
second part of it after a colon
semicolon here is you tell it how long
to look I want you to look as long as j
is less than or equal to 10 and so this
single continuous loop as long as j is
less than or equal to 10 and then the
third thing that you put is what you do
to j everyday time
well every time you go through the loop
j is equal to j plus 1 and so every time
you go through the loop jay is going to
increment by 1 and so let's just kind of
think through this you get to this loop
and then you're telling me are going no
I want to do a for loop i want you to
start with my counter j equal to one
chase an integer I want you to continue
to loop and as long as j is less than or
equal to 10 and then every time through
I want you to increment J by one so the
first time through was she equal to its
equal to one then the second time
through you add 1 to j j is equal to 2
is too less than or equal to 10 yes it
is we're going to go through it again so
j is equal to 10 to two three four five
six
910 and this time J plus 1 is equal 11
that's more than ten it pops out it
doesn't do it so this will do it 10
times that will do it for j equal
123456789 10 and then it will pop out
and come and do whatever the next thing
is so it goes through this loop as long
as j is less than or equal to 10 now
understand you could use a different
variable you could make it I why do I
not use I so much because most of the
misery in my life it's been associated
with using I as a counter and look how
much I looks like one and so it's very
easy to get a typo or you confuse I and
once I just like to use city because it
doesn't look like a one and it's easier
to decode it easy to debug problems in
your code so you can start at ten and go
to 20 and it would you could start at 11
and go to 20 this is the starting j and
the mission loop as long as it's below
this and then every time you make j is
equal to j plus 1
ok so this is going to go through it 10
times this is a for loop for loop is one
of the most powerful things that you can
have in programming and it's just an
incredibly powerful concept because you
see look how much easier this code is in
the earlier code that we did it is so
much easier
let's hit it and see what this does now
ok
it downloads it's happening 23
it goes until you see that the program
is still working at least 10 times and
then it like the yellow 11
ok so what's one thing this is really
cool this is made a lot better
what's one thing that maybe would like
this little better there's one thing
that I don't like about what we did I
don't like how we came down here and
made this a tent
what if I only wanted to loop five times
i guess i can come down here and make
that five and then I could download the
code again and then come over and watch
this it will take a second and then
12345 yellow one two three four five and
then the yellow
ok so you see this is working in
you can come down and change this to
make it go but I never like to change
code down there in the guts of it right
here it's a lot better remember I don't
like using constant down here so how can
we get rid of this constant why don't we
make this red link so this is the number
of times you want to read to blink if
we're going to use this down here we
gotta declare it and so we're going to
come up here if we're going to use the
variable together to clear it up your
integer and that is going to be okay and
let's say that we're going to make it
three times i put the three here and it
always will blink however many times and
only continue to be a good boy i will
put on a number of red
ok so what we have now here is we have a
variable set up where do the way we can
control the parameters down the program
is just playing up here and not getting
down and get to the coat so let's
download that and see if it works
okay look at that one two three blank
123 yellow one two three yellow you see
that's that's kind of nice because now i
don't have to get down here and get
confused about what's doing what i can
do all mine all my controlling up here
let's see if we can make it blink like
faster so let's say that I'm gonna go
let's just make it do different like I
wanted to stay I wanted to be a a short
links are going to have it on for a
hundred a hundred milliseconds and i'm
going to have it off for 900 so that yet
the red-eye mess that up
not that the right is going to be on for
a hundred milliseconds that's a tenth of
a second and off or nine hundred
milliseconds so it's going to be on for
a tent offer nine kids and then on the
yellow looks do it backwards let's have
it mainly on
we'll say it's on for nine hundred and
it off for 100 and then let's have it
blank five times on the red it's
downloading everything is green
everybody's happy
ok let's see if it's going to work ok
the yellow stays on you see the red just
blinking very shortly the Reds mainly
offering side x and the yellow has a
long you see these things are behaving
exactly the way that we told it that we
told it to so that's pretty that's
pretty cool in American just stuck and
just control things from up here
alright hopefully you were able to
follow along with me what I want you to
do for your assignment is I want you to
create another for loop and have the
yellow blank inside of its own for loop
i want you to have the yellow blinking
has its own for loop and therefore
you're going to have to have another
variable up here not yellow black and
that way up here you could say blank
read ten times and yellow 10 times or
blank read one time and yellow 10 times
and so then you're going to have
complete control of both of these LEDs
from up here
okay be sure to check out my website top
tech boy calm i have the source code i
have diagrams I have all the information
as well as the sources of equipment that
you need for this project
be sure to check out the blog and be
sure to the waiting for lesson form
which will be coming shortly talk to you
guys later
Không có nhận xét nào:
Đăng nhận xét