0% found this document useful (0 votes)
44 views10 pages

Practical3b Programming

The document introduces built-in and user-defined functions in Arduino programming. It discusses common built-in functions like abs(), sqrt(), and delay(), as well as how to define and call user-defined functions. It also covers passing values to functions as arguments and returning values from functions. Finally, it introduces one-dimensional arrays in Arduino, which can store multiple values of the same type.

Uploaded by

Muhd Shazany
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
44 views10 pages

Practical3b Programming

The document introduces built-in and user-defined functions in Arduino programming. It discusses common built-in functions like abs(), sqrt(), and delay(), as well as how to define and call user-defined functions. It also covers passing values to functions as arguments and returning values from functions. Finally, it introduces one-dimensional arrays in Arduino, which can store multiple values of the same type.

Uploaded by

Muhd Shazany
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

FSB20203

INTRODUCTION TO INDUSTRIAL INFORMATION


TECHNOLOGY

Practical 3
Computer Programming (b)

Objectives
1. To introduce students with built-in functions and user-defined functions.
2. To introduce students with the usage and the method to access the variable array

PART 5:

Functions

3.6 Built-in functions / pre-defined functions


A function is a subprogram that is included in a program to perform a particular task
such as obtaining data, carrying out some calculations, displaying some information or
messages, and displaying the output data. In Arduino, there are many predefined functions
that are written to simplify the computations. Following is an example in which a predefined
function can significantly simplify the computations

i) abs - computes absolute value. It takes one parameter of type integer, a, and its
value returned is of type integer.
p = abs(a);

ii) sqrt - computes square root. It takes one parameter of type double, a, and its value
returned is of type double.
p = sqrt(a);

iii) delay - Pauses the program for the amount of time (in milliseconds) specified as
parameter, where, ms is the time in milliseconds to pause (there are 1000
milliseconds in a second).
delay(ms);

iv) pinMode - Configures the specified pin to behave either as an input or an output.
pinMode(pin, mode);

1
Discuss the purpose of Arduino built-in function in the program below:
Hands
on
int ledPin = 13; // LED connected to digital pin 13
12
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}

void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}

Discuss the purpose of Arduino built-in function in the program below:


Hands
on
double angle = 0;
13 double increment = 1;

void setup() {
Serial.begin(9600);
}

void loop() {
double sineValue = sin(angle);
Serial.println(sineValue);
delay(100);
angle += increment;
}

2
3.7 User-defined functions / programmer-defined functions
We can write our own functions to perform some operation or computations. These
types of functions are referred to as user-defined functions. A user-defined function may
have two parts:
i) Function definition
ii) Function Call

There are two required user-defined functions in an Arduino code, setup() and
Hands
on loop(). The following Arduino code shows the use of the third user-defined
function. Simulate the following Arduino code output on Proteus and discuss the
14
result.
void setup() {
Serial.begin(9600);
DashedLine(); // function call
Serial.println("| Program Menu |");
DashedLine(); // function call
}

void loop() {
// leave empty for now
}

void DashedLine() // function definition


{
Serial.println("----------------");
// no return values
}

Simulate the following Arduino code output on Proteus and discuss the result.
Hands
on void setup () {
Serial.begin(9600);
15 }

void loop () {
int result = 0;
result = sum_func (5,6); // function call
}

int sum_func (int x, int y) // function definition


{
int z = 0;
z=x+y;
return z; // return the value
}

3
Passing a value to a function
Pass-by-Value is the default argument passing technique for all data types in a program. It is
the only argument passing technique, that is most familiar to many programmers. Arguments
can be variables or immediate values, and multiple arguments are separated by commas. The
number and type of values that a function requires must be specified when the function is
written. Example of passing a value to a function:

Using the same schematic on Hands on 5, write a program in Arduino IDE as


Hands
on shown below, simulate the output on Proteus and discuss the result.
void setup() {
16 // set the speed for the serial monitor:
Serial.begin(9600);

for (int A=1; A<=5; A++) {


DoLights(A); // passing a value A to a DoLights function
}
}

void loop() {
// leave empty for now
}

void DoLights (int LightNumber) {


Serial.print("Switch lights on for light ");
Serial.println(LightNumber);
}

4
Returning a value from a function
User-defined functions can be designed to return a value to the caller. To return a value from a
function, you must include a return statement, followed by the value to be returned before the
function’s end statement. If you do not include a return statement or if you do not specify a value
after the keyword return, the value returned by the function is unpredictable. Example of returning
a value from a function:

Using the same schematic on Hands on 5, write a program in Arduino IDE as


Hands
on shown below, simulate the output on Proteus and discuss the result.
void setup() {
17 // set the speed for the serial monitor:
Serial.begin(9600);

// define our variables


int PocketMoney = 4;
int Savings = 12;
int AllMyMoney = CalculateMyMoney(PocketMoney, Savings); //calling function

// print the values to the serial monitor


Serial.print("PocketMoney = ");
Serial.println(PocketMoney);

Serial.print("Savings = ");
Serial.println(Savings);

Serial.print("AllMyMoney = ");
Serial.println(AllMyMoney);
}

void loop() {
// leave empty for now
}

int CalculateMyMoney(int Wallet, int Bank) {


int Total;
Total = Wallet + Bank;
return Total; // returning a total value to calling function
}

5
Write an Arduino program that will find the largest number among four
Exercise
numbers. The numbers will be passed as arguments from the setup()
13
function to a user-defined function called largest( ). The largest number will be
returned to setup() function to be displayed on virtual terminal.

Write an Arduino program that will display the power of two numbers from 1 to
Exercise
10 on virtual terminal using user-defined function. The program will first read
14
numbers from the setup() function and pass the numbers to a user-defined
function called powNum( ). This function will perform the calculation and then
return the power of two numbers to the setup() function to be displayed.

Create a sketch that has four (4) different blink sequences using four (4)
Exercise
LEDs. Each sequence should be encapsulated in its own function.
15
i) Write a different function for each blink sequence
ii) The sequences should look different from each other, varying speed,
number of blinks, etc.
iii) Call each sequence in the loop() function with a delay of 1000ms
between them.

6
PART 6:

ARRAY

3.8 One-dimensional array


So far, all our variables have been able to hold only one value at any one point in time.
Such variables are called scalar variables. Now it is time for our first non-scalar variable, an
array.

An array is a variable capable of storing multiple values. When we declare an array,


we tell the compiler how many values we want the array to hold. We also tell the compiler what
type of values the array can store. All the values in an array must be of the same type.

Using the same schematic on Hands on 5, write a program in Arduino IDE as


Hands
on shown below, simulate the output on Proteus and discuss the result.
void setup() {
18 // set the speed for the serial monitor:
Serial.begin(9600);
bool LightOn[5] = { false, false, false, false, false };

// switch lights
LightOn[2] = true; // switch the 3rd light on
LightOn[4] = false; // switch the 5th light off

// check if light 2 is on
if(LightOn[1]==true) {
Serial.println("First light is ON!");
}

// check if light 1 is on
if(LightOn[0]) {
Serial.println("First light is ON!");
}

// if light 4 is not on, then switch light 3 on


if(!LightOn[3]) {
Serial.println("4th light was not on, switching light 3 on now!");
}
}

void loop() {
// leave empty for now
}

7
Design a circuit on Proteus as follows:
Hands
on

19

Write a program in Arduino IDE as shown below, simulate the output on Proteus
and discuss the result.
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9};

void setup () {
for (int i = 0; i < 8; i++) { //this is a loop and will repeat eight times
pinMode(ledPins[i], OUTPUT); // set each LED pin to output
}
}

void loop () {
TurnON(); //calling function
TurnOFF(); //calling function
}

void TurnON () //Turns Each LED On


{
for (int i=0; i<8; i++) {
digitalWrite(ledPins[i], HIGH);
delay(100);
}
}

void TurnOFF () //Turns Each LED Off


{
for (int i=7; i>=0; i--) {
digitalWrite(ledPins[i], LOW);
delay(100);
}
}

8
Assume there is an array named myArray[5] with value of 10, 120, -3, 8, 11.
Exercise
Write an Arduino code that able to calculate the summation and average of the
16
array content. Then display the result on Proteus virtual terminal.

Rewrite Exercise 13 using an array.


Exercise
17

Further the program in Exercise 17 by displaying the smallest number, total


Exercise
summation, and the average of the array.
18

9
3.9 Two-dimensional array
Arrays with two dimensions often represent tables of values consisting of information
arranged in rows and columns. Following are the key features of multidimensional arrays:
• To identify a particular table element, we must specify two subscripts.
• By convention, the first identifies the element’s row and the second identifies the
element’s column.
• Arrays that require two subscripts to identify a particular element are called two-
dimensional arrays or 2-D arrays.
• Arrays with two or more dimensions are known as multidimensional arrays and can
have more than two dimensions.

Using the same schematic on Hands on 5, write a program in Arduino IDE as


Hands
on shown below, simulate the output on Proteus and discuss the result.
#define rooms 3 //constant value
20 #define lightPerRoom 4 //constant value

void setup() {
Serial.begin(9600);
boolean Lamps[rooms][lightPerRoom] = { { true, true, true, true },
{ false, false, false, false },
{ true, false, true, false } };

for(int room=0; room < rooms; room++) { // Count ROOMS


for(int light=0; light < lightPerRoom; light++) { // Count LIGHTS per ROOM
Serial.print("Room ");
Serial.print(room);
Serial.print(" Light ");
Serial.print(light);

if(Lamps[room][light]) {
Serial.println(" is ON");
}
else
{
Serial.println(" is OFF");
}
} // end of light loop

Serial.println(); // print empty line after each room list


} // end room loop
}

void loop() {
// leave empty for now
}

10

You might also like