Arduino Tutorial 49: How to Build a Simple Calculator with LCD Display

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 49 in the epic in the spectacular in the legendary Arduino tutorial series the new improved Arduino tutorials I will need you to pour yourself a large mug of iced coffee I will need you to get out your most excellent e Lego kit don't have one look in the description down below there's a link get everything you need for $35 and those in fact are that is in fact is all the hardware I'm using for this entire series of lessons so if you get one and want to play along at home we will be using identical hardware which does in fact make life a little bit easier also you guys that are sort of my regular followers I've kind of given you a heads up that we are getting to the point we're going to be building more complicated circuits and really you reach the point that those simple bread boards don't work and so I need you to pick you up one of these larger bread boards so that we can kind of start getting a lot more projects going here and also I mentioned you really need to get some of these short jumper wires that allow real neat and easy to debug interconnect on the PC boards pick these up when you can and then as we start getting into those more advanced lessons you will be able to do much neater much neater work there comes a day there was a place for these male-to-male long jumper wires but the day is coming that you need the short neat precise connections okay what we are going to do today is we are going to look at the homework that was assigned in lesson number 48 you got to do the homework before you watch this video because I need people out there that are learning to do things as opposed to just learning to watch things the world needs more doers the world needs fewer Watchers you need to do it and then if you got hung somewhere you really didn't understand something or youtl help okay fine do this but do it on your own first and then you can come in and see did you really do it the way that I did it all right I will need you to get your I will get out of your way your Arduino you are going to need the LCD display as seen here out of the kit and then you're gonna need a potentiometer and you're gonna need a bunch of wires now this is where we left off in Lesson number 48 so if you haven't done lesson number 48 you can get caught up by going back there you can find the lesson on my youtube channel or dueño tutorial 48 to get caught up if you need a quick preview of what we were doing I believe I have got the schematic here of how to hook the LCD up to the arduino this is a pretty good picture this is also on top tech boy comm if you go to our dueño tutorial number 48 you can get your own copy that you can that you can look at and also on top tech boy comm Arduino tutorial 48 I have the code which we are starting with here I believe that I can now move to the code view and we can talk about what the assignment was the assignment was to build an Arduino calculator where basically you ask the user for a first number you ask the user for a second number and then you ask the user for an operator and then it would do that calculation and then it would print the calculation out here on the LCD screen nicely formatted remember nicely formatted so let's see if we can get this going all right we're gonna start over here we've already got the LCD nominally functioning we got that we got it hooked up we've got the object created we've got the wires in the right place so we want to kind of keep this much of the code but now what was the assignment the assignment was to create a calculator so what are you going to need you are going to need a first num you're gonna need a second num and you're gonna need an answer to begin with first of all a calculator how do we declare this to do it did you declare your first first num an int if you did deduct one letter grade from your grade already because when you multiply it you don't want just the round numbers you want the in-between numbers like if you're going to calculate the area of a circle you want PI to be 3.14 in the area is not going to be an int so first then needs to be a float which is the in between numbers all right you're a smart guy you probably got it figured out that second second um I don't want to type second every time second num is going to be a float and then float answer all right like that okay now I've got first num second num and answer now I need the operator the operator was going to be plus minus times or divide and I think that you could say add subtract multiply or divide I'm just going to use the symbol but in any case that is going to be a string and we're going to call that string operator o P now why am I not putting values in because you're going to go in and you're going to get the values as part of the loop okay so we have first num second num answer an operator we in the let's see in the void setup I start the LCD but you are going to have to get numbers from the serial monitor right we're trying to untether from the computer and we're starting by developing a display but at this point we are still tethered and the input is going to come from the keyboard so since the input is coming from the keyboard we do need to do a serial dot begin so we can talk over the serial port like that okay then we came down here and we did a lot of our LCD functions but here now what I really need to do is I've started everything I need to set the cursor to zero zero I'll erase the rest of that code in a minute but let me just come here to get input from a user from the serial monitor what three things do you have to do I will give you a hint we taught you how to get input from the user in Arduino tutorial 19 and Arduino tutorial 18 if you don't know how to do that go back and watch those tutorials but just as a reminder to get input from the user you need to do three things you need to ask you need to wait you need to read so let's start by asking serial dot print oh wait a minute we're not going to print to the serial monitor we're going to print we're to the LCD so we're going to say LCD dot print and what are we going to print input first number okay so we've asked now what do we do we wait while LCD that's not gonna work right if we're gonna get I'm sorry okay that will work I'm sorry so we asked by sending the message to the to the LCD okay now what do we do we wait so we save while and we're looking on the serial monitor serial dot available equal equal zero well serial dot available equal equals zero so we're waiting for a response what do we do while we're waiting for a response absolutely nothing so we can just pass in here I don't know if the pass is allowed we'll just leave it point so it's going to sit there and just loop until you type something in once you type something in what should you do you should read it what should you read it into first none is that what we called it first numb let's go back and look first numb okay and then how do we read it what is it it's a float so serial serial dot parse float which means read a float okay first num is equal to serial dot parse float so at this point we have our first num what do we need to do now we need to get our second number and I am just going to take what we just did okay and now what you got to think is we got to think right we got to think about using that LCD so I'm just going to copy this and I am going to paste it so we got our first number and then we come in we do an LCD print input this time it's going to be what second number and then this time we are going to read it into second number but we got to think about using this LCD print because right we printed across the screen and if we do a second print it's just going to run it off the screen so we really need to do what we need to set the cursor to zero again so where are we first num is equal to serial dot part okay before we do this we need to set cursor before we send anything there and it is a good practice in fact that we're gonna do an LCD dot clear I really should be making a lot of mistakes here so that she could learn more but I will just give you a heads up we're gonna clear it and then we're gonna set the cursor back to the row 0 column 0 or column 0 row 0 and then we're gonna ask for the second number I made a lot of mistakes when I was going through in lesson number 48 so I'll not try to deliberately make some so many mistakes this time but you know that she have to be very mindful in using the LCD it's kind of a dumb device so you've got to make sure that you've got your cursor in the right place so second number is equal to serial dot parse flow now I have two numbers what do I do now well I need my operator so what do I do ask wait read so how do I wait I'm going to say I'm gonna I'm gonna ask wait read how do I ask I am going to say again LCD dot clear and then I'm gonna say LCD dot set cursor at column 0 row 0 that's the upper left I need to spell cursor right cursor okay it's very useful how it turns colors for recognized command so if you're typing a command and it doesn't see it then that's kind of you know that cute clues you in that you've made a mistake so now what do I need to ask wait read so I'll do it LCD dot LCD dot print and what do I want to print please and put please in I'm not going to say I'm not going to be poetic I'm just going to say offer rate or and then I'll kind of clue it in that it's plus minus add subtract multiply or divide so I'm kind of telling the user what what is choices are so he kind of knows how what format this is is expected I hope that doesn't cause me problems trying to print a it's inside of a string so it should and I'm just worried whether it's going to try to interpret those parentheses as code but I don't think it will okay I'm now asked now I need to read so I do what was my variable name for the string my variable name for the string was Opie and so then here I will say Opie how do I read a string Oh P is equal to serial dot read string like that now what do I do what I do depends on what the operator is so what is the operator well if pop if operator equal equal what let's add first plus like that if operator is equal to plus start your clause with the open curly what do we do well answer would be equal to numb or what did I call those things first none you know what I'm not is so now as it might seem it's just I want to make sure that I'm not making mistakes so I check first num plus second none so there I would have the answer in that case that's all I would need to do I'll print everything mmm no I'm going to no I'll print everything at the end okay if operator equal equals what's the next case we're going to do - okay this case answer would be equal to first num - second num okay if operator equal equal what will we do multiply okay in this case ah that is very wrong open curly in this case a and STR II I don't know am I even spelling answer right to answer have an e on the end of it I hope so I am a terrible speller but I will tell you if you are good enough in math and engineering they will pay someone to follow you around spelling for you I can tell you that for sure you know what answer this seems like some people put an e on it and some people don't know I guess there is not supposed to be any on there how do you like that okay if you're a poor speller don't worry about it we'll have to come up here it's like I say I was a very poor speller and they just paid someone to follow me around connect correcting my spelling carnage that was going on okay so I digress that is that other nonsense okay if operator equal equals star answer equal first naam times second num and then finally if operator equal equal divide that looks good answer equal first not first num / second num now we've got the cases right so what you would do here then is you would now start to do your printing so you would say LCD dot print and I told you I want it to be nicely formatted right so you would say you would print first you know what we better put ourselves LCD dot clear clear the display and then we should do LCD dot set cursor and you guys might scream in Yeol because of this you don't need to do that but just to be clear I really like to be deliberate about clearing the screen and putting my set cursor so I don't end up with nasty surprises okay set cursor to column zero row zero now we will do LCD print and we need to say first num and then we need to say LCD dot print and then we need to say operator and then we need to say LCD print second num and then we need to say LCD dot print and I'm going to go Speight I'm going to go quote space equal space and then LCD print and a and SWE are without the trailing e even though if it were me I really think the Ebel longs there but I won't get into this have you ever wondered who gets to decide how things are spelled like who is there a person that roams the earth that is the spell captain who says answer doesn't have an e and we or is it that we do it that way because that's the way they did it that that's the way they did it and then they did it that way before but then who was it originally that got to decide that answer does not have a trailing e like if he's asked me who discovered calculus well you know Sir Isaac Newton and it makes perfect sense we go back and we see why the limit as Delta X goes to zero of X of f of X plus Delta X minus f of X over Delta Emme we know that we know why that's true but we don't know why answer doesn't have an e on the end of it all right if you if you can help me understand this leave a comment down below but I digress okay I think that looks pretty good let's run this thing and see what happens you guys are probably screaming at me for all of the errors hell hold your breath Oh what is wrong with that Oh second num is sec okay remember to be short okay hold your somebody didn't hold their breath that's why it didn't work ah look at that okay well first of all I see I have a little problem because you see I use more characters than I have but let's go ahead and play with this and what I'm going to do is I'm going to open up the serial monitor do I have a serial monitor shot here that will show everything that I want do I have a serial monitor shot in the house this one maybe no I have so many shots here so many different views let's see here okay how about this okay so in this shot you can see my serial monitor and you can see that okay so input input your first number let's say 25 okay then it says input your second number that actually looks pretty good input your second number put two okay and then input the operator now we have like a complete breakdown what did I not do I must not have on the operator asked wait read so let's come back to a different view let's come back over here to this okay do you see how it didn't wait for me to get the operator you see I didn't I asked for but I didn't wait and I read immediately whether there was anything there or not so I say wow did you guys catch that leave me a message down below saw you didn't wait for the response while cereal dot available equal equals zero guys normally when I'm coding I'm a lot more careful but I do allow myself to be a little distracted and a little a little sloppy because as I make mistakes then hopefully one of the most important things you're learning is how to troubleshoot when something doesn't work alright so that's a sweet read also let's clear up those other things where the first message input first number that was too long so I'll put 1 St you learn like clever abbreviations and then input second number 2 nd like that so let's try that okay input first number let's see if we can go back to a serial monitor view if I can find my cursor if you guys only knew how complicated all this nonsense was all right let's see if I can get my serial monitor back up here my serial monitor okay now you can see where was that most excellent view I had where you guys could see the serial monitor I really need to go out and clean out some of these different shots that I have I think it's this one okay let me make that a little bit smaller so we can see okay so input the first number what is the first number 25 so far so good that looks good so I'm inputting 25 okay input second number two okay and then input the operator I'm gonna have to adjust that you see that was that was too long but let's just put plus just to see if this thing is working and look at that 25 plus 2 is 27 well that counting down below what that nonsense was was that was do you remember that I had that extra code down there the time has come to take that crazy code out and so let's come down here and this extra code that we had with this counting let's take that out okay and then what I'm going to put is a nice little LCD dot set cursor cursor and I'm gonna go to column zero row one which is the second row and I'm going to say LCD print and what I'm going to LCD dot print is going to be thank you I want to say thank you for using the calculator but that's too many too many characters so I'll just say thank you like that now we got to think we don't want to clear immediately after that so we better put a delay and how long do we want to delay where they can see their answer before it goes back in loops again let's delay five okay then let's see up at the top when we start we set the cursor so down here we better clear it LCD clear what am i doing all this just so you sort of seed you got to think a little bit about using this alright so now let's try this we're going to come into okay input first number that looks good we I'm not going to go back to the view I will just tell you what I am inputting rather than have to find that shot again so I'm going to input 13 okay input second number 12 what is your operator forgot to fix that plus it printed nothing at all okay let's go back and look at the code now so I LCD set cursor first nom operator second nom o5o me and I was in Python where that would have been five seconds this is in millisecond so five thousand all so let's get that second that operator which was too long I will just say input like that okay let's see if that works all right let's see if this works this time maybe we should go to an overhead view I'll get out of your way okay input first number where am I in putting it on the serial monitor you know that right okay so I got the serial monitor what am I going to input I'm going to input the number three okay and then it's asking me for the second number I'm going to put two okay input okay that's not plus minus I think that's pretty good I'm gonna put a divide to check you guys that are not using floats three divided by two equal 1.5 thank you boom okay now it asked me again so let's try 3.14 4 pi okay now let's do a two and we're gonna find out what two times pi is and so I will do eight times 3.14 times two is six point two eight boom alright that is working okay now is one of the really big things that I'm going to tell you because I teach high school students and this is a mistake they always make they check their work this much and then they don't check it anymore and then when I come up and check them I really try to break what they're doing I try to do the special cases I try to do things that maybe they had not thought of and and so they do plus and minus and they it seems to work they don't check all things you as a programmer need to exhaustively check your code so on something like this you need to try add subtract multiply divide how about this like what if I say first number is a three okay second number is a minus two okay and then I'm gonna do a minus three minus a minus two will it know how to do that three minus negative two equal five okay that's not beautiful formatting but it did that case okay first number let's say at two point two five you know do it do some floating point make sure this this stuff works 1.5 1.5 okay and then let's do a times this really looks pretty good guys this really looks pretty good okay uh okay this is how I would try to break someone like 3.14159265 okay and then three point two three four five three okay and then times okay well it looks like the LCD is pretty smart about the number of places that it prints and so I was hoping that I could make like it would print out a really long number and then overflow but it seems like it ran its rounding it - it - like - two significant figures so that is pretty that is pretty good okay guys hopefully you had fun in this in this lesson and hopefully you guys did a good job and I really really really really really hope that you declared those those variables as those variables as floats and not and not not integers okay that's that's pretty good that's pretty good I'm trying to think of what would be a good homework assignment for for next time okay what I want you to do for your homework assignment for next time is I want you to create a device using the LCD display that would calculate the area of a circle and so you're going to ask the person what is the radius of the circle and then you're going to calculate the area of the circle and then you're going to display on the screen the area of your circle is and then the answer and so the area of your circle is you're probably going to have to wrap around to the next line and then give the area of the circle okay and then next time we're going to jump in and start doing some little more a little more you know adding adding adding some more things because what we've talked about here is we've gotten you in these last two lessons comfortable with using the LCD display but the whole purpose of the LCD display is to actually do something with it okay is to actually do something with it so leave your LCD display hooked up because we will be doing something with that okay palma quarter from top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 36,355
Rating: undefined out of 5
Keywords: LCD, Arduino, Calculator, STEM
Id: YdWiiM96-yM
Channel Id: undefined
Length: 33min 19sec (1999 seconds)
Published: Tue Feb 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.