#include <16F877A.h>
#fuses NOWDT, HS, NOPROTECT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N, stop=1)

// Define pin names for clarity 

//LED's
#define  LED1  PIN_B3                              // LED connected to RB3 - Star Signal
#define  LED2  PIN_B4                              // LED connected to RB4 - Coolannt Confirmation
#define  LED3  PIN_B5                              // LED connected to RB5 - Alert Indicator

// Input (Switches) 
#define  SW1   PIN_C0                              // Switch connected to RC0 - Engine Start
#define  SW2   PIN_C1                              // Switch connected to RC1 - Coolant Sensor Trigger
#define  SW3   PIN_C2                              // Switch connected to RC2 - Fan Activation

// Outputs (morot driver)
#define MOTOR_IN1   PIN_B1                         // Motor connected to RB1 
#define MOTOR_IN2   PIN_B2                         // Motor connected to RB2 

// LCD connected to PORTD
#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>


void main() {

   set_tris_b(0b11111001);
   
   // All RC pins as inputs (SW1-SW3 use RC0-RC2)
   set_tris_c(0b11111111);
   
   // Initialize motor outputs LOW (motor off)
   output_low(MOTOR_IN1);
   output_low(MOTOR_IN2);

   set_tris_d(0x00);                                  // Set PORTD as output
   lcd_init(); 
  
   lcd_putc("\fSystem Ready");                        // Display System Ready
   printf("System Ready!\r\n");                       // Display System Ready
  
   while(!input(SW1));                                // Start Signal
   delay_ms(50);   
   
   lcd_putc("\fEngine Started");                      // Display Engine Started
   printf("Engine Started!\r\n");                     // Display Engine Started
   output_high(PIN_B3);                               // Turn on the LED
   delay_ms(1500);

   while(!input(SW2));                                // Coolant Confirmation
   output_high(PIN_B4);                               // Turn on the LED
   delay_ms(50);  
   
   lcd_putc("\fCoolant Okay");                        // Display Fan Activated
   printf("Coolant Okay!\r\n");                       // Display Fan Activated
   delay_ms(1500);   

   while(!input(SW3));                                // Blink Altert LED
   delay_ms(50);  
   lcd_putc("\fFan Activated\nMotor Running");        // Display Fan Activated
   printf("Fan Activated!\r\n");                      // Display Fan Activated
   printf("Motor Running!\r\n");                      // Display Motor Running
   output_high(MOTOR_IN1);                            // Turn on the Motor
   output_low(MOTOR_IN2);                             // Turn on the Motor
   delay_ms(3000);                                    // Wait for 3000 milliseconds (3 Seconds)
   
   printf("Alert!\r\n");                              // Display Alert
   lcd_putc("\fAlert");                               // Display Alert  
   output_high(PIN_B5);                               // Turn on the LED
   delay_ms(100);                                     // Wait for 100 milliseconds
   output_low(PIN_B5);                                // Turn off the LED
   delay_ms(100);                                     // Wait for 100 milliseconds
   output_high(PIN_B5);                               // Turn on the LED
   delay_ms(100);                                     // Wait for 100 milliseconds
   output_low(PIN_B5);                                // Turn off the LED
   delay_ms(100);                                     // Wait for 100 milliseconds
   output_high(PIN_B5);                               // Turn on the LED
   delay_ms(100);                                     // Wait for 100 millisecond
   lcd_putc("\fShutdowm");                            // Display Alert
   printf("Shutdown!\r\n");                           // Display Alert
   
   output_low(PIN_B3);                                // Turn off the LED
   output_low(PIN_B4);                                // Turn off the LED                 
   output_low(PIN_B5);                                // Turn off the LED
   output_low(MOTOR_IN1);                             // Turn off the Motor
   output_low(MOTOR_IN2);                             // Turn off the Motor
   
   while(TRUE) {
   
   }
}
   
  
