Course Content
Hands-On ESP32 Robotics: Build Smart Robots Step by Step

🧩 Functions in Arduino Programming

🎯 Lesson Objective

In this lesson, students will understand:

• What functions are in programming
• Why functions are useful in robotics programs
• How to create a function in Arduino programming
• How functions help organize large programs
• How functions improve code readability and reuse

This lesson introduces functions, which allow programs to be divided into smaller, manageable sections.


1️⃣ What is a Function?

A function is a block of code that performs a specific task.

Instead of writing the same code many times in a program, we can place the code inside a function and call it whenever needed.

Functions help simplify programs by grouping related instructions together.

For example, a robotics program may contain functions such as:

• moveForward()
• turnLeft()
• turnRight()
• stopRobot()

Each function performs a specific action of the robot.


2️⃣ Why Functions Are Important

Functions make programs easier to understand and manage.

Some advantages of using functions include:

✔ Reducing repeated code
✔ Improving program organization
✔ Making code easier to read
✔ Simplifying debugging and modification

In robotics projects, programs can become very long and complex.

Using functions helps divide the program into smaller logical parts.


3️⃣ Structure of a Function

A function usually contains three main parts:

• Function name
• Parameters (optional)
• Code block

Basic Syntax

void functionName() {
// code to execute
}

Explanation:

void indicates that the function does not return any value
functionName() is the name of the function
• The braces contain the code executed when the function is called


4️⃣ Creating a Simple Function

Example:

 
void turnOnLED() {
digitalWrite(ledPin, HIGH);
}
This function turns on an LED.

To run the function, we simply call it inside the program.

Example:

turnOnLED();
 

When this line executes, the LED turns on.


5️⃣ Functions with Parameters

Functions can also accept parameters, which allow data to be passed into the function.

Parameters make functions more flexible.

Example:

 
void setMotorSpeed(int speed) {
analogWrite(motorPin, speed);
}
 

Explanation:

• The function receives a value called speed
• This value controls the motor speed

Example usage:

 
setMotorSpeed(150);
 

The motor will run at speed value 150.


6️⃣ Functions in Robotics Programs

Functions are widely used in robotics programs to control robot behavior.

Examples include:

void moveForward() {
digitalWrite(motorA, HIGH);
digitalWrite(motorB, HIGH);
}

void stopRobot() {
digitalWrite(motorA, LOW);
digitalWrite(motorB, LOW);
}

Instead of writing motor control code repeatedly, we simply call the functions when needed.


7️⃣ Example in a Robot Program

Example program logic:

1️⃣ The robot moves forward.
2️⃣ If an obstacle is detected, the robot stops.
3️⃣ The robot turns and continues moving.

Using functions, the program may look like this:

moveForward();

if (distance < 20) {
stopRobot();
turnLeft();
}

This makes the program much easier to read and understand.


8️⃣ Why Functions Improve Program Design

As robotics programs grow larger, organizing code becomes very important.

Functions help create modular programs, where each function performs a specific task.

Benefits include:

✔ Cleaner code structure
✔ Easier debugging
✔ Faster development
✔ Reusable code

This is a common practice used by professional robotics engineers and software developers.


🚀 What Happens Next

Now that you understand how functions help organize and simplify programs, the next step is to learn how the microcontroller can communicate with the computer and display data.

Scroll to Top