filmov
tv
Arduino Tutorial: How To Write A LED Library

Показать описание
In this video i will teach you how to write a library for your arduino sketches.
The Arduio Code:
Script for Video:
"Welcome to the arduino programming series. In this video i will teah you how to write an arduino library.
First we need save our sketch so it creates a folder to store our header file and our cpp file. For this example i will save mine as LED_Library.
Then create another new tab. Name this file LED_Lib.h.
I write down what i will need in my library, and what functions I should add. This helps keep track of the code and progress.
I will add functions such as blinks, fade on, fade off, and pulse. It's required to include the Arduino header file so our header has access to standard arduino functions. In the header we need to create a class. A class contains the functions and variables that this class will access.
A semicolon is required after each class, and this is only required in the header file.
Functions and varialbes can either be public or private. Public variables and functions can be accessed by the sketch. Private functions and variables can only be accessed by the class.
We need to create our construct in public. Then create a variable, for that construct to use, in private.
The cpp file also requires us to include the arduino header file so that it can access the standard arduino functions. Then we will need to include our header file.
The format for setting up the functions is simple. The class 2 colons and then the function. These will be spelled the same as in the header file. The header file acts as a pointer so that our sketch can access the functions and variables within the library.
In this case our clase is LEDLIB and our functions is LEDLIB with the variable pin passed. So we need to type the class 2 colons the function open bracket close bracket.
This function will be the construct. This is the function that is executed when an instance of the class is created. We will need to save the pin to a variable. I used a the private variable tmpPIN and then set the pinMode to output.
Next we include the header file in the sketch. We create an instance by typing the class, a variable to to use as the instance,
and then the function for our construct. Pin 13 is not a pwm pin but
I'm going to use it for this example.
So far we've setup the header file, the cpp file, and the function that is our construct. Next we will create a function called blinks. This function will blink the led according the the variables we pass it. We need to setup the function in the header file. It will be passed 2 variables, count for how many times it blinks, and delayms for the total time it will take to complete the blinks.
In the cpp file we layout the function in the same format as we did the construct. The class the function and the variables passed. Open bracket close bracket.
Count is the number of times the led will blink. delayms is the total time of the blinks. We use this equation to set delayms to the amount of delay we need per step. It is good to use a for loop to control the blinks. For a blink we need to set the led high and then set the led low. The function blinks is now added to the library. To use this function in our sketch we simply type our instance LED1 dot BLINKS. In the parenthesis we set the amount of times we want it to blink and the duration of the BLINKS.
Now that the function blinks is finished we'll move on to the fade on function. To add a fade on function we'll set it up in the header file similiar to the blinks function. we'll only need to pass the amount of delay time since it is just fading on. This equation divides the delay by the amount of steps in the for loop. The for loop will fade from 0 to 255. To call this function in our sketch
we simply type LED1 dot FADEON and in the parenthisies we put the amount of time we want it to fade.
Now that we've added a fade on function we can add a fade off function as well. To do this we follow the same steps as we did with the fade on. The only difference in this one, is that in the for loop we start from 255 and go down to 0. Everything else is the same.
The last function to add to our library is pulse. In the pulse function we will use the fade on and fade off functions that we preveiously created. The delay is divided by 2 before it is passed to each function. Half the time fade on, half the time fade off.
Hopefully by now you can create your own libraries and add your own functions. If you have any questions leave down in the comments. Hope you guys learned something, have a nice day."
The Arduio Code:
Script for Video:
"Welcome to the arduino programming series. In this video i will teah you how to write an arduino library.
First we need save our sketch so it creates a folder to store our header file and our cpp file. For this example i will save mine as LED_Library.
Then create another new tab. Name this file LED_Lib.h.
I write down what i will need in my library, and what functions I should add. This helps keep track of the code and progress.
I will add functions such as blinks, fade on, fade off, and pulse. It's required to include the Arduino header file so our header has access to standard arduino functions. In the header we need to create a class. A class contains the functions and variables that this class will access.
A semicolon is required after each class, and this is only required in the header file.
Functions and varialbes can either be public or private. Public variables and functions can be accessed by the sketch. Private functions and variables can only be accessed by the class.
We need to create our construct in public. Then create a variable, for that construct to use, in private.
The cpp file also requires us to include the arduino header file so that it can access the standard arduino functions. Then we will need to include our header file.
The format for setting up the functions is simple. The class 2 colons and then the function. These will be spelled the same as in the header file. The header file acts as a pointer so that our sketch can access the functions and variables within the library.
In this case our clase is LEDLIB and our functions is LEDLIB with the variable pin passed. So we need to type the class 2 colons the function open bracket close bracket.
This function will be the construct. This is the function that is executed when an instance of the class is created. We will need to save the pin to a variable. I used a the private variable tmpPIN and then set the pinMode to output.
Next we include the header file in the sketch. We create an instance by typing the class, a variable to to use as the instance,
and then the function for our construct. Pin 13 is not a pwm pin but
I'm going to use it for this example.
So far we've setup the header file, the cpp file, and the function that is our construct. Next we will create a function called blinks. This function will blink the led according the the variables we pass it. We need to setup the function in the header file. It will be passed 2 variables, count for how many times it blinks, and delayms for the total time it will take to complete the blinks.
In the cpp file we layout the function in the same format as we did the construct. The class the function and the variables passed. Open bracket close bracket.
Count is the number of times the led will blink. delayms is the total time of the blinks. We use this equation to set delayms to the amount of delay we need per step. It is good to use a for loop to control the blinks. For a blink we need to set the led high and then set the led low. The function blinks is now added to the library. To use this function in our sketch we simply type our instance LED1 dot BLINKS. In the parenthesis we set the amount of times we want it to blink and the duration of the BLINKS.
Now that the function blinks is finished we'll move on to the fade on function. To add a fade on function we'll set it up in the header file similiar to the blinks function. we'll only need to pass the amount of delay time since it is just fading on. This equation divides the delay by the amount of steps in the for loop. The for loop will fade from 0 to 255. To call this function in our sketch
we simply type LED1 dot FADEON and in the parenthisies we put the amount of time we want it to fade.
Now that we've added a fade on function we can add a fade off function as well. To do this we follow the same steps as we did with the fade on. The only difference in this one, is that in the for loop we start from 255 and go down to 0. Everything else is the same.
The last function to add to our library is pulse. In the pulse function we will use the fade on and fade off functions that we preveiously created. The delay is divided by 2 before it is passed to each function. Half the time fade on, half the time fade off.
Hopefully by now you can create your own libraries and add your own functions. If you have any questions leave down in the comments. Hope you guys learned something, have a nice day."
Комментарии