Traffic light system project in c++

Recently a major task I had to complete was to implement a traffic light system project in C++ with 3 lights which included red, yellow and green. Only one lamp could be on at at a time and the user had to use command line arguments to set the initial traffic light and the amount of cycles the three lights would display.

The concepts applied in this major lab task included the following

traffic light

Designing classes with public and private attributes

Since we are working with hardware, it would be wise to have a private class to store the values of the PORTS being used, as well as appropriate accessor functions which could read and write to the ports. Also a private class variables to store the current state of the traffic light would also be safe thing to implement as well, to avoid conflict problems.

ousb class

Traffic light class


Implementing the classes with object orientated principles

After the classes were created, we could now implement the class functions with object orientated principles, including the use of constructors and advanced concepts including overloading constructors for the TrafficLight class.

OUSB class implementation

</pre>
<pre>unsigned short OUSB::runOUSBcommand(char *command)
{
 FILE *fpipe;
 char line[256];
 fpipe = (FILE*)_popen(command, "r"); // attempt to open pipe and execute a command 
 if (fpipe != NULL) // check that the pipe opened correctly
 {
 while (fgets(line, sizeof(line), fpipe))
 { // do nothing here, or print out debug data
 //cout << line; // print out OUSB data for debug purposes
 }
 _pclose(fpipe); // close pipe
 }
 else cout << "Error, problems with pipe!\n";
 int ousbOP = (int)atoi(line);
 return ousbOP;
}
unsigned short OUSB::readPORTA(unsigned short pinNumber)
{
 sprintf_s(command, "ousb −r io porta %d", pinNumber);
 PORTA = runOUSBcommand(command);
 
 return PORTA;
}
unsigned short OUSB::readPORTB()
{
 PORTB = runOUSBcommand("ousb −r io portb");
 return PORTB;
}
unsigned short OUSB::writePORTB(unsigned short newValue)
{
 sprintf_s(command, "ousb −r io portb %d", newValue);
 PORTB = runOUSBcommand(command);
 return PORTB;
}
unsigned short OUSB::readPORTC()
{
 PORTC = runOUSBcommand("ousb −r io pinc");
 return PORTC;
}</pre>
<pre>

TrafficLight Class implementation

</pre>
<pre>void TrafficLight::redOn()
{
 OUSB ousb;
 ousb.runOUSBcommand("ousb −r io portb 1");
}
bool TrafficLight::isREDon()
{
 OUSB ousb;
 if (ousb.readPORTB() == 1)
 redLamp = true;
 else
 redLamp = false;
 return redLamp;
}
void TrafficLight::yellowOn()
{
 OUSB ousb;
 ousb.runOUSBcommand("ousb −r io portb 3");
}
bool TrafficLight::isYELLOWon()
{
 OUSB ousb;
 if (ousb.readPORTB() == 3)
 return true;
 else
 return false;
}
void TrafficLight::greenOn()
{
 OUSB ousb;
 ousb.runOUSBcommand("ousb −r io portb 2");
}
bool TrafficLight::isGREENon()
{
 OUSB ousb;
 if (ousb.readPORTB() == 2)
 return true;
 else
 return false;
}
void TrafficLight::changeTrafficLightState()
{
 OUSB ousb;
 switch (ousb.readPORTB())
 {
 case 1:
 greenOn();
 break;
 case 2:
 yellowOn();
 break;
 case 3:
 redOn();
 break;
 default:
 cout << "error" << endl;
 break;
 }
}</pre>

Bitwise operators

The way the three lights are turned on are by setting the bits on the hardware with the help of bitwise operators. The way the set bits are sent is by using commands to the hardware, with the “runOUSBcommand” function. This can be seen in the TrafficLight class implementation code in void TrafficLight::redOn, TrafficLight::yellowOn and TrafficLight::greenOn. In the image below, the bits programmed for the traffic lights are the 8 LEDS on the OUSB Board.

board_picture

Control Structures and Command Line Arguments

The control structure in the int main() part of the code included control for the command line arguments which would control the program. If the argument entered in was one, it would output “Traffic Light program”. If argument three was set, it would run through the initial light entered in and the amount of cycles it would run though. A for loop control structure was used to control the amount of times the traffic lights were cycled through as the user had to input the number of cycles it would go through from the command line arguments.

Setting the program to set the green light first then run through 5 cycles

green traffic cycle

</pre>
<pre>int main(int argc, char *argv[])
{
 if (argc == 1) // no parameters print this line.
 {
 cout << "Traffic Light Program\n";
 }
 else if (argc == 3)
 {
 OUSB ousb;
 TrafficLight light;
 int num = atoi(argv[2]);
 if (num <= 50 && num >= 0)
 {
 if (argv[1][0] == 'R')
 {
 light.redOn();
 }
 else if (argv[1][0] == 'Y')
 {
 light.yellowOn();
 }
 else if (argv[1][0] == 'G')
 {
 light.greenOn();
 }

 // User input of the amount of cycles it will go through
 for (int i = 0; i < num; i++)
 {
 Sleep(500);
 light.changeTrafficLightState();
 }
 }
 else
 cout << "number out of range" << endl;
 }
 else
 cout << "wrong argument inputed" << endl;
 return 0;
}</pre>
<pre>

 

Share this post