#include <18F452.h>
#include <main.h>


//---------DECLARE GLOBAL VARIABLES-------------------------------------------------------------//
//#include <stdio.h>
void but();
void pot();
void traffic();
void countFunc();
//unsigned int nums[10]={ 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
unsigned int nums[10]={0x6f, 0x7f, 0x07, 0x7d, 0x6d, 0x66, 0x4f, 0x5b, 0x06, 0x3f};
unsigned int count = 0;
unsigned int pot1 = 0;
unsigned int pot2 = 0;
unsigned int pot3 = 0;
unsigned int pot4 = 0;

void main()
{
	setup_adc_ports(ALL_ANALOG);			//set port a to ADC																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																					
	setup_adc(ADC_CLOCK_INTERNAL);																					
      
	while(TRUE)
	{
	    output_d(0x03);				//S1 & S4 green, S2 & S3 red
	    output_e(0x02);				//Pedestrian crossing lights red
	    pot();					//pot adc function
	    delay_ms(500);
	    traffic();					//traffic light function
	    //countFunc();				//counter test function
	    but();					//pedestrian crossing button function
	}
       
}
//---------------------------------TRAFFIC LIGHTS FUNCTION-----------------------------------------------------------------------------//
void traffic (void)
   {
      if(!input(PIN_C2))
	 {
	    
	    delay_ms(1000);
	    output_d(0x06);				//S1 & S4 orange, S2& S3 red
	    delay_ms(200);					
	    output_d(0x18);				//S1 & S4 red, S2 & S3 orange
	    delay_ms(200);
	    output_d(0x30);				//S1 & S4 red, S2 & S3 green						
	    
	    delay_ms(1000);
	    
	   
	    output_d(0x18);				//S1 & S4 red, S2 & S3 orange
	    delay_ms(200);
	    output_d(0x03);				//S1 & S4 green, S2 & S3 red
	    delay_ms(1000);
	    
	 }
   }

//---------------------------------PEDESTRIAN CROSSING BUTTON FUNCTION-------------------------------------------------------------------------//

void but(void)
   {	
      while(input(PIN_C2))
	  {
	   delay_ms(100);
	   output_d(0xc);				//All traffic lights orange
	   delay_ms(100);				
	   output_d(0x12);				//All traffic lights red
	   delay_ms(200);
	   output_e(0x01);				//Pedestrian crossing lights green
	   delay_ms(500);
	   !output_d(0xc0);
	   if(output_e(0x01))
	    {
	       output_high(PIN_D6);
	       output_low(PIN_D7);



	       while(count<10)
		  {
		     output_b(nums[count]);
		     delay_ms(100);
		     count++;
		  }
			   
		  count = 0;
			 
	       }

	    }
	   delay_ms(500);				//delay to let people cross the road
	   
 
 
   }
 //---------------------------------------POT TO SIMULATE DETECTION-OF-TRAFFIC FUNCTION--------------------------------------------------------------------------//
void pot (void)
   {
      set_adc_channel(0);				//set analogue digital channel 0, etc.
      delay_us(20);					
      pot1 = read_adc();				//read adc channels for respective pot's
      set_adc_channel(1);
      delay_ms(20);
      pot2 = read_adc();
      set_adc_channel(2);
      delay_ms(20);
      pot3 = read_adc();
      set_adc_channel(3);
      delay_ms(20);
      pot4 = read_adc();
      printf("pot 1 = %3U\n\r", pot1);			//print respective pot values to terminal
      printf("pot 2 = %3U\n\r", pot2);
      printf("pot 3 = %3U\n\r", pot3);
      printf("pot 4 = %3U\n\r", pot4);
     
      if(pot1>170)
	 {
	    output_d(0x03);				//S1 & S4 green, S2 & S3 red
	 }
	 
	 if(pot2>170)
	  {
	    output_d(0x30);					//S1 & S4 red, S2 & S3 green
	  }
	    
           
   }
//-------------------------------------------------------counter test function-----------------------------------------------------------------------------------------//
   
void countFunc()
   {	
    output_high(PIN_D6);
    output_low(PIN_D7);


      while(count<10)
	 {
	 output_b(nums[count]);
	 delay_ms(100);
	 count++;
	 }
      
      count = 0;
    
      }
       



   