How to Use Switch Case in Arduino (+ Why You Totally Should)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody I hope you're doing awesome in this lesson we're going to take a lot of the stuff we learned and put it together to do something a little more complex and cooler than what we've done so far we're going to take our new serial Library functions we learned about last time and combine them with some of the control structures we've learned about in past lessons specifically we'll be using the if statement for Loop and our new friend the switch case statement now when I first decided to create a lesson which combined all the above I had to take a while to think about what we would do I wanted something that was not overly complicated since we're still kind of beginners but I didn't want something overly simple either I needed an idea that was sort of like the culmination of what we learned so far so I sat around thinking for a while then I hit the internet to search for a sketch that used a switch case along with other control structures I found one that met those criteria but also Ed the available and read functions from the serial library and that's why we cover those first and I'm a fan of full disclosure so I'll tell you that the sketch I found is actually on the Arduino website and was written by a guy named Tom AO or iGo and I don't know Tom but if Tom ever sees this I sure hope I pronounce his last name right and he has my apologies if I murdered it anyway don't think that this is just going to be a copy and paste exercise though we're going to be making a couple changes to Tom's sketch and we're going to type it out from scratch in the IDE so let's first take a quick look at the circuit we've seen stuff like this before so there's not a whole lot to say we have five LEDs and five 220 ohm resistors in series with the LEDs and here the way I have this drawn you can see that there's a gap on the breadboard that runs down the middle across the whole way and the all the little dots on the breadboard as we probably already know are connected vertically and that Gap is kind of like a break in the connection of those dots so it's very important that if you wire it this way and you could arrange the components how you want by the way but if you choose to go this route that the LED straddle that gap on the breadboard but anyway don't let the simple setup of the circuit for you I think you're going to enjoy this exercise the goal here is to have a user input a character into the serial Monitor and light up the appropriate LED or all of them based on the user input this is still kind of basic but gives us a neat little preview of how we can potentially use the Arduino to control our world and interface it to other devices before we dive into the IDE I want to take a second to think about the flow of the program we'll be writing and kind of visualize it since it's a little longer and more complex than anything we've done thus far what we see here is known as a flowchart as usual I know that some of you may have seen these before and others maybe not we're going to devote a whole lesson to project planning and design in the near future and we'll talk more about flowcharts and other design tools but for now just think of it as a graphic iCal representation of the flow and logic of a program let's go over this one really quick first we need to declare variables for our PIN numbers because we know that using literal hardcoded numbers like PIN numbers in our main program is like a mortal sin then we get into setup where we need to set up serial communication with the B rate and also set the pins as outputs next we get into the loop the first thing we need to do is check to see if there's anything in the serial buffer we use serial data available to do that if not we just kind of keep coming back to like a thousand times a second or something like that then when we finally find something in a Serial buffer we read it with serial. read and assign it to a variable then we fall into the switch case and do something based on the user input like turn on a certain LED or maybe turn them all on or all off after that we jump back up and wait for the next character to enter the serial buffer rinse and repeat okay so now we have a general idea of how the sketch is going to work and flow let's get into the IDE first thing to notice here is that I left the original creators comments up as I've said before it's always a good idea to start out with some comments that describe the sketch and give some information about it especially once you start doing more complex projects now in the interest of not being boring I usually don't do this myself but as the old adage goes do as I say not as I do and to keep things consistent I'm just going to add this comment revised by me and put my name there so let's do that okay let's do some coding now first thing we'll need to do which Tom didn't do in his original sketch is declare some variables for the PIN numbers so we don't need to hardcode literal numbers which is good programming practice so I'm going to leave a space and we're going to go ahead and do that and as usual we're using bytes to save program space so here we have our PIN numbers declared and that's okay but we're going to do something new that we haven't done yet we're going to use the const qualifier and what this does is it makes the variables read only so they can't be inadvertently changed later if you're familiar with C or C++ plus you may have done something similar using a pound define statement in Arduino you can use either but con preferred over pound Define and I'm going to suggest that you stick with that so okay let's go ahead and do this all right so now we want to declare variables for the first LED and last LED this again is so we can avoid hardcoding any numbers in the for Loops we're going to use and if you're still confused as to why we're doing this it'll become clear in a minute and it's important to knowe that before we go on for this to work the way we want it to our LEDs need to be in consecutive order without skipping any pins so for example we wouldn't like hook an LED up to pin two and then skip pin three or something like that so we have to use these consecutive pins or any pins that you pick doesn't matter the numbers but we're going with 2 through six next we need to initialize the LED pins as outputs and remember by default All Digital pins are inputs so it's important not to forget this step and if you try this and your sketch isn't working or the LEDs aren't lighting up that's definitely one thing you want to check first so let's go ahead and do that we're going to use a for Loop all right so finally we need to initialize serial Communications so we'll do that real quick and I think that's all we need to do in setup okay so if you remember our flowchart the first thing we need to do in Loop is check to see if there any btes of data in a Serial buffer if not we just keep checking until we get a bite we're going to use the available function coupled with an if statement
Info
Channel: Circuit Crush
Views: 864
Rating: undefined out of 5
Keywords: #arduino, #programming, #electronics
Id: PFLJQwRxqoc
Channel Id: undefined
Length: 7min 39sec (459 seconds)
Published: Fri Mar 22 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.