How to Use Switch Case in Arduino Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 so here we go again we'll use a bite data type for this because we don't expect it to go above 255 and the original author named it something like inite and also use an integer but I think user input describes what we're doing here better so that's what I'm going to call it and by the way I'll post a link to the original sketch so you can compare if you'd like next we need to match the user input with one of the cases in the switch case statement so we can do something different depending on the character of the user types so we're going to get into our switch case statement for this and user input will be our test variable now before I write the rest of the cases I want to point something out notice the single quotes around the letter A this tells the compiler to get the asky value for the character for example lowercase a has an asky value of 97 and lowercase b is 98 if I leave the quotes off and just type the letter A I'll get an error so let's just demonstrate here and we're going to verify it okay and here we see we get an error that a was not declared in this scope because the compiler thinks that it is a variable let's put the quotes back and try again okay and we can see it work so the compiler was complaining because it thought a was a variable and we never declared it however I can use numbers like one 2 Etc and just demonstrate that we'll do that and that would work fine okay so this obviously is meaningless in the context of this progr so I'm going to put a back but we can see that numbers are fine if we're going to use characters we have to put them in quotes we don't need quotes for numbers because a number isn't a variable it's just a a number and the quotes tell the compiler again to look for the asky representation of that character so when I type A in there that's going to tell the sketch that I want to light the first led so let's finish building our switch case okay all done so we have a few different things that can happen depending on what the user types if the user types any of the letters a through e one of the LEDs will turn on but if they want all of them on then the user types F and of course we use the default case to turn them all off by typing any other character so let's give this thing a shot let me open this serial Monitor and so when I type a lowercase a we see the first led comes on and now if I type B the second one comes on so what happens if I type like I don't know like w or something well they go off well we can see since W isn't one of our predefined cases it drops into the default case and turns them all off okay let's try to turn them all on by sending an F okay cool they all come on as we would expect let's turn them off again I'll type in h you know maybe we could turn like two on at once or something let's try that I'll type A and B and send it okay so we were able to turn on the first and second led by sending both an A and B at the same time but you know did they really light up at exactly the same time so each time I send something it goes through this Loop it has to First Look for a character and notice we had two characters in serial buffer and remember when it reads one out they all shift down right so the second character becomes the first so it has to compare everything to the switch case find the appropriate one light the LED and then come back into the loop for the second character so technically they didn't really come on both at the same time it just goes through the loop really really fast so it looks like they come on at the same time but if you know I were to film this and slow my camera down really slow you would see that they actually come on at different times so you know what would be nice though if we could turn on a specific LED or that the user typed a lower case or uppercase letter you know just in case they had one too many or something I forgot to turn the caps lock key off so what can we do to make that happen here well all we need to do is add one line of code for each letter so we're just going to do that for the letter A so you could see what I mean and I made that one little change here so now when we send a lowercase or uppercase a the first led is going to light regardless so let's go ahead and upload it okay and we can see that that worked and we could you know we could actually put a third character it could be anything doesn't have to be the letter A but we're just going to stick with this right now and I'm going to open this serial monitor again and I'm going to type in uppercase a this time and of course the LED lights well you know that's pretty cool so by now now it should becoming more apparent about how you could use the Arduino to control our world and do some pretty cool things it just give you a lot to think about and and this really is just a tip of the iceberg this is still a relatively simple program even though it's probably the most or is the most complex one we've written so far so that wraps up this lesson and I'll see you in the next
Info
Channel: Circuit Crush
Views: 311
Rating: undefined out of 5
Keywords:
Id: 4b9_lxBAlHs
Channel Id: undefined
Length: 6min 51sec (411 seconds)
Published: Fri Apr 05 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.