Arduino Workshop - Chapter Three - SWITCH CASE Statements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] so far we've looked at the most common types of loops and statements which you can use to accomplish many different projects but as with most things in programming there is usually a simpler way to do things if statements are so useful and creative to implementing if this than that logic but if you have a number of different possible outcomes for a single event then you'll end up using a long list of if statements comparing the same values and that'll work just fine it's not a problem but there are better ways to do it which brings us to using switch cases a switch case allows you to use a variable or value and create a series of outcomes or cases depending on the value results for example imagine that you've got a variable which could be any number between 0 and 9 and what you want is something different to happen for each possible value you could of course use a series of if statements which would look like this let's take a look at the Arduino IDE so you could go into your loop and create if some value is equal to 0 and do something break out of that if some value is equal to 1 break out of that and do something you would end up with a very convoluted list which is not what you want you want it to be elegant and simple or you could use a switch case which makes everything simpler and easier to understand to create a switch case you use the following syntax let's take a look so if we remove all of this we use a switch case much in the same way as an if statement your eyes switch and we put into brackets the variables that we like to compare so again we use some value in our brackets and we use the following syntax we use case and this is where it gets a little odd a little different we use case with a regular column so it double dot here enter and it doesn't recognize that as a new level so you want to styling purposes perhaps you want to hit tab and then you can create whatever it is you want and in case you you put your values so let's say again we use the example 0 the case Europe the case where some value is equal to zero you can do through something here and then much like our for loops and while loops we use great and that will exit that particular case and it can only ever perform or Excel neither equal one case at a time so we'll go through and we'll create case one we can do something here and break out of that now what this means is that it's not going to go on to case zero if something happens to change goes on to case one again we're using break which breaks out of this switch here now of course you could not use break and it will perform case zero and go on and check the others but generally that's not how you want to use which cases to break get out of this switch or performs whatever function it is you could jump to another section of your code perform a simple action and then break out of it switch cases are super useful so let's look at using something in a practical context so I've got the source code for the mini project here and it's all pretty straightforward we've got our pin definitions a global variable which were calling last type value which are going to keep track of whether our value has changed so again take the potentiometer and wire it up to our board so we can explain the code as if it's working so again if you remember back to our section on using analog pins I'm taking one pin connecting it up to ground making sure to keep my head out of the way X the other pin up to five volts nothing we haven't done before and I'm connecting the middle pin up to a zero which is our hot pin and happy now we've got that set up let's go through the code so setting up up here now is normal initializing up to report nothing out of the ordinary there then what we're doing is we're taking port value and we're assigning it to the analog read of type pin which as we know is 0 to 1023 it's a 10 bit value and then we're going to divide it by 255 why are we doing this well it's going to give us five different values if we divide 1023 we'll have one thousand 24 by 255 you will get five you get five different case of the week news from 0 to 4 remembering that let zero indexing them so let's go through and take a look at what we're doing so it divided by 255 which means the plot value will equal zero one two three or four really simple integer numbers so case zero so we've entered our switch case here and a bubble if you can see we've got another if segment what's that all about well here we're using another relational operator which we looked at in some of the previous loops and we're going to take a more in-depth look at in the next chapter on operators so what we're saying here is if plot value is not equal to you might have seen the double equal signs which means is equal to well is not equal to is a check and in the previous tutorial where we were using a total value we had it the other way around we had you read left to right is equal to not last top value but if we put the exclamation mark before this is where the syntax is really important we're saying if type values is not equal to and so you can read it actually makes sense when you read English what value is not equal to last spot values and the reason for this is we're going to be printing out some outcomes based on the value of the top but if we didn't have this in here is if we left it on the 1 value what would keep printing and keep going through that switch case but we only really want to know when it changes we want to know something different happens if it enters a new range which is why we've got that check in there and at the very end you can see that we update last what to equal what the current value is equal to now switch 0 we're going to print out very low in the case of one located to moderate okay so three high case of four extreme and then we're using an extra thing here called default with that well it is equivalent to the else statement from our ear statements it's saying if none of these are true if it doesn't meet any of those is to back up a contingency a default if none of those is equal to half something's gone wrong on our board and we want to know about it so if none of those are true at the very end then we print error and we break out of it quite a simple project with a lot of fun so let's upload it and see how we go handy trick I'm using the shortcut control U or command u if you're on a mac to upload your code which says we have to create the button all the time let's open up a serial monitor so let's try turning it all the way 0 sorry all the way up we can see that it goes to extreme now if we turn it back the other way there's a little bit of jail a little bit of noise in the channel that goes from high to moderate to low to very low and that's the range of our potentiometer and that annoy is why it's registering multiple times is a little bit of noise in the actual voltages that are going on that so it's between this threshold and it's triggering back and forth it's not a super Queen trigger and there's ways you can as methods you can do to get around that and to filter that out further but that's not really the point of this we all let's go for looking at switch cases and it's a really cool usage of taking a set of events and bypassing the need for long estate which which will just quite a rocky code make really hard to read the equivalent of that in if statements would take up perhaps 2 3 4 times in my turn depending on how you assignment so much cleaner and much simpler so that's a bit about different logic statements there's plenty more other things you can do such as do and try and different methods but most projects can be accomplished using a combination of what we've covered so far and to finish off the chapter we're going to be looking at mathematical functions and operators and all those functions in general how to create your own
Info
Channel: Core Electronics
Views: 116,435
Rating: 4.8871794 out of 5
Keywords: arduino switch case, how to use switch case arduino, arduino how to use switch case
Id: zui-gxK1DRc
Channel Id: undefined
Length: 8min 8sec (488 seconds)
Published: Tue Mar 07 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.