#include <16F877A.h>
#device ADC=10
#fuses HS , NOWDT, NOPROTECT , NOLVP
#use delay(crystal=20000000)

// LCD PIN CONNECTIONS
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_ENABLE_PIN PIN_D2
#define LCD_DATA4 PIN_D3
#define LCD_DATA5 PIN_D4
#define LCD_DATA6 PIN_D5
#define LCD_DATA7 PIN_D6

#include <lcd.c>


// UART
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

// Inputs (Buttons)
#define SW1 PIN_A0 // Engine Start
#define SW2 PIN_A1 //Coolant
#define SW3 Pin_A2 //Fan

// Outputs (LEDs)
#define LED1 PIN_B0 //Start
#define LED2 PIN_B1 //Coolant
#define LED3 PIN_B2 //Alert 

// Motor (L293D)
#define MOTOR1 PIN_C0
#define MOTOR2 PIN_C1

void main()
{
 lcd_init();
     
  // Set pin directions
  set_tris_a(0xFF); // Inputs
  set_tris_b(0x00); // Outputs
  set_tris_c(0x00); // Motor
  set_tris_d(0x00); // LCD pins as output
  delay_ms(50); // VERY IMPORTANT
 
  
  // Turn everything OFF
  output_low(LED1);
  output_low(LED2);
  output_low(LED3);
  output_low(MOTOR1);

  
  // Initial message
  lcd_putc("\fSystem Ready");
  printf("System Ready\r\n");
  
  
  while(TRUE)
  { 
    // WAIT FOR ENGINE START
    while(!input(SW1)); // wait for press 
   output_high(LED1); // wait for press SW1
       lcd_putc("\fEngine ON");
      printf("Engine Started\r\n");
    delay_ms(500);
    
    
    {
       
      
      output_LOW(MOTOR1);
      //output_low(MOTOR2);
    
      delay_ms(500);
    } 
    
      // WAIT FOR COOLANT
      while(!input(SW2));
    output_low(LED1);
      output_high(LED2);
      lcd_putc("\fCoolant OK");
      printf("Coolant OK\r\n");
            output_LOW(MOTOR1);
      delay_ms(500);
      
      // WAIT FOR FAn
      while(!input(SW3));
      lcd_putc("\fFan Activated");
      printf("Fan Activated\r\n");
      
      
      // MOTOR ON
      output_high(MOTOR1);
      output_low(MOTOR2);
      
      lcd_putc("\nMotor Running");
      printf("Motor Running\r\n");
      
      delay_ms(3000); 
      
      // MOTOR OFF
      output_low(MOTOR1);
      output_low(MOTOR2);
      
        output_low(LED2);
   output_high(LED3);      
      // ALERT BLINK (3 TIMES, 100ms)
      for(int i=0; i<3; i++)
      {

        delay_ms(100);
    output_high(LED3);
        delay_ms(100);
    output_low(LED3);
    delay_ms(100);
   
    
        
      // Show alert on LCD and UART
      lcd_putc("\fALERT!");
      printf("ALERT!\r\n");
    
    
      } 
   
      
    
      
      // SYSTEM SHUTDOWN
      
      // Turn OFF motor
      output_low(MOTOR1);
      output_low(MOTOR2);
      
      // Turn OFF all LEDs
      output_low(LED1);
      output_low(LED2);
      output_low(LED3);
      
      // Display shutdown message
      lcd_putc("\fSystem shutdown");
      printf("System Shutdown\r\n");
      
      // To end
      while(1) {
    // Do nothing
      }
   
      
    
      }
     
    }
   
  


