[adrotate banner=”7″]

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.


[adrotate banner=”1″]

arduino enum Video

[mam_video id=zui-gxK1DRc]



[adrotate banner=”2″]


[mam_tag id=5021]



[adrotate banner=”3″]


arduino enum news






[adrotate banner=”4″]



arduino enum Social



[adrotate banner=”5″]




👋 Want to bring Tony Stark-like gesture control to your projects? Learn how with the BLE-enabled MKR WiFi 1010 and Nano 33 BLE Sense boards using the ArduinoBLE library.

Arduin-yo ho ho! A fairground favorite, the pirate ship is a fun way to explore the oscillation of a pendulum. How much fun, you ask? Access our Science Kit Physics Lab preview and see for yourself: http://bit.ly/2oC6L0Q

🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today: http://bit.ly/2MnQ7fr


[adrotate banner=”6″]