Arduino Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to my Arduino C programming tutorial and this one to Touro I'm gonna cover pretty much everything you can do C programming wise with an Arduino now of course I'm not gonna be able to cover all the different types of hardware but I will cover a lot of different hardware in regards to how we're going to use an Arduino but I will cover pretty much everything you can do 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 gonna need an Arduino Uno if you're going to do this tutorial or something like an Arduino Uno and you're gonna 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 gonna 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 gonna 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 gonna look exactly like this and one thing you're gonna 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna do is we're gonna 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 gonna turn our LED on and off so we are going to define another integer and we're gonna say how much time we are going to delay the well we're gonna turn the LED on and then we're going to hit this is a tenth of a second it's gonna be on and then it's gonna 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna create an another integer and this is going to be count direction and I'm gonna 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 gonna 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 gonna create a new function called check Direction change and I'm gonna throw into that I'm gonna 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 gonna 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 gonna 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 gonna change this from direction to count direction and then I'm going to continue I'm gonna 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna see that is going to work exactly as we had planned so the wait time is increasing and then once it hits 1,000 it is then going to turn around and the wait time will start decreasing just like we see right there and now I'm gonna jump over and I'm gonna 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 gonna 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 gonna go and do a couple more things I'm gonna create a function inside of here that is going to do a countdown before it starts manipulating the code and I'm gonna pass the value of five and that means it is going to go and it's gonna print 5 4 3 2 1 and then go and then it's gonna start executing this code so this function is gonna 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 gonna 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 gonna 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 gonna come down here in a for loop and we're gonna say we want to print and we're gonna print out I and this is gonna be our countdown and then we're going to say serial print once again and we're gonna 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 gonna 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 gonna have to define the variable you're going to be working with outside of the while loop so I'm gonna 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 gonna 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 gonna 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 gonna 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 gonna do is we're just gonna 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 gonna call these two functions and if you're used to other programming languages you might think you're gonna get different results but you're gonna see with Arduino that you get exactly the same results so I'm gonna call add floats and then I'm gonna 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna say can vote if it's equal to true well in that situation now you're gonna 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 gonna 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 gonna 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 gonna 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 32,000 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 gonna 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 gonna 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 gonna want now it's going to be a bunch of integers so we're gonna put in in front of it I'm gonna 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 gonna say 1 2 3 & 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna print out all that and that is array three which is this guy which is just my name it's gonna 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 gonna 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 gonna return anything so I'm gonna put void inside of there and I'm gonna 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna see that all of that information in regards to what I was doing with the string Sprint's out and you're also gonna 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna 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 gonna cover a couple other additional things so we're gonna create two this guy that is going to determine when we leave our while loop so I'm gonna 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 gonna return zero and zero and C is exactly the same as false so we're gonna 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 gonna increment the value of I very important to do that then well let's go on I'm gonna 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 gonna 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 gonna 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 gonna 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 gonna be two point five max is gonna 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 gonna be rounded up floor is gonna round down everything and ceilings gonna 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 gonna throw inside of it so it's going to return the value of X if X is between a and B otherwise it's gonna 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 gonna 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 gonna 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 gonna have to do is we're gonna 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 gonna go I is equal to zero and we're gonna cycle through as long as I is less than ten so we're gonna create ten random values we're gonna increment it of course and then to actually go and create our random value we're just gonna say random and we're gonna say that we want a value between 1 up to 10 not including 10 so it's gonna 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 gonna see that it shows and it generates 10 random values all right and what else are we gonna 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 & 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 gonna have them set as integers if we want to actually use binary what we're gonna 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 gonna do is do some bit manipulation okay so looking if we go into bin 1 and then 2 what its gonna 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 gonna 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 gonna 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 gonna 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 gonna give us well it's gonna 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 gonna see that in fact that that is true and that's all we're gonna do with bit manipulation right now and now I'm gonna 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 gonna allow us to do is we're gonna 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 gonna 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 gonna return anything that's the reason why voids there so what we're gonna do is we're actually gonna create what is a buffer which is going to store some information for us which is gonna 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 gonna 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 gonna 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 gonna come in and I'm gonna 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 gonna 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 & 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 gonna 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 gonna 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 gonna have to pass that information inside of there and then we can go in and print out each of our array items just by using our index notation like that and that we can come up inside of here and just go print array and how we pass our array inside of it is to go crimes and then the total number of items which is 4 and everything is working the way that we would hope and it's gonna print out all of those prime values ok guys so there is a rundown of the vast majority of what you're gonna do with just the core C language using Arduino I hope you guys found that useful and I'll be doing hardware videos you know connecting LEDs talking about electronics just making a ton of different stuff all based off of whether you guys like these videos or not so please leave your questions and comments below the last till next time
Info
Channel: Derek Banas
Views: 1,036,225
Rating: undefined out of 5
Keywords: Arduino Programming, Arduino, Arduino Tutorial
Id: QO_Jlz1qpDw
Channel Id: undefined
Length: 52min 56sec (3176 seconds)
Published: Wed Aug 15 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.