Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

CS-101(L)

COMPUTER SYSTEMS &PROGRAMMING


(LAB)

PROGRAMMING PROJECT

TRAFFIC CONTROL SIGNAL SIMULATION

Submitted by:

Muhammad Haseeb Khan (1128-FET/BSME/F22)

Abdul Hannan (1136-FET/BSME/F22)

Muhammad Saboor Akhtar (1137-FET/BSME/F22)

Submitted to:
Engr. Farhan Khan
Traffic Control Signal Simulation

Abstract
The traffic control signal simulation when combined with real data makes one of
the best tools for research into optimization of traffic management. In this
project, a traffic control system simulated model is created by a program that
mimics red, yellow, and green lights of traffic signal. The program utilizes
various codes and graphic library for the simulation purpose can create a traffic
schedule for help in the movement of vehicles at on the road. The red, yellow,
and green lights come on one by one within 10 seconds of each other and the
timing may also be delayed by altering the code. This program provides the way
to control the movement of traffic automatically as the sequence repeats
automatically at the end of each cycle (i.e., Red, Yellow, Green) using increment
command. The number of times to allow sequence to repeat can also be altered
based on the requirements. The following data shown in Table 1, will be handled
by the computer program.

Data Priority Duration


Red 1 10 sec
Yellow 2 10 sec
Green 3 10 sec
Program 4 300 sec (10
continuation cycles)
Table 1: Traffic Signal Simulation

Introduction
Computers can use graphics.h to offer direct functions for drawing various
coordinates, such as circles, rectangles, etc. These shapes can be used to draw a
variety of items. This project focuses on creating a traffic light simulation in DEV
C++ by leveraging the graphics.h library and a variety of functions. A high-level
object-oriented programming language called C++ aids developers in creating
quick, adaptable program. Most other programming languages are slower than
C++. Because of this, it can be effective in settings where server speed is very
important, including web servers and database servers. In comparison to the
majority of other programming languages like Python, etc., C++ is also closer to

2
hardware. This makes it beneficial in situations where software and hardware
are closely integrated, and low-level software assistance is necessary.

The Dev C++ Code


#include<iostream>

#include<graphics.h>

#define BLUE 1

#define GREEN 2

#define CYAN 3

#defineRED 4

#define PURPLE 5

#define LIGHT_GRAY 7

#define GRAY 8

#define LIGHT_BLUE 9

#define LIGHT_GREEN 10

#define LIGHT_CYAN 11

#define LIGHT_RED 12

#define PINK 13

#define MAGENTA 13

#define LIGHT_YELLOW 14

using namespace std;

void Color(int textColor, int bgColor)//Void functions are created and used just like
value-returning functions except they do not return a value after the function executes.
void color for background color and text color

{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (textColor + (bgColor


* 16)));}// you can loop int higher to see more color choice

3
//GetStdHandle function returns a handle to the standard device (input, output, or
error). A handle is an index in the system table which gives access to the Windows
kernel object

int main()

{int start;

system("color E1");

Color(GREEN,LIGHT_YELLOW);

cout<<"INTERNATIONAL ISLAMIC UNIVERSITY ISLAMABAD"<<'\n';

Color(GREEN,LIGHT_YELLOW);

cout<<"FACULTY OF ENGINEERING AND TECHNOLOGY"<<'\n';

Color(LIGHT_BLUE,LIGHT_YELLOW);

cout<<"WELCOME TO TRAFFIC LIGHTS PROJECT"<<'\n';

Color(RED,LIGHT_YELLOW);

cout<<"A PROJECT BY:"<<'\n';

Color(BLUE,LIGHT_YELLOW);

cout<<"Muhammad Haseeb Khan (1128-FET/BSME/F22)"<<'\n';

Color(BLUE,LIGHT_YELLOW);

cout<<"Abdul Hannan (1136-FET/BSME/F22)"<<'\n';

Color(BLUE,LIGHT_YELLOW);

cout<<"Muhammad Saboor Akhtar (1137-FET/BSME/F22)"<<'\n';

Color(BLACK,LIGHT_YELLOW);

cout<<"ASSALAM-O-ALAIKUM!"<<'\n';

cout<<"We have Created This New Project, We Hope You Will Like This. . ."<<'\n';

Color(RED,LIGHT_YELLOW);

cout<<"About This Project:"<<'\n';

cout<<"THIS IS TRAFFIC LIGHT CONTROL SYSTEM"<<'\n';

cout<<"In This Project Lights Turn ON and OFF Automatically After Each Ten Seconds";

initwindow(800, 800); // define width and height of canvas

4
rectangle(250, 50, 350, 350);//define position of rectangle

circle(300, 100, 50);// create circle (x, y, r)

circle(300, 200, 50);

circle(300, 300, 50);

//creating animation

for(int i =0; i < 10; i++)//runs the traffic light sequence 10 times

setfillstyle(1, RED);//Fills red color in circle one

floodfill(300, 100, WHITE);//Stops red color going outside circle white border

outtextxy(280, 100, "STOP");//writes text "STOP" over red circle

delay(10000);//red signal remains for 10 seconds

setfillstyle(1, BLACK);

floodfill(300, 100, WHITE);

setfillstyle(1, YELLOW);

floodfill(300, 200, WHITE);

outtextxy(280, 200, "HOLD");

delay(10000);

setfillstyle(1, BLACK);//Helps in blinking of last yellow signal light

floodfill(300, 200, WHITE);//Helps in blinking of last yellow signal light

setfillstyle(1, GREEN);

floodfill(300, 300, WHITE);

outtextxy(280, 300, "GO");

delay(10000);

setfillstyle(1, BLACK);//Helps in blinking of last green signal light

floodfill(300, 300, WHITE);//Helps in blinking of last green signal light

}}

5
The Python Code

import time #in Python provides functions for handling time-related


tasks.The time-related tasks includes,reading the current
time,formatting time,sleeping for a specified number of seconds and
so on.

from tkinter import * #Represents all the functions and built-in


modules in the tkinter library. By importing all the functions and
methods, we can use the inbuilt functions or methods in a particular
application without importing them implicitly.

root = Tk() #It helps to display the root window and manages all the
other components of the tkinter application.

root.title("Traffic Signal") #Use to set title to window. root.


config – Used to configure attributes to the window, such as width,
height, background color.

canvas = Canvas(root, width=200, height=300) #The Canvas widget


supplies graphics facilities for Tkinter. Among these graphical
objects are lines, circles, images, and even other widgets.

canvas.pack() #organizes widgets in horizontal and vertical boxes


that are limited to left, right, top, bottom positions offset and
relative to each other within a frame.

red_light = canvas.create_oval(25, 25, 75, 75, fill="red")

yellow_light = canvas.create_oval(25, 100, 75, 150, fill="black")

green_light = canvas.create_oval(25, 175, 75, 225, fill="black")

def traffic_signal(): #The traffic library helps to work with common


sources of air traffic data. Its main purpose is to provide data
analysis methods commonly applied to trajectories and airspaces.

6
while True: #They are used to repeat a sequence of statements an
unknown number of times.

canvas.itemconfig(red_light, fill="red") #If you need to


configure the Canvas item dynamically, then tkinter provides
itemconfig(**options) method. You can use this method to configure
the properties and attributes of the Canvas items. For example, if we
create a line inside the Canvas widget, we can configure its color or
width using itemconfig() method.

canvas.itemconfig(yellow_light, fill="black")

canvas.itemconfig(green_light, fill="black")

root.update() #Update method processes all the pending idle


tasks, unvisited events, calling functions, and callbacks. The method
is applicable for updating and processing all the events or tasks
such as redrawing widgets, geometry management, configuring the
widget property, etc.

time.sleep(10) #Python time sleep function is used to add


delay in the execution of a program.

canvas.itemconfig(red_light, fill="black")

canvas.itemconfig(yellow_light, fill="yellow")

canvas.itemconfig(green_light, fill="black")

root.update()

time.sleep(10)

canvas.itemconfig(red_light, fill="black")

canvas.itemconfig(yellow_light, fill="black")

canvas.itemconfig(green_light, fill="green")

root.update()

time.sleep(10)

traffic_signal() #The traffic library helps to work with common


sources of air traffic data. Its main purpose is to provide data
analysis methods commonly applied to trajectories and airspaces.

root.mainloop() #A method on the main window which we execute when we


want to run our application. This method will loop forever, waiting

7
for events from the user, until the user exits the program – either
by closing

Pseudo Code For Dev C++


1. In DEV C++, first, initialize graphic mode using function initwindow(x, y); as
this also defines width and height of canvas.
2. Create a rectangle using the rectangle() function.
3. Draw 3 circles and color/style them accordingly by using functions like,
floodfill(), and setfillstyle(), outtextxy().
4. Repeat the process until the traffic light is not complete.
5. Use delay() function in between to hold the screen where we want.

Pseudo Code For Python


1. In Python,first using time import function for handling time related
tasks .Using tkinter library that will help us to import functions that we
will use to built a programme.

2. For managing windows and components of tkinter we will use root+Tk


and root,title issued for managing width , height and background colors.

3. Using canva for drawing graphical objects like oval,rectangle and defining
their positions.

4. While statement is used to repeat the statement a no of times for showing


red,yellow and green signal color.

5. Time.sleep will delay the process of execution of signal color. Root,main


loop is used to run he programme until the user exit it.

Results and Discussions


After writing the program it was executed and first traffic signal to appear was
red, the second signal that activated was yellow and the third was green and
timing between each signal was 10 seconds. Thus, the project purpose was

8
successful. The program seems to be suitable for developing graphics as well as
code for actual traffic light signal. Further research in this area may lead to the
development of Traffic light signal that can help authority manage road traffic
much efficiently. Hence there is room for more research in this area.

C++ Output Result:

9
10
Python Output Result:

11
Conclusion
To experience the utility of the C++ and Python language, a traffic control signal
application was created in this project and run in DEVC++. One of the most
significant programming languages is C++ and Python since it can be used to
create similar but more sophisticated programs like Windows, photo editing
software, video games, and web browsers. And almost all the programs/systems
that we use have some or the other part of the codebase that is written in C/C++
or Python. Hence it is concluded that Python and C++ plays an integral role in
almost all applications that we use.

The End

12

You might also like