You are on page 1of 12

Mechatronics Lab

Lab Report
Spring 2020

BEMTS- F-17

Syed Sameed Ahmed


Name

Registration 170554
Number

Class BEMTS-6 B

Instructor’s ENGR. ALI HUSSAIN


Name
Table of Contents

Lab 9: Programming & Program Flow Concepts ......................................................................4


Objective: .........................................................................................................................4
Per Lab:................................................................................................................................4
Components: ........................................................................................................................5
Task01: Show control diagram and state diagram of your project. .. Error! Bookmark not
defined.
Task02: Show code of your project? .................................................................................6
Conclusions: ....................................................................................................................... 12

Air University, Islamabad P a g e |3


Lab 9: Programming & Program Flow Concepts

Objective:

➢ To get familiar with programming and program flow concept.

Per Lab:

In this lab, I learn about programming and different concept that we can you to make our
programming better. These concepts include,
• Control Diagram
• State Diagram
• States’ Value Tables
• Control Techniques
• Program Flow
Control Diagram: A control block diagram is a pictorial representation of the cause and effect
relationship between the input and output of a physical system. A control block diagram provides
a means to easily identify the functional relationships among the various components of a control
system. It defines the major inputs and output of our system.
State Diagram: A state machine diagram models the behavior of a single object, specifying the
sequence of events that an object goes through during its lifetime in response to events.
States’ Value Tables: It basically tell us about the state diagram, that how our machine works
and what are its justifications.
Control Techniques: Control technique can be single our multiple
In single technique, the advantages are,
• To access only one file
• All data available in one file, easy to search
And the disadvantages are,
• Mix-up
• Difficult to test
• Difficult to remove bugs
• Rough technique-Difficult to implement

Air University, Islamabad P a g e |4


But in multiple control we have,
• Low Level Control
• Intermediate Level Control
• High Level Control
• Main Code File
Program Flow: The sequence of execution of instructions in a program. This is determined at
run time by the input data and by the control structures (e.g. "if" statements) used in the program.

Components:

➢ Laptop
➢ MATLAB software with additional Hardware Packages

Air University, Islamabad P a g e |5


Task01: Show code of your project?
#include <HCSR04.h>

UltraSonicDistanceSensor RightSensor(13, 12);

UltraSonicDistanceSensor FrontSensor(11, 10);

UltraSonicDistanceSensor LeftSensor(9, 8);

UltraSonicDistanceSensor BackSensor(1, 0);

float RightDistance, FrontDistance, LeftDistance, BackDistance;

bool RightSide;

#define LeftIR 47

#define MidIR 45

#define RightIR 43

#define BlackLineDetected LOW

#define Battery1 A0

#define Battery2 A1

#define Buzzer 25

#define LowBattery1 40

#define LowBattery2 42

float vout = 0.0;

float B1 = 0.0;

float B2 = 0.0;

float R1 = 680000.0; // resistance of R1 (680K) -see text!

float R2 = 100000.0; // resistance of R2 (100K) - see text!

int value = 0;

#include <Servo.h>

int ServoGrip = 6;

Servo ServoGripper;

int ServoCont = 7;

Servo ServoContainer;

Air University, Islamabad P a g e |6


int angle = 0; // servo position in degrees

void LeftMotorForward()

digitalWrite(2,HIGH);

digitalWrite(3,LOW);

void LeftMotorBackward()

digitalWrite(3,HIGH);

digitalWrite(2,LOW);

void RightMotorForward()

digitalWrite(4,HIGH);

digitalWrite(5,LOW);

void RightMotorBackward()

digitalWrite(5,HIGH);

digitalWrite(4,LOW);

void Stop()

digitalWrite(2,LOW);

digitalWrite(3,LOW);

digitalWrite(4,LOW);

digitalWrite(5,LOW);

Air University, Islamabad P a g e |7


void GoForward()

Stop();

LeftMotorForward();

RightMotorForward();

void GoBackward()

Stop();

LeftMotorBackward();

RightMotorBackward();

void GoRight()

Stop();

LeftMotorForward();

RightMotorBackward();

void GoLeft()

Stop();

LeftMotorBackward();

RightMotorForward();

void MeasureDistances()

RightDistance = RightSensor.measureDistanceCm();

FrontDistance = FrontSensor.measureDistanceCm();

Air University, Islamabad P a g e |8


LeftDistance = LeftSensor.measureDistanceCm();

BackDistance = BackSensor.measureDistanceCm();

void BlackLine()

if (!(digitalRead(LeftIR)) || !(digitalRead(MidIR)) || !(digitalRead(RightIR)))

return true;

void CheckBatteryVolts()

value = analogRead(Battery1);

vout = (value * 5.0) / 1024.0; // see text

B1 = vout / (R2/(R1+R2));

if (B1<0.09) {

B1=0.0;//statement to quash undesired reading !

Serial.println("Battery 1 Voltage = " + B1);

value = analogRead(Battery2);

vout = (value * 5.0) / 1024.0; // see text

B2 = vout / (R2/(R1+R2));

if (B1<0.09) {

B2=0.0;//statement to quash undesired reading !

Serial.println("Battery 2 Voltage = " + B2);

void LowBatteryAlert()

if (B1 < 11)

digitalWrite(Battery1, HIGH);

Air University, Islamabad P a g e |9


if (B2 < 4.4)

digitalWrite(Battery2, HIGH);

void setup() {

// put your setup code here, to run once:

Serial.begin(115200); // We initialize serial connection so that we could print values from sensor.

pinMode(2, OUTPUT); digitalWrite(2, LOW);

pinMode(3, OUTPUT); digitalWrite(3, LOW);

pinMode(4, OUTPUT); digitalWrite(4, LOW);

pinMode(5, OUTPUT); digitalWrite(5, LOW);

pinMode(LeftIR, OUTPUT);

pinMode(MidIR, OUTPUT);

pinMode(RightIR, OUTPUT);

pinMode(Buzzer, OUTPUT);

pinMode(LowBattery1, OUTPUT);

pinMode(LowBattery2, OUTPUT);

pinMode(Battery1, OUTPUT); digitalWrite(Battery1, LOW);

pinMode(Battery2, OUTPUT); digitalWrite(Battery2, LOW);

ServoGripper.attach(ServoGrip);

ServoContainer.attach(ServoCont);

Serial.println("I'm Starting!!!");

MeasureDistances();

if(RightDistance > LeftDistance)

RightSide = false;

Serial.println("I'm on the right side of Arena.");

else

Air University, Islamabad P a g e | 10


{

RightSide = true;

Serial.println("I'm on the left side of Arena.");

void loop() {

// put your main code here, to run repeatedly:

CheckBatteryVolts();

LowBatteryAlert();

MeasureDistances();

while (RightSide)

GoForward();

MeasureDistances();

if (LeftDistance > 3.2)

GoLeft();

MeasureDistances();

if (LeftDistance < 2.8)

GoRight();

MeasureDistances();

if ((LeftDistance < 3.2) && (LeftDistance > 2.8))

GoForward();

MeasureDistances();

Air University, Islamabad P a g e | 11


}

if (BlackLine())

GoLeft();

MeasureDistances();

Conclusions:

So, in this lab, I learn about programming and how to make control diagram and state diagram.
I also understand how to do better coding. And what factors should be considered while doing
the coding.

Air University, Islamabad P a g e | 12


Pre Lab
/

Performance
/

Results /

Viva /

Critical Analysis
/

Individual / Team
Work

Fulfilment of Assigned
Tasks

Instructor Signature and Comments

Air University, Islamabad P a g e | 13

You might also like