Arduino MASTERCLASS | How to Use Control Structures PART 6
30.6 هزار بار بازدید -
2 سال پیش
-
🤩 FREE Arduino Crash Course
🤩 FREE Arduino Crash Course 👇👇
https://bit.ly/get_Arduino_skills
**Want to have some fun programming your own electronics?**
https://bit.ly/3OU3XDt
**Get the code, transcript, challenges, etc for this lesson on our website**
https://bit.ly/3zWxN5X
**We designed this circuit board for beginners!**
Kit-On-A-Shield: https://amzn.to/3lfWClU
FOLLOW US ELSEWHERE
---------------------------------------------------
Facebook: Facebook: ProgrammingElectronicsAcademy
Twitter: Twitter: ProgElecAcademy
Website: https://www.programmingelectronics.com/
________________________________
Arduino is a great tool for controlling electronic stuff like sensors and motors. But if you wanna build logic into your Arduino program, then you need to understand how to use control structures in your code. In this lesson, we are gonna go rapid fire over several super useful control structures, probably the most important ones. By the end of this lesson, you're gonna have an understanding of how they work and how to use them. Let's go.
Now this video is part of a series and where we left off was talking about variables. So if you haven't already, I highly recommend after you watch this video, going back and watching the previous lessons. So far, we've talked about how every Arduino program is laid out, then we talked about using variables we've gone over a little variable example. We sprinkled in a couple of these most important Arduino specific functions, namely pinMode and analogWrite. But now we're gonna start talking about these control structures. What is a control structure? A control structure allows you to adjust the flow of your code. If we go to the Arduino Reference page and we go to Structure, when I'm talking about control structures, I'm talking about these right here. So we have an if statement, fors, else, whiles, switch cases. These are the control structures that are going to allow you to have your code do different things depending on different circumstances. So let's go back to that code and we'll see we have this if statement, so this is some control code and notice it's followed by an opening and a closing parenthesis and inside this is called our condition. So if statements have a condition. If the condition evaluates to true, then the code inside the curly braces gets run. If the condition evaluates to false, then all the code in here just gets skipped over. So you only do the stuff inside the curly braces of an if statement if this is true. Now you might be looking at this and being like, "Wait, what is going on here?" Well, we've got two things going on. So this right here is actually an or symbol. So this condition is saying if the brightness variable is less than or equal to zero, or if the brightness variable is greater than or equal to 255, then do something. So, well, why don't we just evaluate? Let's pretend we're the Arduino and let's evaluate this. So what did we say? We said brightness was five. So if brightness is five, is five less than or equal to zero? Nope, it's not. Is five greater than or equal to 255? Nope, it's not. So that means we're just gonna skip over this line of code right here, the first time through the loop. So we've controlled our code flow, because we haven't used this based on a condition. So then we get to this next line of code and it says delay 30. Here's another one of those super important Arduino functions that you'll learn to love and hate. It's called delay and what it does is it stops the program in its tracks. No other code will run for the amount of time you put in here and this time is in milliseconds. So for 30 milliseconds, we're gonna say, "Stop. Don't do anything, Arduino board." And when we say, "Stop, don't do anything," what we're saying is, "Don't execute any more code," because what we did up here on line 13, when we PWM-ed pin nine to a value of zero, that means the LED was off, there's no brightness there and we're just gonna hold that position. So delay is like, "Hold it, hold it. Okay, go." That's what's going on here. So after the delay, we get to this final curly brace. Here's the end of this loop function and what do we do? We start back up at the top. So we're back at the top. We run into our friendly function, analogWrite, again, using the PWM feature of the microcontroller and what is brightness this time? Well, if you recall last time through, we changed brightness to five.
CONTINUED...
https://bit.ly/3zWxN5X
**About Us:**
This Arduino lesson was created by Programming Electronics Academy. We are an online education company who seeks to help people learn about electronics and programming through the ubiquitous Arduino development board.
**We have no affiliation whatsoever with Arduino LLC, other than we think they are cool.**
https://bit.ly/get_Arduino_skills
**Want to have some fun programming your own electronics?**
https://bit.ly/3OU3XDt
**Get the code, transcript, challenges, etc for this lesson on our website**
https://bit.ly/3zWxN5X
**We designed this circuit board for beginners!**
Kit-On-A-Shield: https://amzn.to/3lfWClU
FOLLOW US ELSEWHERE
---------------------------------------------------
Facebook: Facebook: ProgrammingElectronicsAcademy
Twitter: Twitter: ProgElecAcademy
Website: https://www.programmingelectronics.com/
________________________________
Arduino is a great tool for controlling electronic stuff like sensors and motors. But if you wanna build logic into your Arduino program, then you need to understand how to use control structures in your code. In this lesson, we are gonna go rapid fire over several super useful control structures, probably the most important ones. By the end of this lesson, you're gonna have an understanding of how they work and how to use them. Let's go.
Now this video is part of a series and where we left off was talking about variables. So if you haven't already, I highly recommend after you watch this video, going back and watching the previous lessons. So far, we've talked about how every Arduino program is laid out, then we talked about using variables we've gone over a little variable example. We sprinkled in a couple of these most important Arduino specific functions, namely pinMode and analogWrite. But now we're gonna start talking about these control structures. What is a control structure? A control structure allows you to adjust the flow of your code. If we go to the Arduino Reference page and we go to Structure, when I'm talking about control structures, I'm talking about these right here. So we have an if statement, fors, else, whiles, switch cases. These are the control structures that are going to allow you to have your code do different things depending on different circumstances. So let's go back to that code and we'll see we have this if statement, so this is some control code and notice it's followed by an opening and a closing parenthesis and inside this is called our condition. So if statements have a condition. If the condition evaluates to true, then the code inside the curly braces gets run. If the condition evaluates to false, then all the code in here just gets skipped over. So you only do the stuff inside the curly braces of an if statement if this is true. Now you might be looking at this and being like, "Wait, what is going on here?" Well, we've got two things going on. So this right here is actually an or symbol. So this condition is saying if the brightness variable is less than or equal to zero, or if the brightness variable is greater than or equal to 255, then do something. So, well, why don't we just evaluate? Let's pretend we're the Arduino and let's evaluate this. So what did we say? We said brightness was five. So if brightness is five, is five less than or equal to zero? Nope, it's not. Is five greater than or equal to 255? Nope, it's not. So that means we're just gonna skip over this line of code right here, the first time through the loop. So we've controlled our code flow, because we haven't used this based on a condition. So then we get to this next line of code and it says delay 30. Here's another one of those super important Arduino functions that you'll learn to love and hate. It's called delay and what it does is it stops the program in its tracks. No other code will run for the amount of time you put in here and this time is in milliseconds. So for 30 milliseconds, we're gonna say, "Stop. Don't do anything, Arduino board." And when we say, "Stop, don't do anything," what we're saying is, "Don't execute any more code," because what we did up here on line 13, when we PWM-ed pin nine to a value of zero, that means the LED was off, there's no brightness there and we're just gonna hold that position. So delay is like, "Hold it, hold it. Okay, go." That's what's going on here. So after the delay, we get to this final curly brace. Here's the end of this loop function and what do we do? We start back up at the top. So we're back at the top. We run into our friendly function, analogWrite, again, using the PWM feature of the microcontroller and what is brightness this time? Well, if you recall last time through, we changed brightness to five.
CONTINUED...
https://bit.ly/3zWxN5X
**About Us:**
This Arduino lesson was created by Programming Electronics Academy. We are an online education company who seeks to help people learn about electronics and programming through the ubiquitous Arduino development board.
**We have no affiliation whatsoever with Arduino LLC, other than we think they are cool.**
2 سال پیش
در تاریخ 1401/04/03 منتشر شده
است.
30,680
بـار بازدید شده