How to Make One Button Have the Functionality of Two or More with Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
do you have an application where you want multiple buttons for different user inputs maybe you have a timer and you want one button for minutes and another for hours but there's a problem you only have room for one button or maybe you only have one button handy so what we're going to do in this tutorial is learn how to use an Arduino to explore how to make one button have the functionality of two or more so let's get started so for this tutorial you will need a momentary push-button 5 jumper wires a saddle or spread board 2 light-emitting diodes doesn't matter what color and 2 220 ohm resistors you'll also need a ripened pomegranate so let's go ahead and set up this circuit so what we're going to do to demonstrate using a single button for multiple functions is set up a simple circuit that has two LEDs in a button and then based on how long we press the button different LEDs will illuminate so let's start with using a jumper wire to connect any ground pin from the Arduino to the ground rail on your breadboard then you want to place an LED on your breadboard and just make sure you know which way the long leg is facing now take another jumper wire and connect pin 13 from the Arduino to the breadboard in the same channel where you have the long leg of that LED attached so pin 13 should now be electrically connected to the long leg of the LED now connect the other pin of the LED to one side of the 220 ohm resistor and then take the other side of the 220 ohm resistor and put that into the ground rail and the resistor the orientation doesn't matter so you don't have to worry which way it goes so now we're going to repeat that but instead of using pin 13 we'll use pin 12 add another LED add another resistor that goes to ground finally we're going to go ahead and put the push-button on the breadboard now depending on the style of push-button you have they often fit well straddling that long trench that goes through the breadboard on one side of the push-button you want to connect a jumper wire from pin 2 on eard we know to that button and then on the other side of the button you simply want to connect it to ground using a jumper wire that's pretty much it for setting up the circuit it's pretty simple so when you press the push button that's going to electrically connect both sides of that button and pin 2 will have ground voltage applied to it and what we'll do is use that ground voltage input to trigger our different functions so let's go ahead and take a look at the sketch so here we are inside the sketch and if you go to the open source hardware group website and the link will be in the description below you'll be able to download the file that is this sketch you'll also be able to download the schematic that we just looked at and you can also get a pdf version of this tutorial in written format so let's go ahead and look at the top of this sketch you can see we have comments comments are pretty straightforward we already went through the circuit have a couple notes and then I just want to make a quick note that this code was actually created by one of the members of the open source hardware group premium course and he was new to Arduino a couple months ago and now he's doing really cool things and he was working on a home automation project and he needed a piece of code that took one button but gave it multiple functions and so I saw it and I thought oh man this is great we should do make something with it and he he volunteered the code so thank you Steve so the code is in the public domain so we can feel free to do what we would like with it so the next block of code is where we're going to declare and initialize variables now we know we have to track how long we're pressing the button so we're going to need a variable to do that and the one we use is called press length underscore milliseconds now that variable name might come off to you as too verbose and extremely annoying and I wouldn't particularly argue with you there however I really feel like including the unit of measurement in the variable name helps readability and readability is good not just for other people when other people need to read your code it's also good for future versions of yourself in you know six months from now when you don't remember what the heck you wrote so again we need a variable that's going to track how long we're holding the push button and this is it so the next thing we need to do is set a length of time for our different options to occur at so I've defined two options here option one milliseconds and option two milliseconds in the value which is in milliseconds is the minimum amount of time that you must press the button in order for that option to happen so you can see 100 milliseconds that's just a tenth of a second really just kind of a click of that button and we should get the first option to happen and then if you want the section second option to happen you have to hold that button for a minimum of 2,000 milliseconds or two seconds if you wanted to add more options this is where you would add those options and then you have to specify the time in here now the only other variables we're going to declare and initialize are the pins where we have our Hardware attached so we have a button and that's at pin two and then we have an LED that's for option one so that LED will turn on when the first option time is selected and then we have an LED for the second option that will come on when the section second option time is is selected so if we've held it for two seconds that second option LED would come on and I've just defined those pins there and that's pretty much it for the declaration and initialization of our variables so now let's jump in to the setup so the first thing we're going to do in the setup is set the modes of our pins so we need to set the mode of the button pin that's pin two and of the LEDs and those are at pin 12 and 13 so we use the pin mode function to do that and for the button you'll notice that the mode we set is actually an input pull-up now I'm not going to get too in-depth on why I use input pull-up here because I do have another video that talks about it but basically what we want to do is we want the state of that pin to be a known value when we're not pressing the button okay so when we set it as an input pull-up what happens is there's an internal 20k resistor inside the Arduino chip and it connects that pin to five volts so at any given time if we're not pressing the button and we query the value at pin 2 it's going to return a 1 because 1 is a high input so we know the state of the pin now when we press our button and we connect the pin to ground then we'll know that the buttons been pressed because we see that ground voltage and when we query that pin it will return a 0 so again that sounds a little confusing check out the video on the probably the floating pins I think I talked about it in that video and that should clarify that all right and then the other thing we do is set the LEDs as outputs and that's because we're going to be applying voltage to pins 12 and 13 hence they need to be outputs finally what we do in the setup is start serial communications again that's really just for debugging and that's going to allow us to look at the amount of time that we have held the button down all right that's for the setup let's check out the loop so before we jump into the code of the loop let's ask ourselves what we have to accomplish in this loop so one we need to be able to record the amount of time that the button is being pressed because that's going to help us determine what option gets executed and then we need to somehow define a way to have those options occur when those time thresholds have been met so let's tackle that first item first let's capture how long we're pressing the button and the way we're going to do that is with a while loop now if you look at the while loop the condition is digital read button pin equal equal low so what does that mean well we're going to use a digital read function and we're going to be looking at pin 2 and that's where the button is now digital read is going to return a 1 or a 0 it's going to return a 1 if the pin is high and recall we had it set as an input pull-up so any time we're not pressing the button the pin is high now when we press the button pin 2 is connected to ground voltage and it would return a 0 which is also low so digital read looking at the button when we're pressing the button is going to return a low value so when we're pressing the button the while loop gets executed and it's not just when we press the button it's while the button is being pressed because as soon as we take our finger off the button now the pin State goes back to a 1 which is a high because we set it as an input pull-up and the while loop is going to stop executing because the condition is not being met so let's pretend we press and hold the button what's the first thing we do well the first thing we do is delay 100 milliseconds after we've delayed 100 milliseconds then we go ahead and set the length of time to the press length milliseconds variable so if your call we set that value equal to zero so what this says right now the first time through the while loop is zero equals zero plus 100 so we are setting press length equal to 100 seems a little odd that syntax but it will make sense as we continue to go through this while loop so essentially what have we done well we've just added 100 milliseconds to our wait time because we just delayed 100 milliseconds so then the next thing we want to do is display that to the serial port and we use the print function from the serial library to do that so let's say we're still holding the button what happens next well we continue this while loop we delay another 100 milliseconds and now we get back to that curious statement where we've got press length milliseconds equal to press length milliseconds plus 100 so now the second time we've gone through the while loop press length the milliseconds is 100 so now we're adding 100 to that value so what's 100 plus 100 it's 200 now we're going to print that value to the serial monitor and we go through the loop again so let's pretend we're still holding that button now we're going to add we're going to delay a hundred milliseconds again and now we're going to add a hundred to that so what's 200 plus 100 well it's 300 so you can see as we continue to hold the button we continue to delay the program by 100 milliseconds and we continue to add a hundred to the variable that's tracking our timing and then all the while we'll do this we continue to print that variable to the serial monitor to let us know how long we've been pressing the button that's pretty much it so now what happens when I release the button when I let go the pin is going to go to a high voltage and that while loop stops and what I'm left with is the amount of time that I held the button stored into the press length milliseconds variable and with that variable we can move on to the different options well now we have the time that we held the button now we need to compare it to the different options we have and then execute the option that fits that amount of time so to do that we're going to use if-else statements now we start with the option that has the longest start time first so if you're going to add additional options to this and if they happen to be longer options then you need to make sure they go before this option right here so the if statement we have the condition we say is we ask is the press length milliseconds variable that we just recorded is that greater than or equal to the option to milliseconds variable which is essentially the threshold for that option to to happen so let's say we held the button for two and a half seconds so what number would be in the press length milliseconds well it would be 2500 and so we want to know is 2500 greater than or equal to 2000 which was the threshold that we sent set for option 2 well yes it is so if it is what do we do well we execute that if statement and inside there we have a digital right and we're writing the LED option to pin-high so we turn on that LED pretty basic so what if we didn't hold it for two seconds or more what if we held it for less than 2 seconds let's say we held it for second and a half what happens then well the first if statement doesn't get executed because the condition isn't met clearly a second and a half is not greater than or equal to two seconds so we move on to the next statement which is an else--if and now we reach we take another condition and it says okay well is the press length milliseconds variable is it greater than or equal to option 1 milliseconds so that was 100 milliseconds so if we press the button for a second and a half that would be 1500 milliseconds so yes 1500 is definitely greater than 100 milliseconds so we're going to execute the code in here and what do we do same thing as before we do a digital right except now we're going to turn on the other LED that we associated with option 1 really that's that's the basis of this program we have a while loop that is collecting the amount of time we're holding the button when that when we release the button we take that variable that holds that time and we compare it to the different options that we've set using if-else statements all right and then finally what we do at the end is we want to reset that timer variable back to zero because we don't want every little button press to be cumulative we want a fresh start every time we've released that button so the next time we press the button we start with a fresh timer set at zero so now let's go ahead and upload this code to the Arduino and open up the serial monitor and then let's take a look how this works so the first thing I'm going to try to do is the long button press so I'm just going to go ahead and hold that button down for at least two seconds and then release so I'm holding it down you can see the numbers counting up I'm past 2,000 to 4,000 reliefs and then you can see the LED comes out so now I want the short button press so I'm just going to go ahead and hold the led down again and release I was at 1700 and you can see the LED option one comes on illuminates and that's pretty much it how it works so it's really not too difficult and I'll tell you adding other options also is intuitive Cult just a couple things to keep in mind as you add more options you want to be careful about how close you squeeze together the different different options or it can make it difficult for the end user to try to figure out what the exact timing that you've got set up so I really haven't come to a rule of thumb yet for what works best but I think if you experiment around you'll find out what works best for you all right well hey I hope that tutorial was helpful and I look forward to seeing you in the next tutorial and you should definitely check out the challenges that are about to follow have a great day bye you you
Info
Channel: Programming Electronics Academy
Views: 104,539
Rating: 4.9001427 out of 5
Keywords: Arduino, mutiple button press, buttons, digitalRead, Open Source Hardware Group, Mutiple Button Function, Press and hold button, How-to (Website Category), arduino tutorial, arduino uno tutorial for beginners, arduino uno programming, arduino project, arduino button, arduino programming, arduino button tutorial
Id: IsDzxtaZCoI
Channel Id: undefined
Length: 15min 39sec (939 seconds)
Published: Mon Jan 05 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.