Arduino Uno R4 WiFi LESSON 15: Controlling LED Based on User Input From Serial Monitor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcarter with toptechboy domcom and we're here today with episode number 15 in our incredible nude tutorial Series where you're learning how to think like an engineer using the Arduino Uno R4 WiFi what I will need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee as always I want to give a shout out to our friends over at sunfounder sunfounder is actually sponsoring this most excellent series of video lessons and in this class we will be using the sunfounder elite Explorer kit now hopefully most of you guys already have your gear but if you don't look down in the description there is a link over to Amazon you can hop on over there and pick your kid up and believe me your life and my life are going to be a whole lot easier if we are working on identical Hardware but enough of the Shameless self-promotion let's jump in and talk about what I am going to teach you today I'm going to show you my solution to the homework assignment that I gave you in lesson number 14 but I must first ask were you successful if you were successful with the homework leave a comment down below I Am Legend double chest bump and if you are not successful leave a comment down below I fold it up like a cheap Walmart lawn chair okay now hopefully you were able to get it because I was really just combining a lot of the stuff that we had learned in earlier lessons what I'm trying to do is reinforce the learning something that I teach in an earlier lesson I want you to apply it in the next lesson so it really begins to stick in uh really begins to sink in and you're not just sort of just sitting and mindlessly doing what you see me doing I want you to really understand it okay what was the assignment the assignment was to control the brightness of an LED by getting input from the user on the serial Monitor and again we showed you how to do each one of those small things now we're going to put them together even if you were successful still watch the homework assignment because I want you to see how I'm doing it because I really want it to work well and by working well I mean I want it to smoothly turn up brightness sort of evenly throughout the whole range of the numbers that you input in the serial monitor remember you were supposed to go from 0 to 10 zero was off and 10 was full brightness and each number between 0er and 10 was supposed to smoothly increase the perceivable the perceivable brightness and so we'll talk a little about that as we get into it but let's jump over here to our circuit View and let me get out of your way and have a sip of coffee okay so we'll do this circuit's real easy we've done it for the last four or five lessons so I'm going to go through this a little bit quicker you need your red LED remember blue LED are only for special occasions so we're going to use the red one today and we have a 1000 Ohm resistor and we have a couple of wires now what I'm going to do is I'm going to turn that long leg of the LED up okay it is pointing in the upward Direction okay that being the up Direction now I'm going to come and I'm going to bring a control signal to this so I'm going to go in the same column the same column as the long leg of the LED same column as the long leg of the LED and then we're going to come over here to let's see what do we want to do how about how about pin n is that pin N9 no I think I missed it let me look to the side here yeah I missed it will come over over here 2 pin9 there you see I got that now you can see I have a little tape under this to try to hold it in place a little bit better then we've got the short leg of the LED here and we're going to get our th000 Ohm resistor and we are going to put it we are going to put one leg of it in the same one leg of it in the same column as the short leg of the LED so the short leg of the LED is to be plugged into the same column as one of the legs of the resistor and then the other leg of the resistor will just drop down here in a convenient spot notice that I cut those long legs off the resistor so that they make a little bit neater circuit build now I come to that other leg of the resistor and I got to bring this thing on back home and I do that by coming back to a ground pin yep want to make sure I didn't put it in the old 5vt pin that would not be good okay so there we are we are hooked up okay now we've got to think a little bit about how to do this so what are we going to have we're going to have a number between zero and 10 okay we're going to have a number between zero and 10 and if you just increased linearly right we're going to get that number from the user from the serial monitor we know how to do that now if we just increase linearly from 0 to 10 you might think well our write voltage is going to go from 0 to 255 so we might start by writing a zero if if zero is input a one would be 25 a two would be 50 a three would be 75 and just sort of Step Up in about 25 increments and then up to a 10 would be 255 right 255 divid by 10 is about 25 you could have 25 increments that you applied to your analog right now what's the problem with that the difference between between Z the difference between 0 and 25 is huge okay but the difference between 25 and 2 the difference between 225 and 255 that is going to be barely perceivable because it's already really bright so you're not going to be able to see that so do we want to increase that right value do we want to increase that analog right linearly no we want to increase it exponentially so with each increment we want to sort of double the right valve okay so how would we do that let's come over here and we really talked about this last time but I want to go through it again because I think it's something that is really important so do I want to do increments like 1 2 3 4 5 6 7 8 9 10 linearly no I don't with each increment of brightness I want to sort of double the signal that I'm sending to the LED so really what I want I want it to go something like this like 1 2 4 8 16 32 6428 and 256 now I understand we can't go to 256 we'll go to 255 but we'll we'll take care of that later the math is easier if we just say okay we're going to go from 1 to 256 six okay well how would we do that what is 1 1 is 2 raised to the 0 okay and look it up any number raised to the zero is a one okay a little bit counterintuitive but that is the fact now 2 raised to the 1 that's two by itself that would be two what is 2 raised to the two that's 4 2 raed to the 3 is 8 2 raed to 4 is 16 2 raed to 5 is 32 that is just a terrible five let me try that again 2 raised to the 5 that is not good either I'm getting obsessive compulsive here I've got to just move on with it uh perfect five okay 2 raised to the 6 is 64 2 raised to the 7th is 128 and 2 raised to the 8 is 256 if you don't believe me go put it in your calculator okay so that is what we have now what signal do we want to therefore what signal do we want to therefore send to the Leed well we want the right value we want the right value I'll just call it WV to be two raised to sum exponent okay two raised to Su exponent well now if IID told you to go from you know the brightness to be from 0 to 8 that would be easy why would that be easy because 8 would correspond to 256 and then I could just go 0 1 2 3 4 5 6 7 8 and it would have all worked out but what did I tell you to do I told you to go from zero being off to 10 being full brightness so I can't just say right value is two raised to the user input okay because two to the 10th would be way more than 256 that's not going to work so what do I have to do I have to do a little maap so what I need to do is I need to come in and I'm going to have some user what is this I'm going to have some user input user input and that is going to go between zero to 10 it's going to go from 0 to 10 now what do I need to calculate I need to calculate an XP so what I can see is the X x p it needs to go the exponent it needs to go from zero 28 because 2 to the 8 is 256 and again we want 255 we take care of that later but just hang in there with me hang in there with me so what do I what do I have I'm going to have UI what do I need I need X p and then that XP will be plugged in here and then from that I can get right value okay so what do I have well let's go in and who is your friend Mr line so let's create a point if my user input if my user says they want a brightness of zero what right value would I want I would want to write value of zero I would want it to be off again we're going to have to subtract ra one in a minute but just hang in there with me now what else do we know what's the largest number that the user would put in the largest number that the user would put in would be 10 and if he puts in a 10 what do I want the XP to be I want it to be an8 because if the XP is 8 then 2 to the eth will give me a right value of 256 does that make sense so what do I need I need the equation of line with the with these two points an equation of a line with these two points well m is equal to Y 2 - y1 over X2 - X1 like that okay and now Y2 is 8 - 0 over x 2 which is 10 - 0 and so m is equal to what what 8 over 10 m is equal to 8 over 10 does that make sense okay now we need an equation of the line y - y1 is equal to M on to x - X1 and y1 is 0er so I just end up with Y is equal to M which is 8 over 10 * x - X1 which is 0er and so what do I have I have y is = 8 / 10 * X now let's put in our variables what is y y that we're trying to calculate is this XP up here so what do I say XP is equal to 8 over 10 of X and what was X that's the number that is the number that the user gave me that is us user input so the exponent XP is 8 / 10times the user interf the user input okay now is it that I want XP no what do I really want I really want WR Val and I'll go ahead and write it out so right Val is equal to right we already got that equation there two to the XP and what is XP it is 8 / 10 * the user input like that okay so I'm going to say what brightness do you want he's going to say 10 10 is going to go into UI 10 / 10 is 1 * 8 is is 8 okay 2 to the 8 is what it's 256 now what did I really want right Val what what I really wanted was 2 55 so I have to subtract one also if he input zero if he input zero up here 2 to the 0o is one but zero should be off so what do I want I want one to go to zero so in both cases what do I want on this equation this whole equation I'm going to subtract one and then everything should work so now what does that mean each increment is I go from 0er to 1 to two to 3 to four to 5 to six to seven to 8 to 9 to 10 each time the with each increment the brightness of the led the right value is going to double but to our eye it's going to look nice and linear now I hope that makes sense let's jump in and see if we can get this thing coated up now I think that was the hard part right there that's a hard part behind us but this is really very similar to what we did in that last uh dimable LED with the potentiometer but I wanted do it again because I want you to see how do you can become to become you can begin to become comfortable with the exponents so let's hop on over here we have got our circuit hooked up we are going to come in and we are going to open up we are going to go to examples we're going to go to Basics and you're going to open bare minimum now we're always going to check and make sure that we are connected to our board our board for me is Comm 5 because that says Arduino Uno R4 WiFi you might not be Comm 5 you pick the Comm that uh the Arduino shows up on for me it's Comm five okay now we're going to come up here and we're going to jump in and we're going to start doing a few things okay so let's define our variables we F well first of all I'm going to have the integer the user input right what brightness does he want so instead of of user interface let's call that bright okay the brightness because that's actually what it is that is the brightness that they want okay uh we did the math with UI so let's let's call it UI so UI is the user input that is the brightness that they want and we're not going to put a value in we're just going to declare it because we will get UI we will get the value of it from the user okay now what else do we need well we're going to change that UI into a what right Bell and that's what we're going to send to the LED and we will calculate that from UI so we're not going to put anything in there now we're going to be talking to the LED so we better set up a pen so I'm going to say int and it is going to be red pin and then we're going to give it a value and I always like to be very careful to make sure that I am putting the right one in and what did we say that was on pin nine so the red pin is pin nine that looks good okay and then uh we're going to go ahead and set up our B rate right so the B rate is going to be 9600 like that let's put in a little bit of a delay so I'm going to put in an INT I'm going to put a weight time is equal to 100 milliseconds that's a tenth of a second and then also we're going to get input from the user so we're going to have to what ask wait read to ask we have to print a prompt to them and so let's go ahead and set that prompt up up here I know I've been bad about putting them down below but let's go ahead and set up the prompt here so I'll just call it prompt you can call yours whatever you want but I'm going to say prompt and then that's going to be equal to it's a what a string so I put it in double quotes please I like to be polite I think that these Arduino programs they should be polite with the user please input desired brightness and then I'm going to prompt him it needs to be 0 to 10 like that okay that looks good 0 to 10 close that close the quote Mr semicolon okay so now I think most of that is set up so now we're going to come here and we're going to be getting we're going to be getting values from the user from the serial monitor so I better initialize the serial port and the serial monitor with a Serial do begin 9600 no no no no no we don't use constants we use what our variable which was BR okay that looks good so we have set that up now we're going to come down to the void Loop now what are we going to do in the void loop we're going to get input from the user we're going to get input from the user okay and then we are going to change that user input into a right Val and then we're going to analog right that right Val so if we're going to get input from the user what three things do we have to do ask weit read so let's ask serial.println and then I've already made my prompt so I just got to put prompt in there okay and then it will ask them for the brightness now what do we do ask weit how do we wait we make a while loop and then we put a condition we wait as long as what's in those parentheses are true and what are we looking for serial. available serial. available as long as that equal equals zero that means that there is nothing there and if there is nothing there then what do we want to do we want to wait so we just create a Clause I'm going to open a curly and then it goes ahead and closes that Curly for me and then I'm going to click enter I just like to have it where I can see it a little better okay if you didn't hit that return and you just said open closed it's still going to work but I have a claw between the open and close curly and what happens in that claw absolutely nothing but it just sits and Loops as long as there's no data in serial dot available okay talked about that last time so I hope that's starting to make sense now so we have asked we wait now what do we do we read and so now we are going to say and what did we call that we called that UI for user input UI user input is going to be equal to what analog read and what do I analog read no no no we don't analog read what are you thinking we don't analog read read tell me what we do we get it from the serial. parse int and then an open close like that serial parcent open close now I have UI now I have to calculate I have to calculate the right value using this equation here I have to use this equation here and we learned how to do powers in the last few lessons so okay so now what I'm going to do here is I'm going to get back over here where you can see what I'm doing I'm going to calculate right Val now let's make sure we declared right Val yeah I always just like to make sure that I'm writing the variable name down right right Val is going to be equal to if we look at that equation it's a power right we're doing a power so it's going to be p w to do the power and we're going to going to raise what number to a power remember we were raising two and then what were we going to raise it to well it was going to be 8 divided by 10 okay 8 8 ided by 10 I close that parentheses times what times UI okay now my parentheses look right there and now I close the Divide open 8 ID 10 Clos now I still got to close that power that P so that looks right 8 divided 10 * UI I'm not going to deliberately make this mistake again but 8 divided by 10 is what it is8 and 8 is an integer 10 is an integer and 08 is a float and so integer divided by integer is going to give you an integer it would drop it to zero and your program wouldn't work so we're going to put an 8 point and a 10o and then that would be a floating point8 and everything's going to work great and then remember we have to subtract one so 256 goes to 255 which we want and one goes to zero which we want and now it should work on all ranges so that looks pretty darn good so that is the right Val okay now what do we do we need to analog write that where are we going to write it to the red pin and then what are we going to write we're going to write Val like that and then like that okay now I probably made some mistakes in here you guys probably saw them but let's go ahead and run it and just see what we end up with and uh let's see here I kind of I got to resize this a little bit okay that looks that looks good okay so I think I think you can see everything there so now let's run this thing and we're hoping for no ugly look oh that ugly orange serial. print line prompt what is that Pro promp promp promp we've got to spell right p r o m PT you could spell either way but you just got to do it the same in both places okay so it's running done compiling no little ugly orange business and now it's downloading and boom okay so now what do I need to do I need to turn on the serial monitor here now why am I not seeing a prompt I am not seeing a prompt because when it's sent the prompt the serial monitor was not open so it had no place to go to so I'm going to come over here and and press this happy little button and it'll start the program again ah okay now if I type in zero what do I expect well if I type in zero zero should be off and so the LED should not come on so I'm going to type in zero okay and boom the good news is the LED did not come on well what if I type in one okay I'm going to type in one and click enter and I really can't see that that came on I really can't see that that came on well let's see what if I put in a 10 what if I put in a 10 okay and let's hit enter okay so I put in a 10 okay it came on but darn you know it's not very bright it's kind of dim for a 10 that's not full brightness so what should I do well let's let's first of all look and see what right Val is so what I'm going to do here I'm going to try to debug this and if you guys see my mistake go ahead and leave a comment because maybe some of you guys saw it as I went through this but there's something definitely wrong with my program and if you caught it leave a comment but I'll show you how we debug this well the first thing I want to know is serial.println what is Right Val I might have read UI wrong I might have calculated right Val wrong there's so many things that could have gone wrong so I'm going to print right Val and I'm going to see if I'm getting numbers that I would expect okay so I'm going to download that okay that looks good and so now what I'm going to do is I'm going to say if I have a zero all right what does it say it says the right p is zero which is what we would want want well what if I say the brightness is 10 what is the right Val it says 255 that looks good well what if I said nine it should be about half of 255 I think and you know 146 that's uh that's pretty uh that's pretty good let's say that I did a two okay a two becomes a two so you see this is this is really I think looking pretty good so I I think this is really this is really looking pretty good so what else could be wrong well one thing that could be wrong is am I on the right pin I said pen n and I look here yeah I'm connected to pen n the other thing is does pen N9 have a squiggly yes pin n has a squiggly what is the other thing ah what did I forget in the void setup who is your friend pin mode and what did did I not do I didn't say that red pin was an output okay and because of that I did not get very bright numbers and so if you do not do your PIN modes those pins are going to behave in unexpected ways those pins are going to behave in unexpected ways so you've got to do your PIN modes okay so let's run this again and there it goes okay now let's try it so let's try again I'm going to put in a zero okay and zero is off I'm going to put in a one okay and then with a one a one still stays at zero so a one why would that be well two raised to the8 minus one it still doesn't get to one so that kind of makes sense I'm hoping here pretty soon I will see a number so I'm going to put in two okay and with two I get two that looks good and what do I see here at two I can see the LED came on then I'm going to go three okay and I saw it come up in brightness a little bit I'm going to go four perceivably brighter okay five okay you guys could see that six saw that seven saw that eight saw that nine saw that and now 10 let's look at 10 boom saw that full brightness so what did you see you saw a perceptible increase in brightness all the way from 0 to 10 because we did the math to match how your eyes perceive light and we didn't do the simpleminded thing of just doing it linearly because we would have seen a difference between 0 and one and 1 and two and maybe four and five but then we would not have seen a difference between seven and 8 or 8 and N or nine and 10 because it starts kind of saturating your eyes and you're not going to perceive that okay guys we got this problem done and hopefully you guys were able to do this on your own what your homework assignment is is that if you did not do it with this exponential fashion go ahead and do it and try to do it from your notes without just copying me step by step I want you to see if you kind of start with a blank bare minimum can you do this by yourself and then make a quick video post it to YouTube in your description link back to this video and then in the comments down below leave a comment pointing to your homework solution so I can go over there and see if you guys know what you are doing if you're actually learning this stuff because if you're not picking it up then I need to really understand that so that I can maybe try to be more clear on things okay so you're going to do that what I'm trying to do is I'm trying to teach you a little hardware and then teach you a little software teach a little Hardware teach a little software so we've been working with the LED that's going pretty well what we're going to do next week is I'm going to teach you about conditionals like right now the program just sits and runs it just Loops in the loop and there's no decision making point in there what I'm going to show you next next week is how to do conditional sort of like well if the number is lower than this you want to do one thing if it's greater than this you want to do this other thing so what we're going to be looking at is if statements and if Clauses and the way conditionals work with if statements and if Clauses okay then after that we'll probably start working on some additional Hardware so we're going to learn a little software little coding we're going to add some more complicated Hardware little bit more complicated software programming a little bit more complicated Hardware because if I just sat and taught you all the coding you would kind of get bored if I just sat and taught you the hardware then you're not going to have the programming skills to do it so we're going to just try to step this thing up kind of little by little okay guys hope hopefully you're enjoying this tutorial series always want to give a big thank you to you guys who are supporting me at patreon uh it's your support and encouragement that is keeping this great content coming you can also help me by giving me a thumbs up or leaving a comment down below if you have not already subscribed to the channel when you do make sure you ring that Bell so you'll get notifications When Future lessons drop and most importantly share this video with other people because the world needs more people thinking like an engineer and fewer people sitting around watching silly cat videos Paul mcarter with top Tech boy.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 3,279
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: d-Ma3u7GngA
Channel Id: undefined
Length: 33min 0sec (1980 seconds)
Published: Thu May 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.