welcome to Arduino made easy or easier this is lesson 4 we're gonna be talking
about if statements and while and four statements so let's get started with
this right now I'm Tom Kvichak and this is Toms Trains and Things this
channel was created to help other modelers who are in need of guidance in
pursuing their dream of building a model railroad and I'm just trying to make it
easy for you to understand Arduino even though Arduino is pretty easy to start
out with if you don't have the experience with coding or electronics it
might be a little bit difficult for you so I'm just trying to make it a little
bit easier so if you would like to see more videos like this go ahead and hit
that subscribe button and while you're at it ding that bell so you could be
notified of when I have a new video coming out and speaking of videos go
ahead and check out my playlist where I have over 40 playlists categorized for
you to see what you're looking for easily so let's get started with this
we're gonna talk about if statements for statements and while statements today
we're going to use the same sketch that we used in lesson 3 we're just going to
change some of the things on it and the Fritzing diagram is going to be the same
so let me show you this right here everything's gonna be the same we're
gonna use the same two LEDs and we're going to use the potentiometer on there
and so whatever we did on the last lesson you could just take a look at
that use the same Fritzing diagram on there and we'll be good to go I have
less than three up here so we could just review this real quickly we declared
what we're going to use in there I'm going to add a couple of couple more
items on here in a minute or so but we're going to use lesson four but
we have the first four and we have in the void setup right here the pinmode my
LED one my led to and serial begin 96 under we're gonna keep that also and in
the void loop we're going to keep the first two lines in there of Val equal
analog read plot pin and Val equal map Val 0 to 1023 and 0 to 255 so everything
underneath this we will not need so normally I tell you to type everything
in but since we're going to be using the same values to start out with as we did
in lesson 3 you could delete those last 4 lines on there keep those curly braces
in there that this last curly brace because you'll need that for here you
see that one matches this one here but anyway we're just going to delete these
last 4 lines here and what you could do is come down here and do a save as and
save it as lesson 4 and or name it whatever else you want it but I have
lesson 4 started already I have the same values that I had in lesson 3 that I
just told you about right there all the way down to mapping the value the VA L
so what we're going to do is add two lines two lines up on top and why we're
going to do that is we want to count how many number of blinks that we're going
to have on our LED so we're going to call a BL n k BL and k equal and we're
just going to say 6 4 right now we could change that at any time and we're not
going to write a description in there right now we'll do that at the end of
the the video and then we want a wait time
for the delay so we're I'm just gonna put in wait equal and we'll do 250
milliseconds
and let me put a space in there and that's all we need right now what what
we're going to do is instead of putting a hard time in the delay say like 250 or
500 we're just gonna type in wait so if we ever use it like several times we
could use the variable weight inside the parentheses of delay so we can change it
whenever we need to up on the top here instead of having to go and change it
several times down in the loop now as you can see right here on the LEDs I
have them blinking each one of them six times and an easy way to get that led to
blink six times is with a for loop and we can also do it with a while loop what
I'm gonna do is I'm gonna write a for loop for one of the LEDs and I'm gonna
write a while loop for the other LED and you'll see they're a little they're
constructed a little bit different but they do exactly the same thing going
down the line you're going to be using these not just the blinking LED but for
a lot of other different things while you're coding your sketch so this is
going to be some valuable information here on the for and the while and then
later on I'll put an if statement in there also so the first thing we're
gonna do is come over here in the in the loop and I'm gonna put I'm gonna put a
space in here between the the two first lines in there so we could separate
everything and see what's going on in here so first thing we're gonna do is
write four with the for statement okay how how we
set this up we have to initialize a and identify so it's going to be an integer
and long long time ago in coding the programmers used an i and i don't know
why they did it they used i and they use j for these type of statements here so
we're gonna follow that but you could call it anything i mean you don't have
to call it i you could give it any kind of name you want you could call it
baloney if you want to integer baloney equal one if you like but but weird to
make it easier and the condense space we're just going to say i is equal to 1
we're actually initializing what we're going to use okay now after the
semicolon we're going to put in another i i okay now what we want to do is we
want to make these leds make one of these LEDs blink six times so what we're
going to do is i is less than or equal to oops and my big fingers got an L in
error okay less than or equal to and BL and K that we used up on top there okay
and semicolon that is the condition we're using so we want to have our item
but we're calling I and we wanted to count six times so the blink is the six
so I is less than or equal to six now we're going to do one more thing in
there and that's going to be the increment and so how are we going to
write that now there's two different ways that we're going to
right it I'm going to do it one way in the fourth statement I'm going to do it
a different way in the wild statement but it's going to have the same results
so we're going to do I equal I plus 1 and what we're saying is every time that
we go through this statement we want to increment it one more time up until we
get up to blink which is 6 and that means that our statement is satisfied
and we'll go on to the next statement after the curly braces now you'll and
the curly braces are coming up here shortly so I got a equal I plus 1 and
that means that we are incrementing it one time now if we wanted to increment
it by two say we you know we have we want to count two or four or eight
whatever we want to put in there we could put that in there we could
increment it data amount of numbers it doesn't have to be one but for this
instance and for mostly every other instance where you're stepping through
something you'll want to do it with one unless there's a specific reason why you
want to do it with something other than one so we have four and we're
identifying we got integer I equal one okay then we have a semicolon then I as
less than or equal equal to the variable blink which is number six so I as less
than or equal to six we're saying that we want to do this as long as we're
lower than six and so and how we going to get to that six we're going to take
the I equal I which is one and then add a 1 to it every time that we step
through this statement here so we'll step through it six times
and then once we step through it six times that for statement is satisfied
and then we'll go on to what's after that for statement okay so that's what
we have right there so far so we need a curly brace so we'll put in two curly
braces okay and then what we'll do is we'll move this down a little bit so we
could put in what we want to happen here so what I want to do is I want to light
up my LED one but remember last time we did it analog and we controlled it by we
control the brightness by you doing a potentiometer we're going to be using a
potentiometer later but right now we're going to use the digital like we did
before and just call it high so what we're going to do is going to do a
digital right my LED high so
okay digital right my LED is high okay and then what we want to do is we want
to put a delay between the time that we have it high and low so we're going to
put in the Al a Y and then what we have for that was weight okay and then we'll
do it again led one comma and then low and then we'll put in another delay with
weight okay so what we have here were by doing that we only have to write the the
digital right high delay digital right low delay one time but we're going to
step through it six times with the for statement right there you see where I'm
getting that right here so that's why we have a for statement so we can condense
what we're writing in there we could write the for statement in there say
that we want to blink that and we're using blink because that's what we're
doing on here we're blinking the LED we could use any any other variable in
there it doesn't have to be blink it be anything else but this is what we're
doing in this demonstration right here we're blinking the LED we're gonna blink
it six times and once we're finished blinking that six times then we'll go
out below the curly braces right here okay right now we don't have anything
under the curly brace so what it would do is if I uploaded this right now it
would just continually blink because it would go through it six times
and once it was finished it was start counting again at one and and go through
that six again but what I would but what I want to show you we're going to use
the serial monitor in here okay and remember we did that last time to see
what the reading was of the potentiometer what I'm gonna do now is
put the serial monitor in there so we could see so you could so as we're going
through that step you could see it going one two three four five six and then
starting over one two three four five six
whenever you're troubleshooting or whenever you're going through your
sketches to test a value or a condition that you want this is an easy way to do
it you you could put your statements in there and then go back and put in the
statement so you could read on the serial monitor what's actually happening
and you could see what's actually happening because if you just wrote this
in here and was expecting it to blink six times and then stop it's not gonna
do it because there's nothing else written after that so you're gonna say
oh my goodness how come that's not doing it well I'll show you why it's not doing
it what we want to do now is put in a serial print line right here so we could
see what steps are being processed in this code
and what we want to do is we want to count the eyes in here
so let's upload this ok been uploading and we'll see that it steps one through
six and then goes through one through six again now it repeats the one through
six because there's nothing after the code that's in the for statement after
you have this curly bracket brace right here this is the curly brace for the for
statement it's counting through this code right here six times that there's
nothing after it so what it does it goes back up to the void loop again and
starts that all over again and starts counting one through six again now when
we put the wind statement in there that'll change all that we're going to
show it we're gonna move down just a little bit further here we're gonna go
I'm going to move that curly brace up a little bit and we'll come down to this
line here now we're going to do a while statement and while does exactly the
same thing as a a for statement it's just constructed a little bit
different and I'll show you that right here so what we're going to do is and in
a while statement we're gonna use a J and the same thing we're going to do
less than or equal to blnk okay then what we get and then we put a curly
brace and then end it and then we'll move this curly brace down and then see
this the while it's saying the same thing J is less than or equal to blink
the only difference about while is you can't initialize something inside the
statement so what we have to do we'll go up to the
top of the loop and right here we'll put it right there and we'll do integer J
equal 1 all right now that's the same thing that we did in the
for statement except in for you could do it within the confines of the
parentheses in the while statement you can't do it it has to you have to you
have to declare it before the while statement so that's the only thing
different about it and that well that's one thing different about it the second
thing that different about it is the way you step through it you put that at the
end of your commands so what we're doing here we have while and while J is less
than or equal to blink we'll do what's inside that so here again to make things
just a little bit easier we're gonna take this and don't do this every time
and we're gonna change something because instead of me writing for five minutes
watching you watching me type we're going to come down here but we're going
to change it to my led to you have my permission on this one to copy and paste
but we're changing it to my led to in here and that that'll make it a little
bit easier to go through this statement right here now I should I told you in
for that I was going to show you a different way to step through it that'll
do exactly the same thing so what we're going to do is just go J and plus plus
and what that and what that's telling us is every time we do a J
we're going to increment it one time because J is one we're just gonna
increment it one time so J plus plus does the same thing now we have our oops
I got a bracket instead of curling brace right there so we're gonna have to take
this up put that there and this up here
and I'm glad I cut that before I tried to upload it so we'll take a couple of
lines out of here or at least one line okay we'll make sure that that brace is
with that brace right there okay so we have the same thing we have the four
statement which we're telling it to blink six times and then once it's
finished blinking six times it'll go to the next statement below the for
statement which is a while statement which we're doing the same thing but
we're doing it with the while statement we're telling it to blink the other led
six times and when that LED is finished we'll go back up to the top of the loop
and start over again so what we're gonna do for us to see the difference between
the blink on the eye and the blink on a J is we're gonna add another line on
here sir okay we're we got serial.print (i) but if we put the same thing in for
J it's just gonna count one through six and one through six but we're not going
to know which one is which so here's here's another thing that we can do and
this is something that I haven't showed you yet
serial println and then what we're going to do is we're going to put a word
in there and we'll put in parentheses not parentheses quotation marks
okay and that's saying we want to add this on the print line exactly how it is
in between the quotation marks and so we're going to take we're gonna say I
equal and then we'll do a space and another quotation mark and then close
that out and hit the colon there's semicolon so what that's going
to do is it's going to say well I want to I don't want to do a print line I
want to keep it on the same line so what we're going to do is cereal that's not a
period okay cereal print and it's going to say I equal and then it's going to go
down to the next line and it's going to put the value in there so before we go
any further I'm going to show you to you and it's gonna do one through six it's
gonna say I equal 1 i equal 2 I equal 3 and then it's going to pause because
it's going through the other statement and you'll see when it does that the
other led is going to line up so let's get started with that right here and
then you'll see one LED blink six times and then the other LED will blink six
times okay now let's pull up the serial monitor
okay now you're seeing a pause there because it's going through the while
statement and it's starting over I equal six okay let's do the same thing with
the J so we put sereal print i and then that right there so let's just come
down here and we'll do the same thing and you have my permission to copy and
paste so this will go a little bit quicker here and we'll just take those
two lines right there and we'll put it right here
okay but we'll change this i to j and will let's clean this up a little bit and do
on a format so it looks a little bit better right there okay so now we have I
equal and whatever the value is and then it'll do J equal whatever the value is
so let's upload this and then we'll turn the serial monitor on and so you see I
and it's stepping through the I and J oops you'll notice when I put the print
statement outside the while loop that it only identifies J as one which is what
we declared it as in the integer J equal one so we have to put it inside the
while loop for it to step through so let's do that right now we'll take it
out of here
and put it in the wild statement like that okay let's clean this up a little
bit let's go up to here Auto formatted get back down there now now that we have
it in the wild statement then we should be good to go with the the counting on
it and there we go it's uploaded and then we'll take a look at it right there
and you can see it's stepping through the eye and then it's stepping through
the J it's counting one through six on both of them now we're gonna do it a
little bit something different we're going to show you an if statement
in here and what we're going to do is put the if statement up on the top right
here and we're just going to type in if
I'm do a parentheses value is greater than 128 okay is that what I get and
I'll have to put a curly brace right there
and then another curly brace I'll put it right here so that way if value is
greater than 128 and that's on a potentiometer then it will run this
right here the eye but if it's less than 128 it'll skip this right here and then
go down to the while so let's upload that and see what happens
okay we have both of them running right now so that means that that is greater
than 128 so let me turn this down and we'll see what happens
and so you see only one of them is running right now so what we're gonna do
we're gonna put another print statement in there up on the top here and put
cereal print now and we'll upload that and see what happens and what it should
do is tell us what the value is when we bring up the serial monitor okay so what
I need to do here I'm gonna put a print print line because it put it on the same
line as the one and it brought up a zero so you see what happens there now let's
open us up again and you could see zero it prints out a zero and it's only doing
a J so that means that whenever the potentiometer is set to zero it's not
it's not cycling through the eye so we'll bring the value up
and trying to get it around okay so now it's above 128 so you can see it's doing
both I and J so that's the if statement right there if the if the condition in
that statement is true then it will run let me move this out of the way if this
condition is true it'll run I it'll run this right here that's inside these
brackets right here if it's false it will go down to the wild statement it'll
completely ignore this statement right here so you see that's what the if
statement does if value is greater than 128 it will blink my led1 if it's less
than 128 it'll skip that and only blink
my led to so I'll bring this down just a hair and you can see that it's just it's
blinking my LED to constantly let me bring up the serial monitor you can see
it's set at 123 so it's lower than 128 so it's not allowing the eye to run it's
not allowing the for statement with the eye and there to run to make the my led
one blink so if we turn this back up
and we'll have both of them Lincoln now it'll go through the for statement and
then it'll go through the while statement so there's our if for and
while statements and he learned a little bit more about the serial monitor in
there now for easier viewing I added serial.print Val equal like I did with
the other ones so whenever we pull this up we could see value equal 163 and then
it does the eyes and then it does the J's and as we decrease the value we'll
bring the value down you can see the value is less than 128 so it only prints
out the J it only steps through it only steps through the J so we'll bring it
back up to above 128 and it also goes through the steps of the I and the J
well there you have the if for and while statements and up with the finished code
right here
so you could see it with all the comments in it and also we'll have it on
Tomstrainsandthings.com/Arduino as always so you could check it out on
my web page and since I don't have a Fritzing diagram for this one it's the
same as lesson 3 so you can just refer back to lesson 3 Fritzing diagram and
i'll just put a note in the notes for this lesson saying that you could look
back on to lesson 3 Fritzing diagram for the same thing now next week we're gonna
have we're gonna go a little bit further into these statements to see what we
could do with it and we're going to substitute the potentiometer with
another sensor and you'll see that sensor next week and you'll see why
we're going to use that sensor
look for that next Saturday so we'll see ya
Không có nhận xét nào:
Đăng nhận xét