Arduino Tutorial 28: Using a Pushbutton as a Toggle Switch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is polemic order from top tech boy comm and we are here today with lesson number 28 and our new or improved Arduino tutorial series so I'm going to need you to pour yourself a nice big mug of iced coffee I'm gonna need you to get out your fabulous a Lego super starter kit if you haven't picked up one up yet look down in the description click on the link hook a brother up order it $35 it has boatloads of components in it in this entire series is based on the hardware in that kit and it actually makes a little better and a little easier for you to follow if we're using the exact same hardware ok what we are going to do today is we are going to do a project where we incorporate a push-button into an Arduino project and this is kind of a follow on to lesson number 27 remember in lesson 27 we had it hooked up where we learned about pull-up resistors and pulldown resistors and we basically let me move over here we figured out how to create a circuit where we were using a pull-up resistor and then we connected it to a connected the Arduino to an LED through a current limiting resistor and then we had it set up in lesson 27 where if you press and held the button down the LED came on and stayed on as long as your finger was on the button and then when you let your finger off the button it would turn it would turn back off and so that really was a very very simple program and then I gave you an assignment and that assignment was to keep the same circuit but to come in and create a different kind of switch using the same hardware and that is a toggle switch where when if you press it the light comes on and then when you press it again it goes off so it switches from on to off now we're demonstrating this with a simple LED but you could use this same concept to control all types of different things on an Arduino project and so what's your assignment for today was was to create something like this I've press the LED comes on I press again the LED goes off I press and hold and it comes on when I let it off or I can press it quickly okay CDC what the goal is the goal is to do this were you able to do it well I'd like to hear in your comments below whether you were successful or not but my guess is many of you were not successful because the first one was so easy you probably jumped in and thought that this new one that I assigned you would be easy as well but it turns out that it's really kind of a hard thing to do you've got to think in new dimensions and new faces that you hadn't thought in before but don't worry I mean if you want to still pause and go away and try to do it pause it go what it try to do it and then come back and follow along with me leave comments down below did you get it by yourself or did you have to kind of watch me to be able to figure it out but this is a pretty this is a pretty good lesson okay first of all just as a reminder go back and watch lesson 27 if you haven't seen it yet but if you're one of those guys that doesn't like to go backwards just wants to go forward this was the circuit that we hooked up push button one leg of the push button switch goes to ground the other goes through a 10k pull-up resistor to a fixed 5 volts and then off of this connection here we come out to pin 12 which we're going to read a digital signal from pin 12 and then of course the LED we connected to pin 8 we went across a 330 ohm resistor across the LED into ground so you can pause it there and try to copy that I'm not going to rebuild the circuit since I already did that in our and he did that in Lesson number 27 so today we are just going to be working on the code working on the code so I need to move to a different and more suitable view and probably also before I start coding we are going to have to kind of talk about what a strategy is on this because I would imagine that there a pretty good chance that you just kind of wandered around in the dark for a while on how to do this but maybe you didn't maybe you guys maybe you guys figured it out but this is what the problem is you remember that when it is just sitting there like if you were just sitting there reading from that pen from from that pen which pen were we reading from it was pen 12 if you were doing a digital read from pen 12 what would happen is is that if you were just just sitting there reading in the no button push state it's gonna be reading once and so as you do not have the button pushed it is going to just be reading once and so you're just making a series of reads very quickly now when you press the button now all of a sudden you will be reading zeros like this okay and then when you let the button up you're going to be reading once again and this is pretty much we showed this working in in lesson number 27 so this is the signals that you have and this is what you've got to write your program around and so what is it that you want to happen well this is kind of a Down transition and the way I showed this working is on the up transition on the up transition this is where you want to change so we are going to have to switch the led on this transition now notice I didn't say turn the LED on because like the first time you let the button up you turn the LED on but the second time that you see the button comes a come up you have to turn the LED off so there is a concept here that you're going to have to kind of kind of introduce yourself to and that is is that we need to keep track of the LED we need to keep track of the LED state okay so in order for this project to work you have to have a variable where you remember where you are you already on our you already awful if I'm already off when I come like this I want to turn on but if I'm already on then when I see this transition I want to turn off and so we need a variable called LED state and that is either a 0 or a 1 that LED state is either a 0 or a 1 a 0 would mean I'm presently off and a 1 would mean I'm presently on now besides that what you got to see is in the earlier lesson you were only worried about the present value of the switch that appeared the button is not pressed and down here the button is pressed and so you're only worried about where the button is now but now you've got to know two things you've got to know where it was before before and where it is now and that way you can compare the present state to the previous state and if the present state is different than the previous state then you want to switch so we have to have I guess you know we have to have two values for the button so we would say I need to know button old and I need to know button new so when do we switch well we switch when we see that if button old was 0 and but new is 1 then we want to switch so we switch on this transition button all the previous reading was 0 and but knew the present reading is 1 then we want to switch well now what does it mean to switch well if I was a 0 I want to go to a 1 if I was a 1 I want to go to a 0 and so that what that saying is if the LED was off turn it on if the LED was on turn it off so you see how with this even though this project seems incredibly similar to the very simple code that was in Lesson number 27 this one's turning out to be kind of like a lot more that you have to think about that you have to remember where is the LED presently and then you have to remember what was the previous value of the button and so there's kind of like two variables associated with the LED two conditions associated with the LED and then you have to have two variables Association associated with the button where the button was last time around where the button is this time so let's try to begin to set that up so what I'm going to start with is I've got to keep track of where the LED is is the LED on or is the LED off so I'm going to call it LED state and when the program first starts the LED is going to be off and so I'm going to set the LED state to zero just indicating we're starting with the LED off like that okay so that's one variable now we also just like we always did we have to have an LED pin and in my case for this circuit the LED is connected to pin 8 so we'll call that eight and I also have a what I also have a button pin okay my button is going to be connected to a pin and that button pin is 12 and that is where I am reading that value between the resistor and that one leg of the switch as seen in the circuit diagram I showed you while ago so but LED pin eight button pin 12 now I've got to keep track of what the value of the button is and I've got to know where it is now and where it was before so where it is now I will call that button new and but new I will read so I won't put a value on it and then I also need button where it was before the last time around that was button old and I'm just gonna go ahead and set button old to one because remember it's just sitting and reading a one before you but before you press the button so we're gonna kick things off with a button old value of one all right we probably will need a little delay in there so I'm gonna put a delay time equal 100 milliseconds and then I think that is probably I think that is probably the that is probably the variables that I am going to need so now in our void set up we need to do our what we need to we've set up our serial dot begin 9600 so we've turned our serial port on what do we need to do we need to do our pin mode so I'm gonna have pin mode and then I'm gonna have LED pen and what is that that's an output because I am sending signals to the LED and then I'm gonna have pen mode and this is going to be button pin and that's a what input I'm reading from that pin so I believe that we have our pin modes done so now we got to come down here and we've got to start domain code so what is the first thing that we need to do we need to read and we need to look and see whether the button is pressed or not so that would be a read and where is that going to go well that's gonna go in button new so what is the new position of the button the new position of the button will be a digital read where are we reading we're reading from button pin I feel like I'm going too fast and guys if you're not keeping up watch lesson 27 because I explain explain this in lesson 27 yeah I explained the basics of the circuit in the basics of doing this read so we're going to do a digital read of button 10 and there is only one parameter on a digital read and that's where you're reading from now what is the case that we are looking for we're looking for when the old value is 0 and the new value is 1 so the if statement that we are going to have is if the old value button old was a 0 and a second condition how do we do and in Arduino and and the second condition is button new equal equal what equal equal 1 then we would want to do the following so does that make sense what is this transition if the old value is 0 and the new value is 1 then we want to switch so that is the condition if old value is equal to 0 button old is equal to 0 in button new is equal to 1 then we want to switch well what would it mean to switch that depends on whether you're presently the LED is on or if presently the LED is off and that's why we set up this led state led state 0 means the LED is presently off so if LED state equal equals 0 indicating that the LED is off then what do we want to do well what we want to do is we want to digital right what do we want to write LED 10 and what do we want to do well it was if it was off we want to turn it on so we'll say hi ok now this is pretty important you're keeping track of is the LED offer on well it was off you just turned it what on so you better leave a little breadcrumb and in and keep track that now LED date is equal to what one okay so that is the first thing if led state is equal to zero you do these commands it starts with this open curly it ends with this or open curly well if not the word is else else that means if led state is not equal to zero else the opposite the other case what would we do well we would do digital right led in what well if LEDs is not often that means led is on and if it's on and we want to switch it what do we do we say low we turn it off and what else do we have to do if we turn it on we if we turn it off we've got to make sure that we remember now that led state equals zero so we have got to keep track of these things and I am doing a horrible job in putting my semicolons in so let me go back and do this okay so what are we gonna do we're gonna read the button alright and if if the previous value of the button is a zero and then the new value of the button is a one then we want to switch well what do we want to switch well if the LED was off we want to turn it on else that means that it was already on then we would want to turn it off and then we've got to make sure that we keep track of whether we are on or off so now we are coming out of all of these if statements and so this curly bracket ends that curly bracket after we do this what do we need to do okay we need to remember what the new value of the button was but now that new value becomes what it becomes the old value so button old and now is equal to button new and then what will I do okay I put the button new into the button old and then I come up and I read a new button new and so we have this all programmed in let's go ahead and see if we can download this this is a crazy one so I need everyone to hold their breath Oh what did I do Oh delay time a hundred why did it not like DT ah I forgot to say int never use that yeah I never use that written yet and that reminds me now that I fixed that that you don't want to run this thing too fast because what can actually happen in real scenarios that you often had trouble with push buttons because when you push the button it doesn't cleanly come down a lot of times it will sort of jump up and down a few times and this is called bounce and a switch and if you're sitting this would go on off on off on off real quickly a lot of times with a button push and so what you want to do is you want to have a little bit of delay to let that clear out and not negatively impact your performance so after we do all this we don't want to go too fast so I'm gonna say delay how much delay time and then let's go back and look at what our delay time was a hundred that's a tenth of a second so I think the circuit will still be pretty snappy if we do that okay problem was someone didn't hold their breath that's why it didn't work all of you I need you to hold your breath PIM mode it's important to spell correctly were you guys yelling at me on that okay this time seriously everyone hold their breath it's gonna work yes it's going to work okay it downloaded and so now let's come over and let's look at a more appropriate view let's see if we can come up to this shot and now we have downloaded we have downloaded the code and now I'm going to come over and I'm gonna press the button and let my finger off so I'll go like that boom the light is on the LED is off I can hold it down a long time and let it off you see it doesn't switch when I'm pressing down it's what switches when I let my finger off and that's what we were doing right remember we were looking for that we were looking for that 0 to 1 transition and we can see that it is working most excellently this is a very very powerful thing because usually you want a switch like this a push-button switch to operate in the toggle mode you usually want it to operate in the toggle mode because you could be turning a fan on you could be turning a motor on you could be turning any number of different things on and off and the way you would typically want it to be done is with a stock I'll switch ok guys this was uh I'd be I really want to hear it excuse me I really want to hear from you guys were you able to do this on your own or did you have to watch me do it and if you did it on your own did you do it this way or did you do it a different way because this was not an easy transition from lesson 27 because there were two things you had to think about you've got to keep track of two values of the button where it was last time and where it is this time hence button old and button new and you had to keep track of where the LED is is the LED presently on or is the LED presently off and so this this required a little bit more multi-dimensional thinking than things we've done in the past leave a comment below and let me know how it how well it went love you guys too give me a thumbs up on the video would love if you subscribe to the channel okay appreciate you tuning in this is polemic order from top tech boy calm I will talk to you guys
Info
Channel: Paul McWhorter
Views: 130,405
Rating: 4.9605694 out of 5
Keywords: Pushbutton, Toggle Switch, Arduino
Id: aMato4olzi8
Channel Id: undefined
Length: 21min 57sec (1317 seconds)
Published: Tue Sep 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.