Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
110 views

Practical3a Programming

1. Draw the circuit diagram in Proteus using an Arduino UNO board connected to 5 LEDs through pins 2, 3, 4, 5, and 6. 2. Write the pseudocode and flowchart to turn on each LED for 2 seconds sequentially in a repeating loop. 3. Code the Arduino sketch to set the LED pins as outputs and use digitalWrite() and delay() functions in a repeating loop to turn on each LED for 2 seconds. 4. Compile and verify the code in Arduino IDE and import the hex file into Proteus. Simulate the

Uploaded by

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

Practical3a Programming

1. Draw the circuit diagram in Proteus using an Arduino UNO board connected to 5 LEDs through pins 2, 3, 4, 5, and 6. 2. Write the pseudocode and flowchart to turn on each LED for 2 seconds sequentially in a repeating loop. 3. Code the Arduino sketch to set the LED pins as outputs and use digitalWrite() and delay() functions in a repeating loop to turn on each LED for 2 seconds. 4. Compile and verify the code in Arduino IDE and import the hex file into Proteus. Simulate the

Uploaded by

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

FSB20203

INTRODUCTION TO INDUSTRIAL INFORMATION


TECHNOLOGY

Practical 3
Computer Programming (a)

Objectives
1. To introduce the students with problem solving technique.
2. To familiarise the students with simulation software.
3. To introduce the students with the basic programming structure of flow control.

PART 1:

PROBLEM SOLVING

3.1 Software development procedure


Software development procedure is the life cycle of processes that is comprised of a
well-segregated structure imposed for the development of software. Every step in the
development life cycle gives rise to the end-product needed by the next stage in the cycle.
There are three (3) basic phases that define the process of software development, which are:
i. Development and design
ii. Documentation
iii. Maintenance

In the first phases of software development procedure (development and design), there
are four (4) steps involved.
i. Step 1: Analyse the problem
ii. Step 2: Develop the solution using algorithm
iii. Step 3: Code the solution using programming
iv. Step 4: Testing

1
Problems:
Hands
on Assume that the angle of a given slope is 75°. Determine its angle in radians.
Use the formula given:
1
𝜋
𝑅𝑎𝑑𝑖𝑎𝑛 = 𝑥𝐷𝑒𝑔𝑟𝑒𝑒
180
Solution
Step 1: Analysis
Input: Degree=75 and Pi=3.14
Output: Radian
Formula: Radian = (Pi / 180) * Degree

Step 2: Develop the solution using algorithm


Pseudocode Flow chart
START
START
Read Degree = 75
Read Pi = 3.14
Calculate Radian=(Pi/180) * Degree Read Degree =75, pi=3.14

Display Radian
END Calculate Radian = (Pi/180)*Degree
total = num1 + num2

Display Radian

END

Step 3: Code the solution using programming.


Step 4: Testing. Compile and run the program to check whether the code is producing the
correct output or not.

2
By using the steps in Software Development Procedure, solve a problem to find
Exercise
the circumference of a circle with a radius of 3.3 inches. The equation for the
1
circumference, c of a circle is c = 2πr, where r is the radius and π is equal
to 3.14.

By using the steps in Software Development Procedure, solve a problem to


Exercise
calculate the horizontal force exerted on the T-connecter pipe shown below,
2
with the mass flow rate is 1.5 kg/s, and the fluid’s velocity entering the T-
connecter is 3 m/s. The formula for determining this force is as follows:

Fh = Mf x v

Fh is the horizontal force (N)


Mf is the mass flow rate (kg/s)
v is the velocity (m/s) of the
fluid entering the pipe

Wind turbine power is becoming an important electric generation source. The


Exercise power generated from a wind turbine is given by the formula:
3
𝐶𝑝 × 𝐷 × 𝑆 × 𝑉
𝑃= 2

P: power in (watts)
Cp: performance coefficient (dimensionless=0.25 for most systems)
D: air density(kg/m3)
S: blade sweep area(m2)
V: wind velocity(m/s)

By using the steps in Software Development Procedure, solve a problem to


determines the watts generated by a wind turbine having blade sweep area of
21m2, with wind velocity of 4.47m/s and the air density is 1.23kg/m 3.

3
PART 2:

SIMULATION

3.2 Simulation software


Proteus is a simulation and electronic design development tool developed by Lab Centre
Electronics. It is very useful tool as it ensures that the circuit design or firmware code is working
properly before it can be embedded into the microcontroller. The software is used mainly by
electronic design engineers and technicians to create schematics and electronic prints for
manufacturing printed circuit boards. Figure 1 shows the screenshot of schematic drawing on
Proteus.

Figure 1: Example of schematic drawing on Proteus.

Download the Proteus software from VLE portal. The instruction is also given in
Hands
on the separate file (Proteus_Installation.pdf). Test your simulation software by
sketching the schematic drawing as follows, to turn on the led.
2

4
Proteus does not come with a built-in Arduino library. It needs to be installed
Hands
on separately. Download the Arduino library from VLE portal. Then, follow the
instruction for installation and testing. The instruction is given in the separate file
3
(Arduino_On_Proteus.pdf).

5
PART 3:

ARDUINO

3.3 Arduino IDE


An Arduino Integrated Development Environment (IDE) is used to develop programs for
Arduino projects. It can typically be used to edit programs with added features of automatic
syntax highlighting and run the programs within the IDE. The open-source Arduino IDE is used
to write an instruction (code) and upload it to the board. To start controlling the world, you
need to setup the software to program the board. Visit the Arduino website
(https://www.arduino.cc/en/software#download) and download the latest version of Arduino
IDE. Once the installation is complete, you can start to create a new project on Arduino IDE
as shown in Figure 2.

Figure 2: Arduino IDE.

6
Based on the given figure below, simulates the Arduino project using Proteus
Hands
on simulation software. Use Arduino UNO as the microcontroller, and pin number 5
to be connected to LED. Make the LED blinks for every 2 seconds.
4

SOLUTION:
Firstly, draw the diagram in Proteus, as shown below:

Secondly, solve the problems using problem solving technique.


Step 1: Analysis
Input: -
Output: Pin5
Formula: -

7
Step 2: Develop the solution using algorithm
Pseudocode Flow chart
START
Set digital pin 5 name as PINNo START setup() loop()

START SETUP
Set PINNo as output PINNo = 5 PINNo as output PINNo on
END SETUP

START LOOP Wait 2 seconds


On PINNo setup() return
Delay 2 seconds
Off PINNo PINNo off
Delay 2 seconds
END LOOP loop()
END Wait 2 seconds

return

Step 3: Code the solution using programming in Arduino IDE as shown


below:
int PINNo=5;

void setup() {
// put your setup code here, to run once:
pinMode(PINNo,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(PINNo,HIGH);
delay(2000); //delay for 2 seconds
digitalWrite(PINNo,LOW);
delay(2000); //delay for 2 seconds
}

Step 4: Testing. In Arduino IDE, select File --> Preferences, tick the
compilation box, and press OK. Then, Compile (Verify) the code and copy
the hex file path.

8
Next, in Proteus, double click on the Arduino board to insert the hex file and press
OK.

Finally, after inserting the hex file, the simulation can be started by pressing the
start button.

9
Simulates the Arduino project using Proteus based on the following
Exercise
requirements:
4
i. Arduino UNO as the microcontroller
ii. Using 5 LED’s
iii. Pseudocode:
REPEAT
on LED 1.
delay 2 seconds.
off LED 1.
on LED 2.
delay 2 seconds.
off LED 2.
on LED 3.
delay 2 seconds.
off LED 3.
on LED 4.
delay 2 seconds.
off LED 4
on LED 5.
delay 2 seconds.
off LED 5.
END LOOP

10
PART 4:

Statements and Control Flow

Making decision is useful if only a particular code need to be executed when a certain situation
occurs. It is known as “control flow” or controlling how the program executes only a piece of
program that is required. Conditional statements check whether a programmer-specified
Boolean condition is true or false.

3.4 Selection Statement


Selection statement in Arduino:
i. If
ii. If-else
iii. Switch case

The figure below simulates the Arduino project using Proteus simulation
Hands
on software. Arduino UNO is used as the microcontroller, and virtual terminal is used
to display the output.
5
- Connect the RXD of the virtual terminal to TXD of the Arduino.
- Connect the TXD of the virtual terminal to RXD of the Arduino.

11
Write a program in Arduino IDE as shown below, simulate the output on Proteus
and discuss the result. Try to change the values of Test and Final.
void setup() {
// set the speed for the serial monitor:
Serial.begin(9600);

// variables declaration
int Test;
int Final;
int Total;

// assign the values


Test = 20;
Final = 18;
Total = Test + Final;

// print the values to the serial monitor


Serial.print("Written Test = ");
Serial.println(Test);
Serial.print("Final Exam = ");
Serial.println(Final);
Serial.print("Total marks = ");
Serial.println(Total);

// If statement
if(Total>50) {
Serial.println("Congratulation...");
}
}

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

The simulation that you will get:

12
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. Try to
change the values of Test and Final.
6
void setup() {
// set the speed for the serial monitor:
Serial.begin(9600);

// variables declaration
int Test;
int Final;
int Total;

// assign the values


Test = 20;
Final = 18;
Total = Test + Final;

// print the values to the serial monitor


Serial.print("Written Test = ");
Serial.println(Test);
Serial.print("Final Exam = ");
Serial.println(Final);
Serial.print("Total marks = ");
Serial.println(Total);

// If-else statement
if(Total>50) {
Serial.println("Congratulation...");
}
else {
Serial.println("Bad Luck...");
}
}

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

13
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. Try to
change the values of A.
7
void setup() {
// set the speed for the serial monitor:
Serial.begin(9600);

// define the variable


int A = 5;

// switch case statement


switch(A) {
case 1: Serial.println("A = 1");
break;
case 2: Serial.println("A = 2");
break;
case 3: Serial.println("A = 3");
break;
case 4: Serial.println("A = 4");
break;
case 5: Serial.println("A = 5");
break;
case 6: Serial.println("A = 6");
break;
case 7: Serial.println("A = 7");
break;
case 8: Serial.println("A = 8");
break;
case 9: Serial.println("A = 9");
break;
case 10: Serial.println("A = 10");
break;
default: Serial.println("A less than 1 or greater than 10");
}
}

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

14
Using the same schematic on Hands on 5, add Logic Toggle to the schematic
Hands
on drawing as follows. Logic Toggle act as a Boolean device that used to gives an
input to the circuit. It consists of only two conditions, LOW and HIGH.
8

Write a program in Arduino IDE as shown below, simulate the output on Proteus
and discuss the result. Try to press the red dot on Logic Toggle.
void setup() {
// set the speed for the serial monitor:
Serial.begin(9600);
pinMode(9,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
int x = digitalRead(9);

if (x==1) {
Serial.println("Current value is HIGH");
}
else {
Serial.println("Current value is LOW");
}
}

15
Using if statement, write a program on Arduino IDE that will display a
Exercise
message “WELCOME HOME!” when the logic probes value is 1 and a message
5
“GOODBYE!” when logic probes value is 0 on virtual terminal.

Using switch statement, write a program on Arduino IDE that define an


Exercise
integer number between 1 – 7 and display the corresponding day of the week
6
(1 is Monday, 2 is Tuesday, …) on virtual terminal.

For example, if the variable is defined as 4, the program should print the word
“Thursday” to the screen.

The grade level of undergraduate college students is typically determined


Exercise
according to the following schedule:
7
Number of Credits Completed Grade Level
Less than 32 Freshman
32 to 63 Sophomore
64 to 95 Junior
96 or more Senior

Using if-else statement, write a program on Arduino IDE that define an


integer number of credits a student has completed, determines the student’s
grade level, and displays the grade level on virtual terminal.

A student’s letter grade is calculated according to the following schedule:


Exercise
Numerical Grade Letter Grade
8
Greater than or equal to 90 A
Less than 90 but greater than or equal to 80 B
Less than 80 but greater than or equal to 70 C
Less than 70 but greater than or equal to 60 D
Less than 60 F

Using this information, write a program on Arduino IDE that define a float
number of a student’s numerical grade and displays the letter grade on virtual
terminal.

16
3.5 Looping Statement
Looping statement in Arduino:
i. for
ii. while
iii. do while

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.

9
void setup() {
// set the speed for the serial monitor:
Serial.begin(9600);

Serial.println("Coundown for New Year:");


Serial.println(); //Add new line

//for statement
for(int A=10; A>=1; A--) {
Serial.print("New Year countdown: ");
Serial.println(A);
delay(1000);
}

Serial.println(); //Add new line


Serial.println(); //Add new line
Serial.println("HAPPY NEW YEAR...");
}

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

17
Using the same schematic on Hands on 8, write a program in Arduino IDE as
Hands
on shown below, simulate the output on Proteus and discuss the result. Press the
red dot on Logic Toggle to see the result.
10

void setup() {
// set the speed for the serial monitor:
Serial.begin(9600);
pinMode(9,INPUT);
int i=1, x;

Serial.println("I will count from 1 to 10.");


Serial.println("Press the logic toggle to stop counting");
Serial.println();

//while loop statement


while(i<=10) {
Serial.println(i);
delay(1000);
x = digitalRead(9);
if (x==1) {
Serial.println("STOP");
break; //Stop the loop
}
i++;
}

Serial.println();
Serial.println("You stop at ");
Serial.print(i);
}

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

18
Using the same schematic on Hands on 8, write a program in Arduino IDE as
Hands
on shown below, simulate the output on Proteus and discuss the result. Press the
red dot on Logic Toggle to see the result.
11

void setup() {
// set the speed for the serial monitor:
Serial.begin(9600);
pinMode(9,INPUT);
int i=1, x;

Serial.println("I will count from 1 to 10.");


Serial.println("Press the logic toggle to stop counting");
Serial.println();

//do while loop statement


do {
Serial.println(i);
delay(1000);

x = digitalRead(9);
if (x==1) {
Serial.println("STOP");
break; //Stop the loop
}
i++;
}while(i<=10);

Serial.println();
Serial.println("You stop at ");
Serial.print(i);
}

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

19
Using for loop statement, write a program on Arduino IDE that will display
Exercise
9 even numbers from 1 to 50 on Proteus virtual terminal.

Using while loop statement, write a program on Arduino IDE that will calculate
Exercise
and display the square and cube of a given range of number (1 to 10) as shown
10
below on Proteus virtual terminal.
Number Square Cube
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000

Change Exercise 10 program to work with a do-while loop.


Exercise
11

Using the formula below, starting with a Celsius temperature of zero degrees,
Exercise
write a program on Arduino IDE that determines the temperature value in
12
Fahrenheit. The temperature in Celsius will be incremented by 5°C and the
maximum value of temperature conversion is 50°C
Fahrenheit = (9 / 5) * Celsius + 32
Degree in Celsius Degree in Fahrenheit
0 32
5 41
10 50
15 59
20 68
25 77
30 86
35 95
40 104
45 113
50 122

20

You might also like