0% found this document useful (0 votes)
45 views

Robotics 2.3

Functions allow programmers to divide code into sections that each perform a specific task. There are two main functions in Arduino called automatically - setup() which runs once, and loop() which runs repeatedly. Defining custom functions involves specifying the return type, function name, parameters, and using the return statement. Functions make code more readable, organized, compact, and reduce errors by avoiding repeated code.

Uploaded by

gadee
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Robotics 2.3

Functions allow programmers to divide code into sections that each perform a specific task. There are two main functions in Arduino called automatically - setup() which runs once, and loop() which runs repeatedly. Defining custom functions involves specifying the return type, function name, parameters, and using the return statement. Functions make code more readable, organized, compact, and reduce errors by avoiding repeated code.

Uploaded by

gadee
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Arduino Functions

Ultrasonic Sensor

At the end of the lesson 75% of the students are expected to:

1. identify what is a function.

2. enumerate the different advantages of functions.

3. construct and make a program in arduino IDE using


functions.
What is an ultrasonic sensor?
What is the formula for computing
the distance in ultrasonic sensor?
Functions
The functions allow a programmer to divide a
specific code into various sections, and each
section performs a particular task. The functions
are created to perform a task multiple times in a
program.
The function is a type of procedure that returns
the area of code from which it is called.
Functions

Advantages of using Functions

• It increases the readability of the code.


• It conceives and organizes the program.
• It reduces the chances of errors.
• It makes the program compact and small.
• It avoids the repetition of the set of statements or codes.
• It allows us to divide a complex code or program into a
simpler one.
• The modification becomes easier with the help of functions
in a program.
Functions

The Arduino has two functions which are called automatically in


the background. These are the setup() and loop().
The code to be executed is written inside the curly braces within
these functions.
void setup() - It includes the initial part of the code, which is
executed only once. It is called as the preparation block.
void loop() - It includes the statements, which are executed
repeatedly. It is called the execution block.
Function Declaration

Function return type


We need a return type for a function. For example, we can
store the return value of a function in a variable.
We can use any data type as a return type, such as float,
char, etc.
Function Declaration

Function name
It consists of a name specified to the function. It represents the
real body of the function.
Function parameter
It includes the parameters passed to the function. The
parameters are defined as the special variables, which are
used to pass data to a function.
The function must be followed by parentheses ( ) and
the semicolon ;
The actual data passed to the function is termed as an
argument.
Function Declaration
Function Declaration

void setup()  
{  
  Serial.begin(9600);  
}  
void loop() {  
  int a = 5; // initialization of values to the variables a and b  
  int b = 4;  
  int c;  
  c = myAddfunction(a, b); // c will now contains the value 9  
  Serial.println(c); // to print the resulted value  
  delay(1000); // time delay of 1 second or 1000 milliseconds  
}  
int myAddfunction(int i, int j)   
{  
  int sum;  
  sum = i + j;  
  return sum;  
}  
Function Declaration

void setup()  
{  
  Serial.begin(9600);  
}  
void loop() {  
  int a = 5; // initialization of values to the variables a and b  
  int b = 4;  
  int c;  
  c = myAddfunction(a, b); // c will now contains the value 9  
  Serial.println(c); // to print the resulted value  
  delay(1000); // time delay of 1 second or 1000 milliseconds  
}  
int myAddfunction(int i, int j)   
{  
  int sum;  
  sum = i + j;  
  return sum;  
}  

You might also like