Lab 3 - Looping
Lab 3 - Looping
Activity Manual
Embedded Systems
Activity No. 3
Looping
Course Code: CPE 014 Date Performed:
Course Title: Embedded Systems Date Submitted:
Section: Instructor:
1. Objective(s):
This activity aims to introduce the microcontroller as an integral part of implementing Embedded Systems by demonstrating
how it is used to control electronic components.
2. Intended Learning Outcomes (ILOs):
After performing the laboratory activity, the students should be able to
2.1 Apply looping structure in displaying blinking lights in the series of LEDs.
3. Discussions:
In this lesson, we will be going through a new structure of programming with Arduino, called Loops. A loop is used to repeat
a set of code constantly, whilst a condition is true. When the condition becomes false, the Arduino exits the loop and
proceeds with the rest of the code. There are 2 different types of loops we are going to be looking at, both are very similar to
each other.
A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of
a loop statement in most of the programming languages –
While Loop
A while loop will loop continuously, until the condition inside the parenthesis becomes false. Something must change the
tested variable, or the while loop will never exit.
Do-While Loop
The do…while loop is similar to the while loop. In the while loop, the loop-continuation condition is tested at the beginning of
the loop before performed the body of the loop. The do…while statement tests the loop-continuation condition after
performed the loop body. Therefore, the loop body will be executed at least once.
When a do…while terminates, execution continues with the statement after the while clause. It is not necessary to use
braces in the do…while statement if there is only one statement in the body. However, the braces are usually included to
avoid confusion between the while and do…while statements.
For Loop
The for statement is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to
increment and terminate the loop.
Nested Loop
C language allows you to use one loop inside another loop. The following example illustrates the concept.
Infinite Loop
2. Open the Arduino IDE and copy the codes given below:
The code below begins by utilizing a for() loop to assign digital pins 2-7 as outputs for the 6 LEDs used.
In the main loop of the code, two for() loops are used to loop incrementally, stepping through the LEDs, one by one, from pin
2 to pin seven. Once pin 7 is lit, the process reverses, stepping back down through each LED.
6. Data Analysis:
7. Supplementary Activity:
Tasks:
A:
1. Using a loop function, blink an LED with a constraint of blinking by 10 times with 1000ms interval.
2. It must automatically turn off after 10 times.
B:
1. Using the same code and diagram, use an alternative structure to blink a second LED with a constraint of blinking by 25
times with 500ms intervals.
2. It must automatically turn off after 25 times.
8. Conclusion: