Arduino Uno R4 WiFi LESSON 25: Make a Toggle Switch with Button Switch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcarter with toptechboy do.com and we're here today with episode number 25 in our incredible new 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 take a look down in the description there is a link over to Amazon and you can hop on over there and pick your kit 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 this Shameless self-promotion let's jump in and see what I am going to teach you today and what I'm going to do is I'm going to show you my solution to the homework assignment that I gave you in episode number 24 but I must first ask how many of you were successful if you were successful leave a comment down below I Am Legend and if you are not successful leave a comment down below I fold it up like a cheap Walmart lawn chair okay if you guys didn't get it don't feel bad because this is just a problem it sounds so easy press the button you want the LED to come on press and release the button again you want it to go off you want a toggle switch okay it just sounds so easy the two simplest the two simplest components in the kit the little push button and the little LED how hard could it be well it requires you to kind of expand and make new Connections in your brain okay it requires you to expand and make new Connections in your brain and this is usually introduced in an electrical engineering curriculum maybe in about year two and it's probably one of those Pro problems one of those assignments that is most responsible for engineers leaving the engineering department and moving over to the culinary arts department okay I love cooking so I'm not being hard on on people who cook I I love to do it but I'm just saying this is the trivial simple problem that just absolutely devastates people because again what it requires you to do is it requires you to make new Connections in your brain now once you see it it's going to be obvious but coming up with it on your own the first time you see this problem it can be a little bit daunting so again what is the problem you're going to have a push button on your circuit you're going to have your LED and if you come in and push and release the button the light comes on and it stays on if you come and push and release the button again the light goes off and stays off and so that is what we're going to look at let's take a quick second this is the same circuit that we used last week so I'm going to kind of go through it a little bit quickly because you should already have this built but let's just uh let's just go over it again really really quickly so I'll get out of your way so what you have is you have a push button and you have an LED and then this uh let's start with the LED here okay let's start with the LED I need to get out of your way a little bit further okay so this is the LED circuit the LED goes to pin 10 and so you see the led the long leg is on this side and it comes all the way over to our friend pin number 10 and then the short leg of the LED goes through through a 1,000 Ohm resistor okay a 1,000 Ohm resistor and from there it goes down to this bottom rail which we have created to be a ground rail so if we come over here and look this black wire comes over to G and D and so now this entire bottom row is a ground rail now similarly we're going to come over and we're going to look at the push button and you can see the push button the push button let me push this out of the way so you can see my uh circuit diagram you're going to start at 5 volt and so we start over here at 5 volts right here and this is a 5vt rail because I've got this red wire coming over to 5 volts on the Arduino so now this row this red row is a power Rail and so I come off of that power Rail and I go to the top leg of a 10,000 ohm resist resistor so I go from my 5vt rail to the top leg of the 10,000 ohm resistor then the other end of the 10,000 Ohm resistor is connected to the right leg of the switch and then the left leg of the switch the left leg of the switch goes down to my ground rail left leg of the switch goes down to the ground Rail and then also we want to read we want to read from this connection between the button and the 10,000 ohm resistor we want to read that on the Arduino so that's going to go to pin two and so that is right here this wire is connected to the left leg of the resistor and the right leg of the switch and then that comes all the way over to our digital pin too okay so that is the circuit setup now that part is the easy part and let me tell you the first mistake that people make on a problem like this a new problem like this is they immediately jump in and they they immediately jump in and they start coding okay I've got to set this pen up I got to set that pen up well well what I'm going to do an if statement here and you don't have a strategy and if you don't have a strategy you're really going to struggle and if you get a solution the chances are your solution is going to be insanely complicated and really like just by Brute Force get it to work so let me kind of show you again what are you trying to do you're trying to think like an engineer and so let me tell you the first thing we do is we don't go to the code the first thing that we do is we go to the scratch Pad we go to our engineering notebook and we start thinking through this okay so the first thing I want to do is clearly say in words what this should do and then I've got to take it from those words to a concept and then from a concept to a strategy and from a strategy to coding and if we do the paperwork and the thinking and the strategy the coding is going to be trivial why do people struggle because they don't think through the problem before they start so I want to State the problem Clearly Now what does it mean to press the button we've got to be more specific right if I press the button and release the button I want the LED to turn on Okay press release on how many things three things similarly now if I press the button release the button now I want it to what turn off so I want to press release on or press release off so what kind of three things you've got to get your variables right you got to set up your variables what three things do I need to keep track of I need to keep track of the button State now right now is the button up a one or is the button down a zero I need to know the button State now but I don't do something because the button's up I don't do something because the button's down I do something if last time through the loop the button was down and this time through the loop the butt button is up so I can't just keep track of button State I need to know where the button is now and where it was last time so I need button state where the button is now and I need button State old where it was last time now what is the if thing if the last time if button State old was zero and button state now is one what does that mean the button was pressed and released so if button State old is is zero right because when it's pressed it's zero button State old is zero and button State now is is one if I go from a zero to a one last time it was a zero this times it's a one what do I want to do I want to togg goal okay I don't want to turn it on because it might already be on I don't want to turn it off because it might already be off right I want to what I want to toggle okay I want to toggle so I need a variable button State old I need a variable button State and now what does this toggle means it means well if the LED is on I want to turn it off if the LED is off I want to turn it on okay now what you have to see is the Arduino just sitting there minded its own business does not have a clue whether the LED is on right now or off right now so what do you have to do you have to keep track with a variable of what your LED is so I'm going to call that led State like right now led State might be on or or LED State might be off and I'm going to call that led State equal 1 means that it's on and led state zero means it's off so what do we need we need to know button State now we need to know button State old what it was last time and I need to know is the LED presently on or off now if button State old was a if button State old was a zero and button State now is a one and the LED is off what do I want to do I want to turn it on okay if the button State old was a zero and the button State now is a one and the LED state is a one on what do I want to do I want to turn it off okay does that make sense does that make sense I hope it does I hope that was pretty clear so what I really need is I need three variables I need I'm going to call it butt State because that's easier to write I need but State old and then I need l e uh not okay yeah let's start over there I need to be I need to use uh I need to use better variables than that okay so what what we were using before I think is better I'm going to use but Val for the present state of the button but Val old for the previous version and then I'm going to have led State and with those three variables I should be able to do this I should be looking for going from a down to an up and then I toggle if I go from a down to an up I toggle in order to toggle I need to know where I am now all right so let's jump in now and if that's not clear maybe it'll start making more sense when I actually start coding and so now we are ready to fire up our Arduino IDE and we're going to go to our friend who Mr bear minimum which you come down and you say examples and you say basic and then we go to bare minimum open that up and I always like to make sure that my Arduino is connected I come down to port and what it sees is it does see the com 5 for me has the Arduino and that is selected so we always just want to kind of check that and it's good to see here that under Tools I've got the Arduino Uno R4 Wi-Fi selected so everything there should be good okay now what we want to do is we're going to come over here and we are going to start setting up our variables we are going to start setting up our variables trying to get a little bit of Windows management done here okay give me just a second okay so now I'm going to set up my variables where do we set up our variables above the void setup so the first thing I have got a button pin so I'm going to have int butt pin and where did we have that that was connected to digital pin 2 and then I had a red pin right my red LED and that was connected to digital pin 10 we might need to print something out to do a little debugging and I'm going to go ahead and turn get ready to turn on the serial monitor so I'm going to say our B rate is going to be 115 to 100 like that now if I'm going to use this butt Val and butt Val old and led State I need to go ahead and declare the variables and I need to go ahead and give them a value and I'm just going to make up a value but just so like maybe the first time through the loop the thing doesn't crash if it doesn't have like the the first time through the loop it's going to read butt valve but it's not going to have a butt Val old because it's the first time through the loop so I I like to just assign a number to the variables to make sure that I don't end up trying to use a variable that doesn't have a value in it so what we're going to do is brr what is this nonsense okay there we go now I'm going to have an INT and I'm going to have a but Val and I'm going to just set it to one meaning that you know the first time it's just the button hasn't uh the button isn't pressed okay and then in but Val old and I'm going to say okay and it wasn't pressed last time since there wasn't a last time it wasn't pressed last time okay now we're going to have led State and that is going to start at a zero meaning the LED is off why when the program runs for the first time the LED is going to be off so the LED state is zero and the button is not pushed and it wasn't pushed last time now we'll start reading and manipulating those all right man we're on a roll here what do we do in our void setup what do we do in our void setup we better turn on our serial monitor just in case we need it so I'll serial. begin and what's that going to be BR for B rate boom that looks good and then what I'm I going to do I'm going to do a pin mode on what on the red pin and that is a what that is an output like that okay pin mode red pin is an output and now what are we going to do I'm going to do a pin mode and what do I need to do my button pin I'm going to what I am going to read from that button pin right I am going to read from that button pin uh now I'm going to read from it and since I'm going to read from it that is going to be an input like that okay so my red pin is an output my butt pin is an input and then what am I going to do okay what am I going to do I've got things set up there I've got my pin modes done that looks good I'm going to run this just to make sure I don't I've got everything def defined so far ah you see I made a mistake already pin mode was not declared oh pent pint mode ah just kidding pin mode just kidding all right but I like to kind of get my initial errors out of the way like that it looks like it's going to work this time it doesn't do anything but I'm just making sure that I'm kind of on the right thing okay now the first thing I need to know is I need to know what state is my button presently in so how do I do that I read I read but valal is going to be equal to digital read of what my butt pin like that okay now I know what butt Val is first time through the loop I don't have a butt Val old okay so now what I need to do is but I assigned it a value so that I don't crash the first time through the program but what do I want to do I want to see if the button has been pressed and released if if the button has been pressed and release so how would I do that I would say if and then what but Val old if that was equal equal Z that means the last time the Button had been down if last time the Button had been down and and if this time but Val equal equal one this time it is what it's up last time it was down this time it is up that means we need to toggle so if that condition is true then what are we going to do okay what are we going to do well if the LED State equal equal Z then what do I do if the button state equal equal Z what do I do that means the LED is off what should I do I should turn it on digital right who am I going to write red pin and red pin's going to go what high why because if LED State first of all I got the down and up now I have to toggle well if the LED State equal equal 0 what do I do I write the red pin high and that's going to turn the LED on but on the other hand if Led Led State equal equal one what does that mean that means if the LED State equal equal 1 it means that is it is on and if it's on and I'm still inside this if state statement that's saying if the button went down and went up and now the LED is on what would I want to do I would want to turn it off so I'm going to do a digital right digital right red pin and it's going to be what high like that okay so to get inside of this if statement to get inside this if statement I need that button press down and up now that I'm inside of it I've got to decide is the LED on then turn it off if it's off then I've got to turn it on okay now this is the thing that is very very important and it's very very important and it is easy to forget okay this closes my if statement here this closes my if statement here and then this is my main if statement about the buttons and so while I'm still in there if I get into this if statement that means I am what I'm toggling the LED and if I'm going to toggle the LED if I'm going to go from on to off I better what I better update my LED state so my LED State my LED state is going to be equal to not the LED State and don't tell me you didn't know how to do this because I showed you when we were doing conditionals you can have equal things you can have nots and the exclamation is not so the LED state becomes not the LED state so if the LED state was a zero it's going to become not a zero which is going to be a one and if it was a one then it's going to become a not one which is going to be zero so I'm going to be flipping this from 0 to one but only if I end up in this condition where I press down and I pressed up I'm going to flip the LED on or off and then I'm going to update LED state does that make sense okay now there is one more thing that we have to do now I am all the way down I'm all the way down here I'm going to come out of that if statement going to come out of that if statement what do I have to do I have to drop a breadcrumb I've got a drop of bread crumb because next time through I might have never gone into these if statements or I might have gone into these if statements but what do I have to do I've got to drop a breadcrumb so the next time through the loop butt Val will know what it the butt Val was the time before so what do I have to do do I've got to tell it what butt Val old is and what butt Val old is it's going to be just what but B now is right because now my present value the next time through will be the old value so the old value that I will use next time through is going to be the present but but Val that's sort of like saying you know today is tomorrow is yesterday is sort of like what you're saying but if you think about it I'm putting this breadcrumb so that when I come back to the top of the loop and I read butt Bell I'm going to know what it was last time through does that make sense okay let's see if we can run this thing and see how many mistakes we've made okay ah what was that what was that ex status one compilation error I forgot yeah yeah yeah yeah yeah I've been working on micropython too long and I do indeed forget those semicolons sometimes now notice on an if statement you just go from like the open Clause the open curly to the Clos curly but then the commands inside you need to have that so let's try that again looks looking good looking good look at that okay look at that download it all right let's go to the full screen view all eyes on the LED we're going to come in nothing's happening I'm going to press nothing happens and I'm going to release boom press and release uhhuh look at that now should I celebrate completely yet no I got to make sure it works the other way so I come in press nothing happens and release ah denied denied press and release what happened there what happened there h ah let's look at our code let's look at our code H if the LED state was off I want to turn what I want to turn the LED on but if the LED is on I want to what ah turn it off okay did you guys catch that when I did that hopefully you did okay hopefully you did I like to kind of do that so you guys will be paying attention in not just mindlessly copying what I'm doing okay so now we're going to run it again I think the LED should go off when this thing runs yeah okay so now it is starting completely over we are going to come in and we are going to go to full view all eyes on the LED I press nothing happens and I release and boom LED on uhhuh toggled on but now the Moment of Truth we come in we press and we release boom off okay look it's a toggle switch oh it's a little dark in here I think I would like a little light illumination at my command ah but now it's a little bright I think I'm going to turn it off I come in press release ah what is that press release ah okay I didn't get a clean release on that okay so now ah and I'll tell you what I didn't do why did I get that false that one switch that didn't work I'm going too fast that you have to debounce the switch man I'm glad that happened because you got to debounce the switch which gives it a little time to settle so I need to do a delay between the loops a delay of doesn't have to be much but about 50 no no no no what do we need a variable delay T like that okay and now we come up here and we're going to do an INT delay T delay T is equal to 50 don't forget our semicolon let's come down here like that let's run it again yeah you see when it didn't work that one time it's making the reading so fast it can get a little Jitter in the reading a little it's called switch bounce you put a little bit of delay in there to take that switch bounce out and you didn't see what I just did did you let me go back over here okay what I did was down at the very bottom here at the very bottom right before the end of the Void loop I put in a delay of delay T and then up here at the top I set up delay T to be 50 okay so that was a just a little code I said it out loud but now I've showed it to you so I think I think you guys should be good to go so now that we have that and I think we've run it let me let me uh let me come over here and let me have a little sip of coffee and what we're going to do is it should just absolutely positively work this time 50 milliseconds is enough to debounce a switch so I'm going to come in down hold it boom now I'm going to come down hold it boom quick quick okay look at that guys that really works now what if you said but but but but but but I want mine to work when I press it down that I want it when I press it down to switch not when I release it well okay how would we do that I want kind of like a trailing I want right now I have a trailing Edge toggle and I want a Leading Edge toggle I want to toggle as soon as that button goes down well how would we do that how would we do that very simp simply well if last time it was undisturbed if last time it was undisturbed in the up position and this time through it has suddenly gone down zero then I want to switch on that so let's come in and let's see that okay so we're going to run [Music] it see also kind of have this view I sort of like that view every once in a while as well okay so let's watch that view now I'm going to come in watch the LED my finger comes down and I press and it switches and I release now I come in and I press and it switches and I release so on the Press you see right on on the downstroke on the downstroke the thing switches okay guys man I hope you are having as much fun taking this class as I am making it I do need to give you homework assign assignment and so what the homework assignment is going to be to come in and instead of using this red LED what I want you to use is to use the RGB LED and I want you to have three switches and I want one switch to be the little red button the second switch to be the green button and the third switch to be the blue button and if I come in and press red button the LED turns red if I come in and press the green button the LED turns green if I come in and press the blue but button the LED turns blue and you know it stays that you're going to do the toggle so I can toggle it red or I can toggle red off I can toggle green on or I can toggle green off I can toggle blue on or I can toggle blue off okay now this is just really you you guys should be able to do this homework because this is just just really applying the things that we've learned in the last few lessons so what is it doing it's reinforcing your learning it's forcing you to write some code and make some mistakes and find your mistakes but there's not any great conceptual stretch now I will admit that this lesson I showed you today could be a challenge for you but now the next homework assignment it's just applying what you've learned thinking through it and being careful okay guys I hope you're enjoying these lessons as much as I am making them I am having a lot of fun with this I really as always want to give a big thank you to you guys who are stand Standing with me on patreon it's your encouragement and your support that keeps this great class going also you can help me by giving me a thumbs up and leaving a comment down below if you haven't already subscribe to the channel when you do ring that Bell to make sure you get notifications When Future lessons drop and as always 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 toptechboy do.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 1,853
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: Is8YhpQVVX8
Channel Id: undefined
Length: 32min 14sec (1934 seconds)
Published: Thu Jul 11 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.