Arduino Tutorials:Functions and Libraries in Arduino IDE

Published by sandesh on

Functions:

  • A function is a defined task in a code which we send input variables required for that task and acquire required solution.
  • Functions can be pre-defined (like pinMode , analogWrite , etc), or defined by the user.
  • We can call function any times required.

Example:

void setup(){
Serial.begin(9600);
}
void loop(){
int mul;
mul = multiply(3, 5);
Serial.println(mul);
}
int mul(int x, int y){
return x*y;
}

For the given example, 3*5 = 15 will be printed.This can be understood as follows:

Initially, process is started when we write Serial.begin(9600) in void setup, where initialization generally take place. Then, it comes to void loop, where actual code is written. Here, we wrote mul = multiply(3, 5) , which means we are sending 3 and 5 to mul function. That function stores value of 3 and 5 in x and y respectively and returns value 15 (return x*y; written in function returns the product of x and y). This is how functions are implemented.

Libraries:

Libraries constitutes of some pre-defined functionalities which are generally written in C or C++. They are introduced in Arduino 0004.

Steps to include libraries in the code:

  • Go to sketch menu, and choose Import Library in it, and pick the library you require from the libraries available. This creates an #include statement on top of the sketch for each header file in the library’s folder. This is how an inbuilt library is included.
  • To install your own library, you need to create a folder in ARDUINO/hardware/libraries. The folder should contain a C or C++ file with your code and a header file with your function and variable declarations.

Next Post:Intro to Serial Monitor

Previous Post:Using variables in arduino IDE


1 Comment

Arduino Tutorials:Using variables in arduino IDE - projectsflix · January 3, 2021 at 5:16 pm

[…] Next Post:Functions and Libraries in Arduino IDE […]

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.