is arduino c++
Programming wise, like always all of the code, is available in the description underneath the video. I am also going to link to a kit that I am going to be using and, of course, you’re. Gon na need an Arduino Uno. If you’re going to do this tutorial or something like an Arduino Uno and you’re gon na have to connect it to a USB to your computer and everything works exactly the same on Mac as well as on Windows, and I have a lot to do so. Let’S get into it, okay, so, in regards to software what you’re gon na need, I was going to use Visual Studio code, but I thought there was a very likely chance. It might change. This is going to be the arduino ide, and this is what I’m gon na use. However, you can use Visual Studio code. It works pretty much exactly the same way. Alright, so Arduino dot CC and main idea main software or you can just go to google and type in or do we know IDE, and you will see this tutorial and this Arduino IDE is what I’m using and you can see it’s available here for Windows and Mac and Linux and everything works exactly the same. Alright, and here is the Arduino software or were using, and this on the right side is the serial monitor. You were going to be able to communicate with the Arduino and have it communicate back with us and whenever you go file new you’re going to get this open up.
It’S gon na look exactly like this, and one thing you’re gon na want to do is, go to tools and then board and make sure you have Arduino Uno picked, which is right here and then over underneath ports you’re going to want to check one of these Ports to see which one works for you alright, and whenever you do that, everything will be ready to go, and I am ready to write some code. Ok, so what I’m gon na do here first is I’m, going to define a constant, and that means this value is never going to change and I’m going to call this an integer, because I am going to be storing a value inside of it and what I’m Referencing right here is an LED, pin the Arduino board. It is a light that’s going to light up on your Arduino and integers are going to have a value between negative 30 mm and an upper value of positive 30 mm actually it’s slightly more than that. But I’m going to keep it that way and then you will know don’t go under 30 negative 30 mm don’t go above no positive, 30 mm okay. So we have an integer here and I’m saying that the pin that I want to reference is 13 and this is outside of any of our functions. This is a function, this is a function, and so what that means is this is what we call a global variable, meaning that this value is going to be accessible to all of the other code in our program, and you do not want to have a lot Of these in your program, because it can become confusing because there’s values changing all through your code, you want to pretty much keep everything inside of functions themselves, so that’s the basics there and inside a set up, as you can see, put your setup code here.
This is where you’re going to initialize or set values for different things that are important. Another thing is: we want to be able to talk to the serial monitor once again it’s over here, and to do that you have to start off by going serial begin and 9600, and what this means is. This is going to open up a port that’s going to set how many bits per second and 9600 is equivalent to 960 characters per second and it’s, pretty much a standard, so that’s what I’m going to use here now, if we want to be able to light Up the LED and, of course, you’re going to have to have a USB plugged into an ordinal. You know into the back of your computer to be able to do this. But if you want to be able to use your LED to turn the light on and off you’re going to reference, your pin that we have there, which is 13 and then you’re gon na, have to say that you want to use it for output, which means That you want to be able to change the value, and I just said that I want to save this and I’m just gon na call this or nino tut and save okay, everything’s the same on Windows and and Linux all right. So after we have that all set up, we’re going to be able to come down here to the loop section and, as it says, you’re gon na put your main program code here and it’s going to be run over and over and over again.
Hence the term loop. Now what we’re going to do is this is a global variable. We’Re gon na do is we’re gon na create some local variables that are going to only be available inside of the loop function. One thing we’re going to do is we’re gon na turn our LED on and off. So we are going to define another integer and we’re gon na say how much time we are going to delay the well we’re gon na turn the LED on and then we’re going to hit. This is a tenth of a second it’s gon na be on, and then it’s gon na go off again: okay, so that’s what that is 100 milliseconds. We are then going to say that we want to turn our pin on, which is the same as the LED and to do that we go led, pin and we say hi, and that just means that it is on you can see, LED pin is referenced there. Both times, we are then going to say that we want to have it stay on for a certain period of time, and that is going to be our delay period. And we can say that and then after that, we’re going to say that we want to once again turn the LED pin off and to do that, we say low, and this is a standard way just to test that our Arduino is working properly. For us. Okay and then after we have that, we turn it off.
We want to once again have it be off for a certain period of time, and then it is going to Bri start up here, turn on turn off turn off over and over and over again and now to test that our code actually works. We want to have it saved, then we’re gon na come over here and click on verify and you’re going to see down here. If there’s any errors, there are none, so you’re gon na click on upload and whenever you do you’re going to see that your Arduino is going to blink a couple times and then it’s LED is going to blink pretty quickly on your screen. Now what I want to do is I want to increase the delay period between the LED blinking and then I want to go back or and have it speed up and slow down, speed up and slow down over and over and over again. So how am I gon na do that? Well, I am going to just come inside of the code that we already wrote and right after this guy, I am going to put some more code in here. So what I want to do is I’m gon na say if my delay period is equal to 1000, which is going to be the maximum amount that I want to wait or one second or so. This is a condition, and this is going to be another condition. The delay period is equal to zero, which is going to be the minimum that I want it to wait and there we go, and you can see make sure you have all your print as he’s closed.
So, if either of those is true, I want to go and say that I want a variable called direction to change so I’m going to multiply this times negative one, and that is exactly the same as if I would say Direction is equal to Direction times negative. One, so that is going to change that that little variable for me, but as you can see this function, we have here starting to get kind of long and we’re gon na be adding even more code to this. So what I want to do is create another outside function that I can then call to keep my functions a reasonable length overall, so I’m gon na start off using good coding practices here right from the beginning. So up here, right after this variable has been declared. I am going to mend and I’m gon na create an another integer and this is going to be count direction and I’m gon na have it set to one first off, and maybe we put a space there to make this a little bit more readable and then I’M gon na take this guy right here and cut him right out of there and I’m going to go count. Direction is going to be equal to and I’m gon na, create a new function called check, Direction, change and I’m gon na throw into that I’m gon na pass some values over to it, which is going to be our delay period and our count direction and it’s Going to return information back to us so let’s create that function right now.
This function is going to turn an integer, of course, because this is an integer, so that makes sense and I’m just gon na say, check direction, change and this delay period is an integer. So I’m going to have this be an integer and I’m just gon na. Let it have the same name, sometimes that’s a good idea, sometimes not so much and then int count direction, and these are going to be completely different variables from these variables. So, even though the name is the same and then inside of it I’m going to paste in that code, I started throwing inside of there and I’m gon na change this from direction to count direction and then I’m going to continue I’m gon na say if an If statement is going to perform an action, if a given condition is true or not so I’m going to say, if the current count Direction is less than 0, which means my delay period is 0 or greater. Well, in that situation, well, I’m going to write out to my serial monitor that a change is going on so and that’s going to be over here so that I can see it and know what’s going on, even though the Arduino is just sitting here, blinking. So I’m gon na say going down and then close that off. If this doesn’t totally make sense, it will, as the code starts executing and things start popping on the screen again, I’m going in the print line, means that we want to put a new line over here on this side of the screen.
Each time we print out some code, so there are there’s a line break between each thing that prints out on the screen and then after I do that, I can say, return, count direction and close that off and now that I have that function defined what I Can do up here is right after this guy and once again this is in my loop function, I’m going to come in and I’m going to just change my delay period – and this is gon na – be plus equal to 100 that’s. What 100 milliseconds times the new count direction, and once again, that would be the same as DP is equal to TP plus 100. Okay, where DP is the delay period, this guy right here, all right, so that’s, just a shortcut way of doing that so that’s going to allow me to increase and decrease the amount of delay that I have between the LED turning on as well as turning off And let’s say I also want to print out a message over here on my serial monitor, so that I can also get a little bit more information, so I’m going to say print, and here that means I do not want a new line after this so I’m. Going to say what is the new wait time and close that off and then afterwards we can throw in a new line and then we’ll say delay period and there that is now. I, on purpose, made a mistake so that I can show you what’s going on.
So we’re going to go in here and compile that and then load it and you’re gon na see over here on the right side of the screen here in a second new wait time, and you can see that it’s 200 forever. Why is that? Well? The reason why is each time we come in here and we go whenever we loop? These values are going to be reset, so that’s going to be reset to 100 and that’s gon na be some reset to 1 that’s a problem because we are changing those values down inside of here. However, they’re not going to go and turn the light on on the LED on and off like we want to, because these values are being set to their original value each and every time. There is a way around that, however, and the way around that is the delay period is what we want to be able to change. So what we’re going to do is we’re going to come in and we’re gon na put static in front of that, and what static means is that we only want to initialize or set a value for this one time and whenever we come into loop. The next time we do not want it to be reset to 100. We want to start it off at 100 and then be able to change it each time through the loop so once again, we’re going to click, compile and we’re gon na run it, and this time you are going to see on your LED that the wait time Is going to slowly increase and I’m going to show you my are doing doing this here in a second, so it’s increasing, and then it is going to see going down well, it looks like we have a little bit of a bug.
Let’S go in here and fix that. Well, so, did you catch the bug kind of did that on purpose just to test you out there and see if you were paying attention I’m, also going to have to have the count Direction set as static as well? And if I do that and re upload, you’re gon na see that is going to work exactly as we had planned, so the wait time is increasing and then once it hits 1000 it is then going to turn around and the wait time will start decreasing. Just like we see right there and now I’m gon na jump over and I’m gon na show you my Arduino and exactly what this looks like on the Arduino and then we’ll get back to write more code. You can see here on the Arduino. It is going to get slower and slower and slower, and then eventually it will start speeding up, just as we added programmed it to do. Okay and we’re back one thing I wanted to mention is right here where we said we were going to execute this code. If this was true – or that was true – I just wanted you to know, though you can also put two ands inside of that or ampersands, and that would say that we’re gon na execute. If this was true – and this is true – but in this situation we only need one of them to be true for our code to work all right.
So let’s come back up inside here and I’m gon na go and do a couple more things. I’M gon na create a function inside of here that is going to do a countdown before it starts manipulating the code and I’m gon na pass the value of five, and that means it is going to go and it’s gon na print 5, 4, 3, 2, 1 And then go and then it’s gon na start executing this code, so this function is gon na come down inside of here and I’m going to start writing it. Okay. So this function is just going to perform an action, so it doesn’t need to return a value when it executes. So in that situation, you’re gon na type in void and it’s called countdown, and it is going to receive an integer which is going to be the nack maximum number that it wants me to print and then count down from and then do whatever. Now, what I’m gon na do is I’m going to introduce a for loop that’s. Basically, what this guy is right here, this looping structure that we have. However, this for loop, instead of executing indefinitely, is going to execute a certain number of times, and that is going to be the max value. So we’re going to define another integer inside of here call 2i we’re going to say that it has a maximum value of the max they passed in and it is going to continue to execute.
As long as the value of I is greater than the maximum value and with four loops, and then on top of that, we are going to increment the value of I each time through, and this is the same as to doing eyes equal to I plus 1. All right and there that is and make sure you have your curly brackets, set up right now, with 4 loops we’re going to be able to perform conditions on greater than less than greater than or equal to less than or equal to and that’s, basically it. But of course, we’re going to be able to do and conditions or can ditions if we want to have multiple conditions. Okay, so we’re gon na come down here in a for loop and we’re gon na say we want to print and we’re gon na print out. I – and this is gon na, be our countdown and then we’re going to say serial print once again and we’re gon na put a comma inside of here and then close that off and then after it counts down. We are going to finally say serial print line and we’ll say go inside of it, so it’s like a silly little countdown, and if you want to force a newline inside of here on top of the other newline, we can just go like that and there we Are okay, so we got the function created and we called the function right up here before we did anything else, and now we can execute the function.
You can see what it looks like so it’s, just a simple little countdown and it didn’t print, because this actually should be zero and I’m decrementing. For god, I was decrementing, so let’s go like that, and then let’s run it again and you’re gon na see that it works properly this time and there it goes alright. So there is another example of a function and something we can do with for loops. Now there are other ways to loop inside of C using Arduino. Another way is what’s called a while loop and a while loop is going to continue looping forever. As long as a condition is true, they one thing is you’re gon na have to define the variable you’re going to be working with outside of the while loop, so I’m gon na, say wow. The condition of J being greater than zero is true. I want to go and print out, let’s just go and copy the code that we have right here so there’s that I want to continue going and printing out these values except we’re, gon na change that to J and then we’re going to have to decrement J. Underneath of it so the while loop and the for loop are of very similar fellows so let’s go get that and then we can go and throw this right here. And if we do that and upload it to our Arduino, you’re gon na see that it’s going to do a count down from 3 instead, alright, so there’s an example of a while loop.
Another thing we can do is let’s talk about some different values. We can work with here, and one of them is called a float now. A integer is not allowed to have decimal values. However, a float is allowed, so we’re going to say that we want to create a function. Call down here called add, floats that’s, going to return a float and it’s going to be number one and we’re gon na receive another floats, which is going to be number two and it’s just going to return those numbers added to each other and you’re going to See here how precise this is, which it’s not terribly precise and make sure you have the closing curly bracket, all right so add floats, is what we’re going to be calling and we can come up inside of here right after we printed that and paste this inside Of there, and just for the heck of it, let’s go and create another. One called add, doubles now add floats and doubles, unlike in many other languages, are exactly the same in our Arduino and what we’re gon na do is we’re. Just gon na call this and doubles and change this into a double as well, and this into a double as well and I’m, going to show you through an example that they are the same thing. So add, doubles and we’ll come back up here and we will go and throw that in there now. What I want to do is I want to go and do a print out to our screen and inside of this I’m going to say three point and then I’m going to go and create eight ones plus and a four and more ones.
Eight ones again is equal to and then this right here and then I’m gon na call these two functions and if you’re used to other programming languages, you might think you’re gon na get different results, but you’re gon na see with Arduino that you get exactly the Same results, so I’m gon na call, add, floats and then I’m gon na pass those values inside of it so let’s just go. You can also see that you can go and call functions from inside of your print statement without any problems at all and dude. This guy right here and paste that inside of there and whenever you see whenever I close these parentheses you’re gon na see a little blue box, shows up that’s, just a nice, easy way to see that they have changed. And why don’t we just go in here and just do this again and paste that there and doubles and then we’ll just go and add this part onto there. That works perfectly fine, but what I’m also gon na do with the print line is I’m going to say that I want to have eight points of precision for my doubles and how I can do that, and my floats is, I just put a comma inside of There and then follow that up with an eight and now, if I run that you’re gon na sell this other stuff pops up here, but you’re also going to see the precision of a double as well as a float.
I mean, as you can see, they only have one two, three four six points of precision before they mess up and just so you can see. The float is exactly the same. So, whenever you’re working with doubles or floats just be aware that they are only precise up to six digits and then, after that they go absolutely haywire. We also have additional data types that we can work with and I’m going to show you all pretty much. All of them right now so we’re also going to have bullying’s, which can either have a value of true or false and I’m gon na go. Can votes is equal to true and then I’ll show you another way of doing things conditionally, and this is called the ternary operator so I’m going to say print and can I vote and and then follow that up once again with another print line and I’m going To put my condition inside of here, so I’m gon na say, can vote if it’s equal to true well in that situation. Now you’re gon na put a question work. If that comes back as true that condition, then the output is going to be yes, otherwise the output is going to be no and then I’m gon na make sure I close my parentheses and do that and then, if we run it you’re going to see that It’S going to print out yes next, to can I vote, and this is called the ternary Oh operator.
Can I vote yes, all right so there’s another thing we learned: what else can we do? Well? We can also store individual characters. So I’m, just gon na have letter D is equal to and now whenever, we’re, storing individual characters just be aware that they have to be surrounded by single quotes and, of course, you could go and print out that value. If you would like to all right and also other data types there are, this is basically it if you want to use values between 0 and 255 we’re gon na get more into binary and stuff later on, you can use bytes. You can also define unsigned integers which get to be bigger, remember the maximum size for an integer, 32000 it’s, actually, third, two thousand seven hundred and something but like I said you can see that doubled. We can also use Long’s, which are much longer and then unsigned Long’s, which are longer even yet. You can also see here an example of a comment. If you want to put something in your code, that is completely ignored, just put two forward slashes and for a multi line comment you’re just going to go and put a forward slash in a star and then you can have a multi line comment. So there we go and now I’m gon na start up another block of code. Now what I want to do is talk about arrays, and you can see here. I go and got rid of a lot of the code we had previously.
So what are we gon na do here? Well, basically, an array is just a box inside a whole bunch of boxes inside of an individual box, and an array can contain many different values and each of the values stored inside of it has an index or an address whatever you want to consider it. That starts with a 0, so let’s go and let’s define an array that is going to represent the number of blinks that we’re gon na want now it’s going to be a bunch of integers, so we’re gon na put in in front of it I’m gon na Call this number of blinks and to designate this as an array you just go and put those brackets inside of there and then I’m, just gon na, say: 1, 2, 3, amp, 4 and that’s one way to create an array and wants you to find the size Of an array that array size cannot be changed. Everything here can be exactly the same and let’s go delay period. Let’S change that to 1000, just that’s, one second and let’s say I want to change a value on the right. Can change in a value and alright, you go number of blanks and we can change the first one to say five. Instead of one which it was and we’re just equal to five all right, you can also define an array without going in and actually putting values inside of it. However, you’re gon na have to say how big you want it to be so that time I said, I wanted to have ten little places to store values inside of it.
You’Re also gon na be able to store a string of characters and so let’s go and create. That would be a character. All right and we’re gon na see more about strings here and more about all kinds of stuff pointers, even on all kinds of stuff. So there you go, and that is how we go and create a a character, alright and in actuality, a pointer just points to the first character in our address for our character array. So we come in here and also go character. Star STR is equal to and bananas if you would like, and there we go created that, like I said, we’ll do more with pointers here in a second. You can also print this string just by going serial and print line, and there is our string. Let’S go and I’m gon na do a whole bunch of different things with strings, and then you can see the output on the right side of the screen. After that, you can also define just a regular old string like this, so my name is equal to and I can type in my name there. It is you’re going to be able to combine strings so serial once again print line not able to do this with non strings, but you can do it if you’re working with two strings, so I’ll say name and then you put a plus sign inside of there And whatever the name of the other string is what else can we do make sure you spell serial correctly and we can also get the length of our strings? How many characters are inside of them and we’ll get back into arrays here in a second just wanted to go and cover this, because I was talking about strings there and we can also check for equality between our different strings.
So go print line and you would just put whatever the name of your string is, and then this is gon na return, true or false, depending upon. If the string is the same or not and we could go equals and then what you want to compare it to so this, of course, is going to come back as being a not a proper comparison. You could also go and do equals ignore case. If you wanted to ignore upper and lower case instead of just typing in equals, like I did right there, you could also get a character at a specific index. Remember that we are going to do scooped print line and once again just go and get your well let’s. Do I’m gon na show you a couple different things, my name and let’s say we wanted to do the character at the sixth position. Inside of our string. We can do that that way, we’re also going to be able to get to the index that matches a specific character that we have inside of it. So my name, and to do that you go index of and we’re gon na, be able to use substrings to find bigger things also, and you could also come in and remove a block of characters inside of it. So let’s say we wanted to come in and we wanted to start off, we’re moving things at the zero index, and then we wanted to remove a total of six characters that followed.
After that. You can do that. We can let’s go and print that out on the screen, since we changed it pretty dramatically and there it is. We can also replace one substring with another, so I don’t know if you noticed, but my name is not actually bananas its panis, so I let’s say I wanted to go in there and make a change to that so that I had the proper name. I could go replace and na s and then get rid or then put and just an S in place. We can do that. We can let’s go and print the name again so that you can see the change that are taking place and what else can we do with strings? We could also get a substring and print that out on our screen and to do that we just go my name and then substring and let’s say we want to start at the zero index and then get three characters. After that we can do that. We’Re. Also going to be able to change from uppercase change, every single character to uppercase so upper case, and likewise we will be able to change everything to lowercase so this and we could also convert a string to an integer or a float. So let’s go and create a string, call it one and put that there and remember: it’s double quotes for Strings and it’s single quotes for non strings, and then we want to put it inside of an integer and to do that we would just go one and Two int – and I said we could also do the same thing for floats just by typing, in to float with an uppercase F instead of an uppercase.
I and let’s also come in here and do a sum, and that would be number one plus number one we’ll be getting a lot more into math here in a second and then let’s just go and print that out so print line and print out or some. So you can also see that on the screen also. Likewise, I Creole through arrays, because we were talking about arrays previously, as you may have guessed, that is going to be done with a for loop. So I is equal to zero and we’re going to continue cycling through as long as I is less than 5 and then we’re going to go in and we’re going to increment the value of I. And if we want to go in and print those outs. And we can say print array 3 is what I called it, and and then that’s gon na print out all that, and that is array three, which is this guy, which is just my name: it’s gon na print it without putting new lines down each time and Let’S say I’m also going to want to go and blink. My LED a certain number of times, so I want to basically blink my LED like one and then one two, three four it’s actually going to be slightly different because I changed the value of our erasing. I like here, but I went and changed the value to 5, so our LED on Arduino is going to blink five times and then two times and then three times and then four I’m going to do that inside of a function and I’m gon na go and Get this port right here and cut that directly out of there just so we can go and create that so it’s not gon na return.
Anything so I’m gon na put void inside of there and I’m gon na call. This blink LED and it’s going to receive the number of blinks and put that there then I’m going to say 4 and int J is equal to zero and we’re gon na continue blinking. As long as J is less than number of blinks, and then we will increment the value of J, of course, and I’m gon na show you what all that all the stuff I just did with strings here in a second. We can paste that inside of there digital right, let’s go whoops, put that in there, so that everything lines up, it’s very useful to have all your code line up properly and then let’s just change delay period to 400 and 400. Just to have consistency with that. Guy and then what we’ll do is we’ll pop up inside of here and we’ll go and create another for loop and in AI is equal to zero and we’re going to continue running this. As long as I is less than four and then we will increment the value of I once again and then I’m going to call my function, blink LED and I’m gon na pass in number of blanks, and that is going to be the array that we created Very early on whenever we created this stuff same number of blanks, just remember it’s, going to blink five times. First, so we went and we called that and then we can go and put an additional delay between each of those guys and we’ll have delay period, which I believe I still have to find inside of here.
Yeah delay period. So there’s no need to be a full one. Second blink inside of there, while there’s gon na be a 400 blink between the blinks on our LEDs. Just trying to do some different things with Arduino and let’s go and run it and make sure up. I have a little bit of an error. Oh I see what it is. This is upper case and lower case. All right got that saved and now we can execute it, and everything should be working perfectly fine and you’re gon na see that all of that information in regards to what I was doing with the string Sprint’s out and you’re, also gon na see that your do. We know is blinking a set number of times based off of these values. Let’S just run through exactly what we have over here, so serial print line and it’s is going to print out, which one is that bananas that’s, because that’s bananas and then it’s gon na go name and it prints out that we’re going to say the total length Is 13, as you can see, on the right side of the screen? Is it true comes back as 0, which we are getting that right there whoops we got a little bit messed up, let’s run it again. Sometimes a serial monitor gets a little wacky. You can also see character at 6 and there is the index and then you’re gon na see all the different manipulation we did until we finally cut it down to be a n all right, and there is all that information about arrays and strings and now I’m Gon na cover a whole bunch of random things that I haven’t covered so far, all right so to keep everything simple I’m going to do a lot of this coding inside of the setup function here.
So what I want to do is I want to create a character, that’s going to represent a grade, and then I am going to go and show you the switch statement. Now. Basically, the switch is going to perform different actions based on a limited number of possible values. So what we’re going to do is we’re going to check the grade that we just entered inside of here and we’re, going to see what the value of grade is and then perform different actions. So we’re gon na say that, if grade is it has the value of capital a well in that situation, we are going to print out great, for example, and then to jump out of the switch statement and stop checking. We are going to put a break inside of there now. What we’re going to do is we’re going to do this we’re a whole bunch of other different potential letter grades or whatever you want to refer to the mass. So maybe the God of B and we’re gon na say good and we could continue on and in the situation in which none of the conditions are true, we’re going to put in default, and then we are going to come in and print out a different message. So we’ll go to default and we’ll put something like confused inside of here and then break and then that’s it all right. So I think that’s the simple way of understanding how a switch statement works, and you can see here if I load it over that it’s going to print confuse, because I have a Z inside of there and nothing else matches now.
I want to go and take a look at the while loop again and I’m gon na cover a couple other additional things, so we’re gon na create two, this guy that is going to determine when we leave our while loop, so I’m gon na say that I Want to continue looping as long as I has a value that is less than 20, then what I want to do is if I only want to print odd values. What I can do is, I can say, if I – and this is modulus and what modulus is, is it returns the remainder of a division? So in this situation, if the value is multiple of two or even it’s, gon na return, zero and zero and C is exactly the same as false. So we’re gon na come in here and we’re going to pronounce. I, if that is true, that it is in a odd value and then we’re gon na increment. The value of I very important to do that then well, let’s go on I’m gon na show you something else. Let’S say that I then want to jump back to the top of the loop up here and to continue with our while loop but skip everything that follows afterwards. I can just type in continue and it does that for me now. What I’m going to do is put in another condition inside of here, and this is gon na loop as up to the value of 20, but let’s say I mean this is obviously a just a silly thing to do something, but let’s say that if it hits 14, I want to break out of the value that is going to break completely out of the while loop and come down here right after the while loop ends right after this, and that will be the end of that then I’m going to go and increment.
I, and if I run it, you’re gon na see that it’s going to print out all the odd values and stop whenever it hits 14, which is exactly what it did now to save time. I’M gon na cover a whole bunch of math functions, so let’s just go through them one by one. Of course, we can add values, we can subtract, multiply, divide and do the modulus and remember it is it gives you the remainder of a division. We can do absolute value. Of course, hasn’t been a return it to a positive two. We can just go and run that you can see the results over here on the right side of the screen. Men is going to return the minimum of these two values, which is gon na, be two point. Five max is gon na return to three. Of course, rounding is going to round up in this situation to a three anything over five. You know 0.5 it’s gon na be rounded up. Floor is gon na round down everything and ceilings gon na round up everything. So, even though this is 5, this is going to turn into a 2 that’s going to turn it into a 3. This is square root. This is square, this is the power sis is going to be 5 to the power of 2 cube roots, and then we can also calculate logarithms, and you can see both of those guys, and you can also see the results of those difference.
Calculations math functions and you can go and get the code it’s free in the description. If you would like to take a closer look at that so I’m, trying to save time. So now what I’m going to do is I’m going to cover something else which is constrained and what it does is it allows you to constrain a value between a range and what I mean by that is if this is going to be the function, the different Pieces we’re gon na throw inside of it so it’s, going to return the value of X if X, is between a and B otherwise it’s gon na return a if X is greater than or less than a so in that situation, it’s going to constrain everything between Here, unless it falls in between so let me show you an example: I’ll show you exactly how it works, so let’s go and print out this information and we will just simply say constrain, and this is very useful in electronics and five and one and six and Throw that so there and of close your parenthesis, of course, and then we can run it and you’re gon na see that it goes and prints out the value of five. Why? Because five falls between one and six and you can go in there and play around with that to see some other things, but later we’ll do some real live projects. Also we’re gon na have some trig functions, of course, so those will be quite useful, especially in electronics, and there are all of those – and another thing we can do is let’s say we want to generate random values.
Well, what we’re gon na have to do is we’re gon na have to create a random seed. That is basically, if you just call for a random value, it’s going to be the same every single time the program wants. So what we need to do is initialize. The random number generator we’re going to be using here in a second with a random seed value, and what we’re going to use is an unconnected pin which is going to provide us with a random value and the reason. Why is it’s going to be floating value and we’ll get more into that later, so then let’s say I want to go and create a whole bunch of random numbers, so I’m gon na go. I is equal to zero and we’re gon na cycle through, as long as I is less than ten, so we’re gon na create ten random values, we’re gon na increment it of course, and then to actually go and create our random value. We’Re, just gon na say random and we’re gon na say that we want a value between 1 up to 10, not including 10. So it’s gon na give us 1 through a 9 whoops. What did I do here? I forgot to put 0 inside of there. So let’s save that and run it again and also that has to be uppercase and let’s run it again and you’re gon na see that it shows and it generates 10 random values all right.
And what else are we gon na? Do let’s talk about bit? Manipulation, of course, that is very, very useful with electronics. So, basically what we are going to do well, then, I have to cover the basics here: sorta. Basically, there are eight bits in bite and each bit is going to be a binary digit, meaning it has a value of 0 or 1 and I have other tutorials on how binary works. And if you leave a comment below I’ll, go and I’ll link to it. But basically, I’m guessing that, if you’re into computers, you know a binary works. Basically, this is how we’re going to be able to calculate different values. So if we have 1 1 1, that is going to be like 1 to the 2 to the power of 3, 1 2 to the power of 2, 1 2 to the power 1 and 1 to the power of 0 and those if they are all ones, Are going to give us a result of 8 for 2 amp 1, if we add all those up, that’s, 15 and that’s, how we go and convert from base 2, which is binary into base 10, which is what we all use so let’s go and create some Of these guys, let’s go and binary 1 is going to be I’m gon na have them set as integers. If we want to actually use binary, what we’re gon na do is just put a 0 and then a B in front of it.
So we could come in and say this like that, and this is going to be equivalent to 170 trust me on that or you can go. Do the math and let’s go and let’s create another one and let’s just have every single one of these be 1. Ok, so there’s 4, all right now. What we’re gon na do is do some bit manipulation. Okay, so looking if we go into bin 1 and then 2, what its gon na do is it is going to return a value only if both of these are once. Okay, so in those situations, it’ll be 1 in this situation, where there’s a 0 it’s going to remain 0. So what do you think the value is going to be take a wild guess and if I didn’t type it in wrong, you would be able to see I’ve been to and let’s run it again and you’re gon na see that the value is going to be 170, exactly the same as what I had right here, all right and there’s other bit manipulation tools available to us. Let’S just go and copy this, and the ore is going to return a 1 if either one or more one. So guess what the return value is. Gon na be for that guy let’s go into a couple more there’s, also excluse, for which is going to return a one. Only if one is one and another is zero, and that is how we do exclusive, or also going to have another guy inside of here, and the bitwise knot is basically going to convert each to its opposite.
So, no matter what it is, it’s going to give the opposite of that and we’re gon na throw a tilde inside of there and let’s just go 0 B, 0, 1, 1, 1 and that’s actually going to give us a negative value and the reason why it Gives us a negative value is the top bit in any number is what is called a sign bit and whenever it is changed to a one that makes the value negative so that’s. The reason why we add that we’re also going to be able to come in and shift bytes, so let’s come in and let’s go let’s. Do it to been to let’s, say we want to shift bin 2 by 2 over that’s. How we do that and we’re. Also going to let’s copy that and paste that there let’s shift it over to two bits to the left, and this is gon na give us well it’s gon na turn. It into put a comment inside of there: 0. 0. 1. 1. 1. 1. 1. 1, because it shifted it to the right so that’s going to give us a smaller number, while if it went to the left it’s going to give us the full amount, this guy right here and then 2 zeros on it, and if we run it you’re gon Na see that, in fact, that that is true and that’s, all we’re gon na do with bit manipulation right now and now, I’m gon na talk about Struck’s now, basically, structs allow us to create custom data types, so a custom data type that would be useful would Be RGB so let’s say we wanted to have a red represented inside of it and a green, and this could be used with an LED to go and change different colors make sure you put a semicolon here down at the end and make sure you put it Semicolon there so that that works and what that’s gon na allow us to do is we’re gon na be able to go ahead and use our custom data it’s to define a color and then we’ll be able to assign those values.
So let’s come and do this and there we go and then we could do something like if a color is red equal to and color is green is equal to 255 and color blue is equal to zero. Well, in that situation, we could then say that, yes, indeed, we are dealing with the color, green and it’s screen and there it is, and indeed it is green and you can see that it’s gon na print out its green after it does that alright. So a quick example with strux and now let’s talk about s printf so let’s say you know. Whenever we’re, using the regular serial to print out things, it’s very hard to style the information, the way that we want to so, for example, let’s, say we wanted to print out a time with two values and a colon in the middle of them. Well, let’s go and do that, so this function is going to receive an hour. You could also do this with seconds or whatever so there’s that guy and it’s not gon na return anything that’s. The reason why voids there, so what we’re gon na do is we’re. Actually gon na create what is a buffer, which is going to store some information for us, which is gon na be the time. So we can go, do that and then I can call s print F and buffer, which is where this string is going to be stored at and then inside of it.
What we can do is let’s say we always want to have two digits guarantee with it. We have two digits well, we can go in there and put o 2 and D, meaning that this is an integer. Then we can go to inside of there and then close that off and then what we can do is put our and the hour is gon na go here and then we can put in minutes and the minute is going to go right here. So that’s a way for us to style our stuff, so you can end goes prints, a and print or buffer and it’s going to print it. The way that we want it to and now we can jump back up inside here and we can just go Prince time and then pass in one whoops and twelve and print time. We are and run it and whoops s not do Prince F there we go and let’s run it and you’re gon na see that it’s going to print out the time. The way that we would hope that it would print it out and it does all right. So good stuff now let’s talk about pointers, which is something that confuses a lot of people, but it really shouldn’t it’s. Not that confusing. So give you a couple examples and basically a pointer. It just refers to a memory. Relocation for a variable, so let’s go and create one. So this is a pointer.
I put the star in front of there to represent that. That is a pointer, then I’m gon na come in and I’m gon na go value one and say that it is equal to 15. Now, if I want to store the memory location for a variable in a pointer, I just go pointer is equal to and about 1, and then it is in there, and I can come in then and print out the value just by putting the star in front Of the PTR so switches it back and you’re gon na see that it prints out to the way that we expect it to and there it is there’s 15. So what else can we do with it? Well, let’s go and create an array of primes, so it’s equal to 2, 3, 5 amp 7. Alright. So there is our RA and let’s print you can print. Basically, an array is actually a pointer and you can actually quite simply come in here and print. The first index, just by going in like this and primes and run it and you’re gon na see that it prints out a 2 on the right side of our screen, as it did another thing, that’s kind of cool as we can go and get. The second item, if we would like out of the primes by just coming in here and going one like that – and we will get the second value, which is 3, which will show up on the right side of the screen, so cool stuff and let’s do another.
One let’s say we want to go and print out an array. Well, we can go void and prints array. We want to pass a array into here, we’re, just gon, na go and designate that this is an array being passed inside of it, and we can close that off like this and then we can create a for loop. Int is equal to 0. Well, I is less than size and plus plus the size is going to represent. The number of items that are inside of our array. We’Re gon na have to pass that information inside of there and then we can go in and print out.
is arduino c++ Video
is arduino c++ news
Posted on Tuesday October 15, 2019Speeding the Idea-to-Prototype Process with the Arduino Open-Source Ecosystem Electronic Design … Continue Reading »
Posted on Friday October 04, 2019BASIC Compiler & Programming on Arduino Zero Boards (Crowdfunding) CNX Software … Continue Reading »
Posted on Friday October 18, 2019Python programming language: A cheat sheet TechRepublic … Continue Reading »
is arduino c++ Social