Practical3a Programming
Practical3a Programming
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
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
Display Radian
END Calculate Radian = (Pi/180)*Degree
total = num1 + num2
Display Radian
END
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.
Fh = Mf x v
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)
3
PART 2:
SIMULATION
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
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:
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
return
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:
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.
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;
// If statement
if(Total>50) {
Serial.println("Congratulation...");
}
}
void loop() {
// leave empty for now
}
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;
// 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);
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.
For example, if the variable is defined as 4, the program should print the word
“Thursday” to the screen.
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
9
void setup() {
// set the speed for the serial monitor:
Serial.begin(9600);
//for statement
for(int A=10; A>=1; A--) {
Serial.print("New Year countdown: ");
Serial.println(A);
delay(1000);
}
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();
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;
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
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