Practical3b Programming
Practical3b Programming
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
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
}
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
}
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
}
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:
void loop() {
// leave empty for now
}
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:
Serial.print("Savings = ");
Serial.println(Savings);
Serial.print("AllMyMoney = ");
Serial.println(AllMyMoney);
}
void loop() {
// leave empty for now
}
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
// 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!");
}
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
}
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.
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.
void setup() {
Serial.begin(9600);
boolean Lamps[rooms][lightPerRoom] = { { true, true, true, true },
{ false, false, false, false },
{ true, false, true, false } };
if(Lamps[room][light]) {
Serial.println(" is ON");
}
else
{
Serial.println(" is OFF");
}
} // end of light loop
void loop() {
// leave empty for now
}
10