EXPERIMENT- 10
MEMBERS:- 4023151, 4023141, 4023144
AIM:- MODIFY TO SUPPORT 2 DIRECTION (NS & EW).
LOGIC;- The code alternates green, yellow, and red lights between two directions to manage traffic
flow safely and continuously.
CODE:
#include <xc.h>
#define _XTAL_FREQ 20000000
#define NS_RED_LIGHT PORTBbits.RB0
#define NS_YELLOW_LIGHT PORTBbits.RB1
#define NS_GREEN_LIGHT PORTBbits.RB2
#define EW_RED_LIGHT PORTBbits.RB3
#define EW_YELLOW_LIGHT PORTBbits.RB4
#define EW_GREEN_LIGHT PORTBbits.RB5
void init() {
TRISB = 0x00;
PORTB = 0x00;
void main() {
init();
while(1) {
// Phase 1: NS Green, EW Red
NS_RED_LIGHT = 0;
NS_YELLOW_LIGHT = 0;
NS_GREEN_LIGHT = 1;
EW_RED_LIGHT = 1;
EW_YELLOW_LIGHT = 0;
EW_GREEN_LIGHT = 0;
__delay_ms(0.5); // 5 seconds
// Phase 2: NS Yellow, EW Red
NS_RED_LIGHT = 0;
NS_YELLOW_LIGHT = 1;
NS_GREEN_LIGHT = 0;
EW_RED_LIGHT = 1;
EW_YELLOW_LIGHT = 0;
EW_GREEN_LIGHT = 0;
__delay_ms(0.15); // 1.5 seconds
// Phase 3: NS Red, EW Green
NS_RED_LIGHT = 1;
NS_YELLOW_LIGHT = 0;
NS_GREEN_LIGHT = 0;
EW_RED_LIGHT = 0;
EW_YELLOW_LIGHT = 0;
EW_GREEN_LIGHT = 1;
__delay_ms(0.5); // 5 seconds
// Phase 4: NS Red, EW Yellow
NS_RED_LIGHT = 1;
NS_YELLOW_LIGHT = 0;
NS_GREEN_LIGHT = 0;
EW_RED_LIGHT = 0;
EW_YELLOW_LIGHT = 1;
EW_GREEN_LIGHT = 0;
__delay_ms(0.15); // 1.5 seconds
}
OUTPUT:
CONCLUSION:
This program successfully implements a two-direction (North-South and East-West) traffic light
control system using a PIC microcontroller. The logic ensures that while one direction shows a green
or yellow light, the other direction remains on red, preventing collisions. It continuously cycles
through the four standard traffic phases, allowing smooth and safe vehicle movement in both
directions. With proper delay adjustments, it can accurately simulate real-world traffic light timing.
Overall, the code provides a simple, efficient, and reliable method for managing two-way traffic
flow at an intersection..