// LCD module connections
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections

bit oldstate1;
bit oldstate2;
bit oldstate3;
bit oldstate4;

int i=0,j=0;

void main() {

  TRISC1_bit = 1;                                // set RC1 pin as input
  TRISC2_bit = 1;                                // set RC2 pin as input
  TRISC3_bit = 1;                                // set RC3 pin as input
  TRISC4_bit = 1;                                // set RC4 pin as input
  
  TRISB = 0x00;                                  // Configure PORTB as output
  TRISE = 0x00;
  TRISC.B0 = 0x00;
  //PORTB = 0xAA;                                  // Initial PORTB value
  oldstate1 = 0;
  oldstate2 = 0;
  oldstate3 = 0;
  oldstate4 = 0;
  
  Lcd_Init();                        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);           // Cursor off
  Lcd_Out(1, 7, "MENU");
  do{

    if (Button(&PORTC, 1, 1, 1)) {
      oldstate1 = 1;
       }

    if (oldstate1 && Button(&PORTC, 1, 1, 0)) {
      oldstate1 = 0;
      i=i+1;
      j=j+1;
      
    if(i==1){
       Lcd_Out(1, 6, "RELE1");

    }
    if(i==2){
       Lcd_Out(1, 6, "RELE2");
    }
    if(i==3){
       Lcd_Out(1, 6, "RELE3");
    }
    if(i==4){
       Lcd_Out(1, 6, "RELE4");
    }
    if(i==5){
       Lcd_Out(1, 6, "RELE5");
    }
    if(i==6){
       Lcd_Out(1, 6, "RELE6");
       i=0;
    }
    }
    if (Button(&PORTC, 2, 1, 1)) {
      oldstate2 = 1;
       }

    if (oldstate2 && Button(&PORTC, 2, 1, 0)) {
      oldstate2 = 0;
      i=i-1;
      j=j-1;
    if(i==1){
       Lcd_Out(1, 6, "RELE1");
       i=7;
    }
    if(i==2){
       Lcd_Out(1, 6, "RELE2");
    }
    if(i==3){
       Lcd_Out(1, 6, "RELE3");
    }
    if(i==4){
       Lcd_Out(1, 6, "RELE4");
    }
    if(i==5){
       Lcd_Out(1, 6, "RELE5");
    }
    if(i==6){
       Lcd_Out(1, 6, "RELE6");
    }
    }
    if (Button(&PORTC, 3, 1, 1)) {
      oldstate3 = 1;
       }

    if (oldstate3 && Button(&PORTC, 3, 1, 0)) {
      oldstate3 = 0;

    if(j==1){
       PORTE.B2=~PORTE.B2;
       }
    if(j==2){
       PORTC.B0=~PORTC.B0;
       }
    if(j==3){
       PORTB.B0=~PORTB.B0;
       }
    if(j==4){
       PORTB.B1=~PORTB.B1;
       }
    if(j==5){
       PORTB.B4=~PORTB.B4;
       }
    if(j==6){
       PORTB.B5=~PORTB.B5;
       }
     }
     if (Button(&PORTC, 4, 1, 1)) {
      oldstate4 = 1;
       }

     if (oldstate4 && Button(&PORTC, 4, 1, 0)) {
      oldstate4 = 0;
        Lcd_Cmd(_LCD_CLEAR);
        Lcd_Out(1, 7, "MENU");
        i=0;
        j=0;
      }
  }while(1) ;

}