Arduino Basics Handling Multiple States

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right today we are going to talk about detecting button presses and dealing with multiple states and what that involves is a click of the button changes from one led being turned on to the next so let's go over the setup here we have our little tactile click button here bridge it across the gap and on one side it is pulled to five volts on the other side it is going to digital pin 3 and connected on that side also is a 10k pulldown resistor so what's happening there is when the button is not being pressed this input a d3 is pulled to ground so it has a low value it's not floating but when the button is pressed the current will flow from the positive through there and it will go high so we'll have a button press now we have three LEDs a red a yellow and a green and I just realized according to my programming I plug them in in the wrong order but that's ok we'll switch it later the anodes of which are connected to pins 5 6 & 7 the cathodes are going through resistors to ground now in order to match the brightness of these we have to do a little bit of math the red LED takes the least amount of current so I'm using a 220 ohm resistor on it so the anode goes through a 220 ohm resistor to ground the yellow and the green each go through a hundred and 50 ohm resistor to ground now I'll bring this up here so you can get a little better look at it and learn your resistor color codes all right nothing complicated here I'm running one ground over to this ground rail here for the button I'm running another ground over to this ground rail and I'm running the +5 volts to this +5 volt rail so that is our physical setup and what should happen is when we click the button when we turn it on originally all the LEDs will be off we click the button red will light up click it again the red will go off the green will light up click it again the green will go off the yellow will light up click it again and all the LEDs will be off so that is multiple states let's go over to the PC and have a look alrighty here we go with our programming for the multiple state handling and that was written by learn electronics that's me on January 4th 2017 so again I've separated everything we have here in two areas to make it easier for you so we're going to start with our defines and we define button 3 so our push button is on digital 3 our red LED is on digital 5 green LEDs on digital 6 and our yellow LED is on digital 7 again we're using the define because it just makes it easier it takes up less memory than declaring a variable for it when the program compiles ok now we have our variables we're creating an integer to hold the current state called state an integer called owed to hold the last statement and an integer called button pole to hold the button polling state so three total variables next we'll come down here to our setup and the first thing we do is we set our button to input now it is not technically necessary to set an input as an input but when you're programming it's better to leave nothing to chance and be very explicit about everything that you want next we'll turn our LED pins all to output and keeping with our doctrine to keep everything very specific we're going to set an initial State on the LEDs of making sure they are all off that way there's no chance that anything weird could happen when the program starts up alright now we're going to come here to the loop section and the first thing we're going to do is we're going to use a debouncing routine to check the state of the button now if you haven't ever used the debouncing routine before I have a video on it and there's a link to it in the description below so the first thing we're going to do is we're going to pull or read the state of the button and if it is 1 that means the button has been pressed in that case we're going to wait 50 milliseconds to allow all those little oscillations to work their way out and we're going to do it again now if this time the button is zero we can assume it has been pressed because it was here and then we can assume it has been released and if that's the case our integer state will equal the value of old plus one so when the program initially starts out old is zero so state will equal zero plus one or one and if the button hasn't been pressed it's just going to wait 100 milliseconds and start all over again now here we go with our state so we're going to use a switch case statement here which is what you want to use when you have multiple choices you can do it with if statements but you're gonna have a whole bunch of nested if statements and this just makes everything a lot easier and neater so switch our variable state case 1 if state equals 1 then we turn the red LED on and we turn the other LEDs off then old equals state so we set the old state to the current state so if we are now at one which we are since we're in case one old now equals 1 now we come down here to case 2 this is state 2 if we're in state 2 then the green LED is turned on and our red and our yellow LEDs are turned off old equals state again so there were two state equals to old now equals 2 case 3 we are going to turn on our yellow LED and we turn off the red and the green LEDs and again old equals state so state we know is 3 old now equals 3 and then we have our default condition which means if state is not equal to 1 2 or 3 so for instance if we were at case 3 and we went back up here and pressed the button again so old now is 3 state equals 3 plus 1 is 4 that's not 1 2 or 3 therefore we're going to come the default which shuts everything off and sets our old state as zero so we are back to the beginning it's not very difficult and if you wanted to see how it works you could just put a serial begin here and your setup and add a serial print state to each one of these statements open up a serial window and see how it looks okay alright let's go see how it works ok so our code is loaded let's power up the Arduino alright everything is powered and now you notice no LEDs are on because we set all their initial States to off first press read second press green third press yellow fourth press off now we start again red green yellow and you'll notice with our debouncing routine that it is not the actual press that turns it on but the release I like to do that because that means no matter how quick of a button you press it is only actuated on the release makes it nice neat smooth so there you have it now if you like this sort of thing I hope you'll give me a thumbs up and share it somewhere and if you haven't subscribed yet what the heck are you waiting for
Info
Channel: learnelectronics
Views: 120,043
Rating: undefined out of 5
Keywords: electronics, arduino, learn electronics, learn arduino, arduino basics, electronics basics, circuits, digital, analog, machine state, multiple states, how to
Id: ojflP6pvTMc
Channel Id: undefined
Length: 10min 34sec (634 seconds)
Published: Wed Jan 04 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.