Arduino Tutorial 29: Using Push Buttons to Create Dimmable LED

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and we are here today with lesson number 29 and our new our improved our most excellent Arduino tutorials and what we're going to learn today in lesson number 29 is we're gonna learn a little bit more about using a push-button switch as an input device on your projects we learned the basics of operation for a push-button switch in Lesson number 27 where we could turn an LED on that as long as the button was held down the LED was on when the button was LED up the LED went off this was pretty simple but in that lesson we learned the concept of how to use pull-up and pulldown resistors with a switch then in Lesson number 28 we went a little further to make a little more interesting switch a toggle switch where if you press the button and release the LED would come on press the button and release again and the LED would go off a very similar hardware and circuit but we had to think a little bit more about the coding in the programming and today we will take it to the next level and what's your assignment today is I will need you to build a circuit with two push buttons in an LED that is you press one of the push buttons either holding it continuously or pressing it repeatedly the LED will break out very gently get brighter and brighter and brighter and then if you press repeatedly or press and hold the other LED the other button the LED will gradually get dimmer so one button will gradually bring up the brightness the other button will gradually bring down the brightness of the LED so I will need you to pour yourself a most excellent cup of iced coffee I will need you to get out your EE Lego super starter kit if you don't have one check out the description click on the link 35 bucks you get a boatload of components and this is the kit that I use for all of the lessons in this series and so if you get that you'll have identical hardware to me and if we're working on the same hardware it will be a little bit easier for you to follow along with me okay let's get started on this project and I will get out of your way and then we will switch over to our overhead view and what I need you to get out for this project is you will need the Arduino Uno you will need the two you will need to push button switches out of your kit you will need two 10k pull-up resistors you will need a red LED remember we save the green and the blue LEDs for special occasions not going to use them today we're going to use the red one need a 330 ohm current-limiting resistor you will need your buzzer and you will need your breadboard okay so here's how the assignment is going to go you're gonna go away you're gonna hook the circuit up and then you're gonna write your code and you're gonna make it work and then you're gonna post a comment down below in the comment saying hey I did it by myself without watching what you did or you're gonna say hey I just really got stuck and I had to watch what you did or you're gonna say I did it by myself but I did it a different way than than what you did and so let's get some discussion going down there because there's a lot of different ways to do this and I will show you one go away and do it yourself then come back and do it with me or watch me okay go away pause the video then come back okay did you get it working did you get it working all right well I'll go through it with you and I'm just going to remind you to use a switch you've got to have a pull-up resistor that connects to one pin of the switch and then you've got to take that connection and go out to a digital pin on the Arduino and you will read from that pin okay you need to have the other leg of the switch hooked up to ground and then the led of course you know how to hook that up 330 own current-limiting resistor in series with the LED with the long leg of the LED always pointing back towards the voltage source in this picture I'm using pin 12 it's the Reid pin from the switch in pin 8 for the led and then remember you're going to need to build two of these also remember that if you look at your switch there's pins that are sort of pointing towards each other curve towards each other those pins that are curved towards each other need to go in a common column so those two pins that sort of curve towards each other go to a common column and then the switching occurs between these pins which are not going to be in a common column and so that's where your switching is going to occur that's where your switching is going to hurt hopefully you understand that if you don't know what I'm talking about go back and watch Lesson number 27 I can't slow down at this point alright so we're gonna start by building our switch and you can see here that those pins are the ones that are curved towards each other and so I'm going to put those in a common column I'm going to start like over here okay now I will need did that go in yeah I believe that went in okay now I am going to need my pull-up resistor as such okay and I'm going to go ahead and get this one going to so where the where the pins are pointing towards each other I will have those pins in a common column if possible these are kind of hard to plug in this little dip the little switches are always kind of hard to get in a breadboard but I will say the ones with the e Lego kit work better than a lot that I have tried to do alright now I'm going to put my pull-up resistor again same column as one of the legs of the switch sometimes these things are hard to get in there and this particular resistor doesn't want to go okay so now do you see that I have two switches and I have two pull-up resistors now I need to go to five volts what is my problem my problem is my Arduino only has one 5 volt pen coming out so what I'm going to do I'm going to connect to that 5 volts and I'm going to bring that down here to row number 2 which is the positive role and I am creating the positive Row the row that's marked positive and now I've created a rail so that all of these holes in Row 2 have 5 volts so now all I got to do is just grab off of that 5 volts these things do not want to plug in today sometimes when you have a new breadboard they're kind of tight to begin with ok there I am in so now I'm connected to 5 volts from the second row and now I'm connecting that to my full up resistor I will need to do a very similar thing over here with the second pull-up resistor that second pull-up resistor will be connected to 5 volts as well now those two switches will need to be connected to ground so I will take the bottom leg of the switch and I'm going to connect it to GND by the 5 volts and I don't have to create a ground rail because I do have three grounds and that's how many I need and if I don't have to create a ground rail I don't all right now I will come and take that switch to ground so now I pretty much have that built but remember I do need to read off so I've got this much hooked up but now I need to read off that node that connects the resistor to the switch I need to take that I'm going to take the first one to pin 12 so right there write that connection between the switch and the resistor I will take to pin 12 and I will be reading from pin 12 and then on the second one the second switch similarly I will take that node that connects the switch to the resistor let me get this where you can see it a little better okay that node that connects the switch to the resistor and I will take that to pin 11 so on pin 11 I will be reading a reading from the second switch I have my grounds I have my read pens I have my five volts so that one's all done now what we need to do is we need to ah you know what I am gonna need a ground rail because I forgot about the Buzzard so change of plans I am going to take my ground wires out I'm going to come from one G and D on the Arduino and I am going to come to row one right I'm going to come to Row 1 on the breadboard and that Row 1 now that entire Row 1 is going to be ground so now I got to do is just connect to Row 1 and I will be grounded connect that to Row 1 my second switch to Row 1 it's grounded and then my first switch to Row 1 my first switch to Row 1 and it's grounded so I do indeed believe that I have this all hooked up now the the two switches with the pull-up resistors now we need to go to the led so we will start with our current limiting resistor I'll go from like column 25 to column 30 and then long leg of the led into the lower leg of the resistor and then I need to ground the LED so then the short leg will go to row number 1 up here remember Row 1 is my ground rail so now that's connected and now I simply need to come over to the top leg of the current limiting resistor that's the left leg and then I think that I will bring that to pin 3 now since we are going to be controlling the brightness of the LED we will need to use one of the digital pins that has a squiggly bite that would be 3 5 6 or 9 as far as the ones that we still have available and I chose to use 3 okay so that is looking good and now we still need to put our buzzer I am using the active buzzer in the e Lego kit that is the one that has the black on the bottom remember that these matter it matters on these buzzers as far as the polarity and so I've got to make sure that I am connecting the positive the positive and I am running out of wires can you believe that ok I'm going to connect the positive pin and this is just going to be an on or off so I'll just use any o digital pin so I'll say 10 2 and then I need to ground the other side of this so I will come here to the other leg of the buzzer and I will take it to my ground real like that ok that looks pretty darn good I hope that you could I hope that you could follow that guys you should be able to do this because I have done all of these in earlier videos and if you're taking these in order you should sort of know I'm talking about and I can't just take an hour to fully explain every single step on every one of these on every one of these videos otherwise they would get very long and very boring and I'm already getting people complaining that they are too long and too boring so let's let's start coding this thing and because this is a little bit more of a you know a little bit more complicated let's see I think we will go to this view and that looks pretty good I might make this a little bit wider let's see how that looks give me a second to get this all straight okay that I think looks pretty good alright so first thing I want to do is I want to work over here on these buttons and just see if we can get these buttons working so how do I need to do that well I've got to set up some pins I've got to read pins and so I'm gonna have a int and I'm gonna have button 1 I'll say button pin 1 and you can see that as you start getting more complicated in these things you need to start kind of being really descriptive on what your variables are so button pin one that one I believe was hooked to pin 12 so we'll say that that's 12 that looks good then in it button in 2 and that was connected to what that was connected to 11 all right and now let's go ahead and do our pin modes and so we'll come down to void setup and I will say pin mode button 10 1 is a what it's an input we're gonna be reading from that and then pin mode but 10 pin 2 similarly that's an input happy little blue color because it recognizes the word that's always good recognized pinmode now when we come down here I just want to I just want to read the two values so I'm gonna say if I'm gonna read them I better have some variables for that so I'm gonna have int and it's gonna be button Val one no value in it because it's going to read it in an int button Val - so now in my void loop I will read those two values and so it is going to be button Val 1 is equal to digital read and what am I gonna read button Val 1 off of button 10 1 does that make sense so now I've got a value for is the first button up or down if it's just sitting there up it should read a 1 if I press the button it should read a 0 so now I'm gonna go button Val 2 is equal to a digital read of button 10 - so I'm just trying to get my buttons working before I write a whole bunch of big long thing of code so let's just get the buttons working let me come over here to this so now let's see what we're gonna we're gonna get I'm gonna I'm going to print this out and I'm gonna give a series of prints if I'm going to print I better put my cereal again up here and the void setup serial dot begin trusty old 9600 now I should be able to print and I'm gonna do kind of a string of prints here so I'm gonna say serial dot dot print L in and what do I want to print I'm gonna print button time 1 1 equals like that and then I'm gonna cut and that should be serial print because I want to string these things across so then let me copy that so I can kind of start going a little faster control V and then what I'm going to do is serial print and then I want to print what I read what did I read I read button Val 1 and then I think that we would sort of repeat this I hope I've got an undo now let me just build this let me kind of keep building it now I'm gonna do a serial dot print and I'm gonna put a comma need to get my formatting nice okay now I'm gonna do the same thing on the same line for button Val too but I can control C and control V those two and then it's going to say button 2 is equal to button Val - okay did you guys follow that button Val - all right so then this should print out what state these two buttons are in and so let's just go ahead and download that and see if this is looking sorry let's see if this is looking good hold your breath oh what's wrong with button Val 1 what is that button pin 1 ah I said button Val 1 and button bout 1 were you guys yelling at me but about one button Val - alright download mmm what is not happy here cereal oh man I am a horrible horrible speller and then to make it worse I copied and pasted we have to spell serial correctly serial sometimes I make mistakes on purpose and sometimes I'm just not paying attention ok those look good in this one I miss builders well sir real okay now they're all the happy little orange color so let's look at that okay boom so let's see okay so here is I'll show you the code here I'll show you the serial monitor okay do you see what the do you see what the problem is I never put a serial print line over here so it's just going across so I really need to put a print L in at the end so it will go to the next line and also it's going much too fast so I need to put a delay of DT as delay time and then I need to go up into fine delay time so I will come up here and I will say int DT delay time equals let's say 250 to slow this down some so that we can see it let's try to download that again okay and I'm going to try to make this bigger okay and I think you can see that and I do need to get out of your way a little bit so this we can see that something is wrong because it's saying button 1 is equal to 1 what we would expect in button 2 is equal to zero which is not what we should expect and so I might have a problem on the circuit because right in the up position it should not in the up position oh look at this craziness look at this craziness what did i do on the circuit were you guys yelling at me were you screaming at me what did i do on the circuit I never came over Earl wait a minute yes I did okay so that does go to 1011 and I say button pin 2 is pin 11 so that looks good button Val one is digital read button 10 one button Val Oh what should this have been right here oh you can't see me okay I come back over here okay I found my mistake you see I said button Val one is digital read button pin one in button Val one again that should have been what button Val - all right so now let me come back to a good view here and let me download it again and see if that will fix it okay boom do you see how I'm getting button 1 is a 1 in button 2 is a 1 and that's what you would a suspect now I'm going to press button 1 what would I expect I would expect button 1 to go to 0 boom look at that and then I let it up and then I press it and then I let it up and then I press it ok now button 2 let's see if it works when I press button 2 what should happen should go to 0 comes back goes to 0 boom okay do you see how much easier my life is going to be now because what do I know I know my two buttons are working and I know that I have a button Val 1 I have a button valve - and button valve one tells me is button one being pressed button Val two tells me is but two being pressed most excellent so now I'm getting good data now I just have to figure out what to do with those values and so I want to control the brightness of the LED beam so I probably need what I probably need a new variable up here that we are going to call int LED bright and I'll just start it at zero so that it would start in kind of the off position okay it would start uh put in a zero there it would start in the off position now I'm gonna come down here and the first thing that I will do is I will hmm I will just go ahead and at the very bottom I will do a analog right and what do I want to analog right LED led right like that so led bright is zero and so after I go through the loop the last thing that I'm going to do in the loop is apply the desired brightness to the LED but what I got to figure out is if if button one is pressed I want to do what if button one is pressed what do I want to do I want to make the LED brighter so I'm gonna say if button Val one and put my parentheses around there parenthesis if button what pal one equal equal what zero that means that button one is being pressed what do I want to do I'm gonna say well bright I forget what I call that led bright LED bright is that how I did it yeah that I did it up here yeah okay led bright is equal to let's say led bright plus five and so if the button is down it's gonna kick up the brightness a little bit alright but what have I got to think about I don't want to help a lot I think I will deal with that later okay so if the button one is pressed I'm gonna kick up brightness a little bit and then how about this if button Val two equal equal zero what does that mean well that means that button number two has been pressed and in that case what do I want to do in that case I want to say led bright is equal to led bright minus five right I want it to get a little dimmer and then what it will do is it will then write that to the LED and hence it will be changing the brightness now let me see did I do my pin mode oh I didn't do my pin mode on the LED so I need to make sure that I do that and B mode and I don't even have an LED pin so I need to do that too so I need to put int LED 10 and we're gonna set that to three because it was connected to pin three in the circuit so now I have an LED pin now I've got to do a pinmode on LED pen and that is going to be an output all right so you see I was coding up the buttons and then I forgot to do some of the stuff that I needed to do on the LED and similarly we're going to have to go back later and we're going to have to put some work in on the we're gonna have to put some work in on the buzzer but for right now we have the LED connected and so here we go here we go all right now what else do I want to do though is I've got to be really careful because I don't want to write something that is greater than 255 and I don't want to write something that is less than zero so I'm gonna say if LED bright is greater than 255 what do I do if LED bright is greater than 255 well I'm gonna say led bright equal to 55 so if you're just sitting on that button and you it and you got it above 255 it's gonna come back and it's gonna it's gonna put a it's gonna put a ceiling at 255 so you're not going to be able to go above 255 and then similarly if you're sitting on button 2 and led bright is less than 0 what do you want to do there where you want to say LED right equals 0 and so what this makes sure is is this - if statements make sure that you never try to apply a signal greater than 255 to the LED and you never try to apply a signal less than 0 all right this is going to be kind of interesting to me because I think that it might work alright so let's download it here Oh LED bright what is wrong with led bright equals 0 it doesn't need a closed parenthesis there that was quite foolish right quite foolish let's try again and analog right LED bright so now let's make sure this parenthesis goes or that curly bracket goes with that one that curly bracket goes with that when that curly bracket goes with that one that curly bracket goes with that one the void loop ends there you see I'm just making sure that my I'm making sure that all of my I'm making sure that all of my curly brackets line up so it doesn't like my analog right why does it not like analog right ah I've got to give it a value so I'm gonna analog right I got to tell it what I'm gonna analog right to LED pin and did I do that right LED pin yeah so I'm gonna write to LED pin what am I gonna write LED bright now when I download this this time it is going to be happy okay it's gonna be happy and look the numbers are coming up good both of them are up both buttons are up and so now I'm gonna try to give a little brightness by hitting button one ah look at that did you see it came on just a little bit do you see it's getting brighter as I either hold it boom this thing is getting brighter and brighter and brighter okay all the way to full brightness okay half of it is working but now can we bring it back down with button to okay it is coming down it is coming down it is getting dimmer and dimmer and dimmer and it is off okay so I can all the brightness with these two buttons okay what is the one thing I still need to do like if I just sit on this brighter button I need to give an audible feedback to the person like hey buddy stop hitting the UP button you're all the way up so where would be the best place to do that the best place to do that would be if LED brightness is greater than 255 issue a warning and so we are going to need to turn pin 2 on because pin 2 is what the buzzer is up to so now I will say int buzz pin is equal to pin 2 and then I'll have to do a lot on buzz pin I'll have to do a pen mode pen mode and then we have buzz pin and what do we have it is an output alright and then what do I do here if I hit this thing too hard I need to give a warning so now I'm gonna say digital right buzz pin what hi know how let's give it like a half a second so then what I will say is the lay back what DT for the lay time and I need to come up and set that variable up if I have not yeah delay time I already have but I'm gonna put it at half a second let's say 500 okay so if I've hit that button too many times and it's already maxed out I'm going to turn the buzzer on I'll leave it on for delay time and then I will digital write digital right buzz pin what hello dub you alright so if you turn the buzzer on you got to remember to turn it off and then similarly the same thing we will want to copy and then down here if we're hitting the down button too long we can give the person a warning hey buddy take your finger off the button so now let's see if that will work all right now when we do this it's going to reset the the brightness to zero all right so now okay it's starting to come up I can see it coming on coming up getting nervous you're getting nervous here I'm not hearing the buzzer come on okay so the buzzer didn't work let's see I am on pin two I am grounded I am connected up right so the good thing about these things is when they happen the first thing is it seems like the circuit is working one thing is I can test the buzzer I'm just going to come in and I'm gonna go to Row 2 which is my plus 5 volts so you can see hopefully you could hear that it's kind of an annoying noise here hopefully what you could see there is is that the buzzer is working so for some reason I am not turning 10 to on so I say if LED bright is less than zero then I say led bright is equal to zero and digital right buzz pen high delay and then digital right buzz pin low and then if I look at buzz pen it is pen 2 so this is getting quite mysterious why it does not seem to be all right so what am I going to do am I really getting in this loop or not if LED bright is less than zero am I really getting in here so I'm gonna say serial dot println and then I am going to just type in buzz high and if I see buzz high I will know that I got into or this would be buzz low and that's just basically saying that I'm in that if statement where the value was too low and then I will take this and I will also put it up here as such and if I'm end up here that is saying that I should buzz because it's too high so since it's not buzzing oh I'm gonna see am I at least getting inside of these two if statements right so i download it looks happy all right it's reading both buttons is up and then as I hold this down you see I don't ever appear to be getting to you see I never see it printing it is never getting in there so it's like LED brightness never gets above 255 all right then what do I do now I need to print LED brightness because I think that I'm holding it in long enough to be that bright so here I'm just gonna do serial dot print Ellen I'll take all these out later but it helps me debug LED bright so I will see what the value is okay I did not make this mistake on perfect but I am gonna figure out what's wrong okay CC LED brightness is zero if you look here coming off the serial monitor and then I hit it and it goes to five the LED does come on okay ten all right CC we're letting it get brighter and brighter and brighter you think I just didn't hold it on long enough it should go to 255 I bet I didn't hold it on long enough and that's why it never issued that warning okay yeah you know what it was working all along I just wasn't patient enough and it was kind of you know going sort of slow I'm gonna leave this on here so that I can see it but if I if I'm at full brightness and I try to go brighter you see what it does it gives me a warning okay so now let's dim it okay so you see it's dimming and you notice how I can hit the button repeatedly or I can just press and hold we're down to 170 you're virtually able to see the LED getting dimmer 65 55 we're getting there okay we get the warning that you can't go any lower the LED is off okay I really like that the one thing like what you might do is you might play around with I think a way to improve this would be to play around with these offset values and maybe the minus 5 and plus 5 for each button for each button push maybe what I would do is maybe it should be plus 10 it's a little bit more responsive but you could play around with that and see what you like boom guys this was a great project look we're starting to get we're starting to get more complicated and what we're doing look at this we have got two input buttons we've got an LED and we have feedback through the inaudible feedback through the buzzer and so if you look at this you've got two input devices which are buttons and you've got two output devices one output device is the LED the other output devices the is the buzzer I think this is a pretty cool project and actually I kind of had some fun playing around with this I hope you you enjoyed it as well okay guys this has been kind of three lessons on the buttons you can see they play half an hour each so I didn't want to make an hour-and-a-half video but between these three you've got kind of like now a new tool in your tool set and that's how to use buttons to give input to the Arduino and kind of control what is happening alright guys this is palma quarter from top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 71,661
Rating: undefined out of 5
Keywords: Pushbutton, Dimmable LED, Arduino
Id: XWL6Ydsmi0s
Channel Id: undefined
Length: 41min 37sec (2497 seconds)
Published: Tue Sep 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.