#include <16F877A.h>
#fuses XT, NOWDT, NOPROTECT, NOLVP
#use delay(clock=20000000)

// Define LED Pins for Traffic Lights
#define LED_RED_A PIN_B0
#define LED_YELLOW_A PIN_B1
#define LED_GREEN_A PIN_B2

#define LED_RED_B PIN_B3
#define LED_YELLOW_B PIN_B4
#define LED_GREEN_B PIN_B5

#define LED_RED_C PIN_A1
#define LED_YELLOW_C PIN_A2
#define LED_GREEN_C PIN_A3

// Define Pedestrian Light Pins
#define P_YELLOW PIN_A0
#define P_RED PIN_B6
#define P_GREEN PIN_B7

// Define Pedestrian Button Pin
#define PED_BUTTON PIN_A5

// 7-segment display digit patterns (assuming common cathode)
unsigned int display[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};

void pedestrian_stage() {
    int i;

    // Pedestrian Crossing Phase: All Traffic Lights Red
    for (i = 0; i < 10; i++) { 
        output_d(0xB0); //'3'
        output_c(display[i]); // Update 7-segment display with current count

        // Set all traffic lights to red
        output_high(LED_RED_A);    
        output_low(LED_YELLOW_A); 
        output_low(LED_GREEN_A);  
        output_high(LED_RED_B);    
        output_low(LED_YELLOW_B); 
        output_low(LED_GREEN_B);  
        output_high(LED_RED_C);    
        output_low(LED_YELLOW_C); 
        output_low(LED_GREEN_C);  

        // Turn on pedestrian green
        output_high(P_GREEN);   // Turn ON pedestrian GREEN

        // Flash pedestrian yellow while pedestrian green is on
        output_toggle(P_YELLOW); // Toggle pedestrian YELLOW
        delay_ms(1000);           // Flash every 500 ms
    }
for(i=0;i<1;i++)
{
         output_d(0x99); //'4'
      output_c(display[i]); // Update 7-segment display with count
      delay_ms(300); //1 second delay
}
    // Reset pedestrian lights after cycle
    output_low(P_YELLOW); 
    output_low(P_GREEN); 
}

void main() {
    int i;
    int count = 0; // Initialize count for 7-segment display

    set_tris_b(0x00); // Set PORTB as output for traffic lights
    output_b(0x00);   // Initialize PORTB to 0
    delay_ms(300);    // Initial delay
    set_tris_c(0x00); // Set PORTC as output for 7-segment display
    output_c(0x00);   // Initialize PORTC to 0 
    set_tris_d(0x00); // Set PORTD as output for 7-segment display
    output_d(0x00);   // Initialize PORTD to 0
    set_tris_a(0x10); // Set PIN_A4 (PED_BUTTON) as input

    while (1) {
        // Check if the pedestrian button is pressed
        if (input(PED_BUTTON)) {
            delay_ms(50); // Debounce the button
            if (input(PED_BUTTON)) { // Confirm button press
                pedestrian_stage();
                continue; // Return to normal operation after pedestrian stage
            }
        }

        // Phase 1: Traffic Light A - Red, B - Yellow, C - Green
        output_d(0xC0); //'0'
        for (i = 0; i < 10; i++) {
            output_c(display[count]); // Update 7-segment display with current count
            count = (count + 1) % 10;  // Increment count and loop back to 0 if it reaches 9

            output_high(LED_RED_A); // Turn on RED for Traffic Light A
            output_low(LED_YELLOW_A); 
            output_low(LED_GREEN_A); 
            
            output_low(LED_RED_B); 
            output_high(LED_YELLOW_B); // Turn on YELLOW for Traffic Light B
            output_low(LED_GREEN_B); 
            
            output_low(LED_RED_C); 
            output_low(LED_YELLOW_C); 
            output_high(LED_GREEN_C); // Turn on GREEN for Traffic Light C

            output_high(P_RED);    // Turn ON Pedestrian RED
            delay_ms(1000);        // 1-second delay
        }

        // Phase 2: Traffic Light A - Green, B - Red, C - Yellow
        output_d(0xF9); //'1'
        for (i = 0; i < 10; i++) {
            output_c(display[count]); // Update 7-segment display with current count
            count = (count + 1) % 10;  // Increment count and loop back to 0 if it reaches 9

            output_low(LED_RED_A);    
            output_low(LED_YELLOW_A); 
            output_high(LED_GREEN_A);  // Turn on GREEN for Traffic Light A
            
            output_high(LED_RED_B); 
            output_low(LED_YELLOW_B); 
            output_low(LED_GREEN_B); // Turn on RED for Traffic Light B
            
            output_low(LED_RED_C); 
            output_high(LED_YELLOW_C); // Turn on YELLOW for Traffic Light C
            output_low(LED_GREEN_C);   

            output_high(P_RED);      // Turn ON Pedestrian RED
            delay_ms(1000);          // 1-second delay 
        }

        // Phase 3: Traffic Light C - Green, A - Yellow, B - Red
        output_d(0xA4); //'2'
        for (i = 0; i < 10; i++) {
            output_c(display[count]); // Update 7-segment display with current count
            count = (count + 1) % 10;  // Increment count and loop back to 0 if it reaches 9

            output_low(LED_RED_A);    
            output_high(LED_YELLOW_A); // Turn on YELLOW for Traffic Light A
            output_low(LED_GREEN_A);  
            
            output_low(LED_RED_B); 
            output_low(LED_YELLOW_B); // Turn on GREEN for Traffic Light B
            output_high(LED_GREEN_B);  
            
            output_high(LED_RED_C);   // Turn on RED for Traffic Light C
            output_low(LED_YELLOW_C); 
            output_low(LED_GREEN_C); 

            output_high(P_RED);      // Turn ON Pedestrian RED
            delay_ms(1000);          // 1-second delay 
        }
    }
 }


