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

Lab 3 - Looping

Uploaded by

Aljiven Alejo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 3 - Looping

Uploaded by

Aljiven Alejo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES

COMPUTER ENGINEERING DEPARTMENT


Quezon City

Activity Manual

Embedded Systems

Engr. Ryan D. Francisco, CCpE

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

Example of a 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

Example of a Nested Loop:

C language allows you to use one loop inside another loop. The following example illustrates the concept.

Infinite Loop

Using for loop:

Using while loop:

It is the loop having no terminating condition, so the loop becomes infinite.

4. Materials and Equipment:


Arduino Board
6 220-ohm resistors
6 LEDS
TinkerCAD
5. Procedures
For Loop Iteration
1. Connect six LEDs, with 220-ohm resistor in series, to digital pins 2-7 on your Arduino.
Wiring Diagram:
Schematic:

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.

3. Simulate the code in the TinkerCAD.


4. Observe the output
5. Measure the voltages of the pins when digital write is HIGH and also for the voltages of the pins when digital write is
LOW.

Pins Measured Voltages (V or mV)


HIGH LOW
2
3
4
5
6
7
6. Screenshot the simulation and recording of voltages, and explain the result in the Data Analysis.

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.

Screenshot of the output:

Screenshot of the codes:

8. Conclusion:

You might also like