#include <main asignment.h>
#fuses HS, NOWDT, NOPROTECT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7, bits=8, parity=N, stop=1)


// Defining LED & Fan on PORT B as Outputs 
#define LED_1                     PIN_B0
#define LED_2                     PIN_B1
#define LED_3                     PIN_B2
#define MOTOR_IN1                 PIN_B3
#define MOTOR_IN2                 PIN_B4

// deifing Switches on PORT C as Inputs
#define SW1                       PIN_C0
#define SW2                       PIN_C1
#define SW3                       PIN_C2

// LCD on PORTD (4-bit)

#define LCD_RW_PIN                PIN_D1
#define LCD_RS_PIN                PIN_D0
#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(){

//PORTB
set_tris_b(0b11111000);                    // Set RB0-RB3 as Outputs

// PORT C All RC pins as inputs (SW1-SW3 use RC0-RC2)
set_tris_c(0b11111111);
// PORT D  All RD pins as outputs
set_tris_d(0b00000000);
lcd_init();                              // Initialisze LCD once

// Initialize motor outputs LOW (motor off)
Output_low(MOTOR_IN1);
Output_low(MOTOR_IN2);
   
 //   Initialize all LED outputs LOW (LEDS off)
Output_low(LED_1);
Output_low(LED_2);    
Output_low(LED_3);
lcd_putc("\fSystem Ready");              // Output Message on LCD
Printf("System Ready\r\n");              // Output Message on UART
// Start engine
While (!input(SW1));  
output_high(LED_1);                      // Green LED 1 ON

Delay_ms(50);                          //  Debounce timer
lcd_putc("\fEngine Started");            // Output Engine Started Message on LCD
Printf("Engine Started\r\n");            // Output Engine Started Message on UART

While (!input(SW2));                     // Coolant Sensor Trigger
output_high(LED_2);                      // Green LED 2 ON
lcd_putc("\fCoolant OK");                // Output Coolant OK Message on LCD
Printf("Coolant OK\r\n");                // Output Coolant OK Message on UART


// Fan acitvated
While (!input(SW3));    
lcd_putc("\fFan Activated\nMotor Running");// Output Fan Activated on 1st line and Motor Running on 2nd line Message on LCD
Printf("Fan Activated, Motor Running\r\n");// Output Fan Activated Message on UART Output Motor Running on UART         
output_high(MOTOR_IN1);                    // Fan running Clockwise
output_low(MOTOR_IN2);

delay_ms(3000);                            // 3 seconds delay
lcd_putc("\fALERT!");                      // Output ALERT! Message on LCD
Printf("ALERT!\r\n");                      // Output ALERT! Message on UART

// RED LED 3 blink 3 times with delay on and off 100ms 
for(int i = 0; i < 3; i++)   {
    output_high(LED_3);
    delay_ms(100);

    output_low(LED_3);
    delay_ms(100);}

delay_ms(1000);
 output_low(MOTOR_IN1);                     // Fan Stop
output_low(MOTOR_IN2);      
output_low(LED_2);                          // Output RED LED 2 OFF
output_low(LED_1);                          // Output RED LED 2 OFF
lcd_putc("\fShutdown Done");                // Output Shutdown Done Message on LCD 
Printf("Shutdown Done\r\n");                // Output Shutdown Done Message on UART
 
While (TRUE) {
 }
}
