Arduino Workshop – Chapter Three – SWITCH CASE Statements

If this than that logic, but if you have a number of different possible outcomes for a single event, then youll end up using a long list of if statements comparing the same values and thatll work. Just fine its, 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 youve 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 lets. 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 lets. 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 doesnt 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 lets 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 well go through and well create case. One we can do something here and break out of that now. What this means is that its not going to go on to case zero if something happens to change, goes on to case one again were 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 thats, 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 lets look at using something in a practical context. So Ive got the source code for the mini project here and its all pretty straightforward. Weve 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 its working so again, if you remember back to our section on using analog pins Im, 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 havent done before and Im connecting the middle pin up to a zero, which is our hot pin and happy now. Weve got that set up lets go through the code, so setting up up here now is normal. Initializing up to report nothing out of the ordinary there, then, what were doing is were taking port value and were assigning it to the analog read of type pin which, as we know, is 0 to 1023, its a 10 bit value and then were going to divide It by 255: why are we doing this well, its going to give us five different values? If we divide 1023 well, 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 lets go through and take a look at what were 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 weve entered our switch case here and a bubble. If you can see, weve got another if segment whats that all about well here were using another relational operator, which we looked at in some of the previous loops and were going to take a more in depth. Look at in the next chapter on operators. So what were 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 were 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 were going to be printing out some outcomes based on the value of the top. But if we didnt 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 weve 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 were going to print out very low in the case of one located to moderate, okay, so three high case of four extreme and then were using an extra thing here called default with that. Well, it is equivalent to the else statement from our ear statements, its saying if none of these are true, if it doesnt meet any of those, is to back up a contingency a default. If none of those is equal to half somethings 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 lets upload it and see how we go handy trick Im using the shortcut control: U or command? U, if youre on a mac to upload your code, which says we have to create the button, all the time lets open up a serial monitor so lets 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 theres 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 thats the range of our potentiometer, and that annoy is why its registering multiple Times is a little bit of noise in the actual voltages that are going on that so its between this threshold and its triggering back and forth its not a super Queen trigger and theres ways you can as methods you can do to get around that and to Filter that out further but thats not really the point of this.

We all lets go for looking at switch cases and its 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 thats a bit about different logic, statements, theres 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 weve covered so far and to finish off, the chapter were going to be looking at mathematical functions and operators and all those functions in general.

Share.
Exit mobile version