Thursday, April 26, 2012

Final Project Code_Final (for SONY TV)


#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,13,10,9,8);
#include <IRremote.h>
IRsend irsend;

int buttonState1 = 1;
int buttonState2 = 1;
int buttonPin1 = 4;
int buttonPin2 = 6;
int power = 0;
int menuState = 0; //Channel Surf = 1, Channel input = 2, Volume = 3, Power = 4;
int displayState = 0; //Channel Surf = 1, Channel input = 2, Volume = 3, Power = 4;
int CS = 0; //Channel up = 1, Channel down = 2, Exit = 3
int CI = 0; //hundreds = 1, tens = 2, single = 3, Exit = 4
int hundreds = 0;
int tens = 0;
int ones = 0;
int Vol = 0; //Volume up = 1, Volume down = 2, Exit = 3
int channel = 0;
void setup(){
  Serial.begin(9600);
  lcd.begin(16,2);
}
void loop(){
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  lcd.noAutoscroll();
  if (power == 1){
    //switch through main menu
    if(menuState == 0){
      if(displayState == 1){
        //LCD Displays Channel Surf Menu
        Serial.println("Go to Channel Surf Menu");
        //        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("To Channel Surf ");
      }
      if(displayState == 2){
        //LCD Displays Channel Input menu
        Serial.println("Go to Channel Input Menu");
        lcd.setCursor(0,0);
        lcd.print("To Channel Input");
      }
      if(displayState == 3){
        //LCD Displays Volume Menu
        Serial.println("Go to Volume Menu");
        lcd.setCursor(0,0);
        lcd.print("To Volume Menu  ");
      }
      if(displayState == 4){
        //LCD Displays Power Menu
        Serial.println("Go to Power Menu");
        lcd.setCursor(0,0);
        lcd.print("Turn Off        ");
      }
      if(buttonState2 == 0){
        displayState = displayState + 1;
        if(displayState == 5){
          displayState = 1;
        }
      }
    }
    //go into the menu (not just display)
    if(buttonState1 == 0){
      menuState = displayState;
    }
    //Channel Surf Menu
    if (menuState == 1){
      delay(100);
      buttonState1 = digitalRead(buttonPin1);
      buttonState2 = digitalRead(buttonPin2);
      Serial.println("In Channel Surf Menu");
      lcd.setCursor(0,0);
      lcd.print("Channel Surf    ");
      if(buttonState2 == 0){
        CS = CS+1;
        if(CS == 4){
          CS = 1;
        }
        Serial.println(CS);
        if (CS == 1){
          lcd.setCursor(0,1);
          lcd.print("Channel Up      ");
        }
        if (CS == 2){
          lcd.setCursor(0,1);
          lcd.print("Channel Down    ");
        }
        if (CS == 3){
          lcd.setCursor(0,1);
          lcd.print("To Main Menu    ");
        }
      }
      if(CS == 1){
        if(buttonState1 == 0){
          Serial.println("transmit code to switch channel up");
          //transmit code to switch channel up
          for (int i = 0; i<3; i++){
            irsend.sendSony(0x90,12);
            delay(40);
          }
          delay(100);
          Serial.println("transmitted");
        }
      }
      if(CS == 2){
        if(buttonState1 == 0){
          Serial.println("transmit code to switch channel down");
          //transmit code to switch channel down
          for (int i = 0; i<3; i++){
            irsend.sendSony(0x890,12);
            delay(40);
          }
          delay(100);
          Serial.println("transmitted");
        }  
      }
      if(CS == 3){
        if(buttonState1 == 0){
          menuState = 0;
          lcd.clear();
          //goes back to main menu
          Serial.println("go back to main menu");
        }
      }
    }
    //Channel Input Menu
    if (menuState == 2){
      delay(100);
      buttonState1 = digitalRead(buttonPin1);
      buttonState2 = digitalRead(buttonPin2);
      Serial.println("in channel input menu");
      lcd.setCursor(0,0);
      lcd.print("Channel Input   ");
      if(buttonState2 == 0){
        CI = CI + 1;
        if (CI == 6){
          CI = 1;
        }
        Serial.println(CI);
        //        if(CI == 1){
        //          //          lcd.setCursor(0,1);
        //          ////          lcd.print("                ");
        //          //          lcd.print("hundreds");
        //        }
        //        if(CI == 2){
        //          //          lcd.setCursor(0,1);
        //          ////          lcd.print("                ");
        //          //          lcd.print("tens");
        //        }
        //        if(CI == 3){
        //          //          lcd.setCursor(0,1);
        //          ////          lcd.print("                ");
        //          //          lcd.print("ones");
        //        }
                if(CI == 4){
                            lcd.setCursor(0,1);
                            lcd.print("Transmit Channel");
                }
                if(CI == 5){
                            lcd.setCursor(0,1);
                            lcd.print("To Main Menu    ");
                }
      }
      if(buttonState1 == 0){
         if(CI == 1){
          hundreds = hundreds + 1;
          if(hundreds == 10){
            hundreds = 0;
          }
          channel = 100*hundreds + 10*tens + ones;
          lcd.setCursor(0,1);
          //          lcd.print("                ");
          lcd.print(channel);
        }
        if(CI == 2){
          tens = tens + 1;
          if (tens == 10){
            tens = 0;
          }
          channel = 100*hundreds + 10*tens + ones;
          lcd.setCursor(0,1);
          //          lcd.print("                ");
          lcd.print(channel);
        }
        if(CI == 3){
          ones = ones + 1;
          if (ones == 10){
            ones = 0;
          }
          channel = 100*hundreds + 10*tens + ones;
          lcd.setCursor(0,1);
          //          lcd.print("                ");
          lcd.print(channel);
        }
        if(CI == 4){
          Serial.println("transmit input");
          lcd.setCursor(0,1);
          //          lcd.print("                ");
          lcd.print("Transmit Channel");
          //transmit code to input channel
         
          if(hundreds == 0){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x910,12);
              delay(40);
            }
          }
          if(hundreds == 1){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x10,12);
              delay(40);
            }
          }
          if(hundreds == 2){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x810,12);
              delay(40);
            }
          }
          if(hundreds == 3){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x410,12);
              delay(40);
            }
          }
          if(hundreds == 4){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0xC10,12);
              delay(40);
            }
          }
          if(hundreds == 5){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x210,12);
              delay(40);
            }
          }
          if(hundreds == 6){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0xA10,12);
              delay(40);
            }
          }
          if(hundreds == 7){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x610,12);
              delay(40);
            }
          }
          if(hundreds == 8){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0xE10,12);
              delay(40);
            }
          }
          if(hundreds == 9){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x110,12);
              delay(40);
            }
          }
          delay(500);
          if(tens == 0){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x910,12);
              delay(40);
            }
          }
          if(tens == 1){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x10,12);
              delay(40);
            }
          }
          if(tens == 2){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x810,12);
              delay(40);
            }
          }
          if(tens == 3){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x410,12);
              delay(40);
            }
          }
          if(tens == 4){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0xC10,12);
              delay(40);
            }
          }
          if(tens == 5){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x210,12);
              delay(40);
            }
          }
          if(tens == 6){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0xA10,12);
              delay(40);
            }
          }
          if(tens == 7){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x610,12);
              delay(40);
            }
          }
          if(tens == 8){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0xE10,12);
              delay(40);
            }
          }
          if(tens == 9){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x110,12);
              delay(40);
            }
          }
          if(ones == 0){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x910,12);
              delay(40);
            }
          }
          delay(500);
          if(ones == 1){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x10,12);
              delay(40);
            }
          }
          if(ones == 2){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x810,12);
              delay(40);
            }
          }
          if(ones == 3){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x410,12);
              delay(40);
            }
          }
          if(ones == 4){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0xC10,12);
              delay(40);
            }
          }
          if(ones == 5){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x210,12);
              delay(40);
            }
          }
          if(ones == 6){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0xA10,12);
              delay(40);
            }
          }
          if(ones == 7){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x610,12);
              delay(40);
            }
          }
          if(ones == 8){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0xE10,12);
              delay(40);
            }
          }
          if(ones == 9){
            for (int i = 0; i<3; i++){
              irsend.sendSony(0x110,12);
              delay(40);
            }
          }
//          ones = 0;
//          tens = 0;
//          hundreds = 0;

        }
        if(CI == 5){
          menuState = 0;
          Serial.println("back to main menu");
          lcd.clear();
          ones = 0;
          tens = 0;
          hundreds = 0;
        }
      }
      Serial.print(hundreds);
      Serial.print(tens);
      Serial.println(ones);
    }
    if (menuState == 3){
      buttonState1 = digitalRead(buttonPin1);
      buttonState2 = digitalRead(buttonPin2);
      delay(50);
      Serial.println("in volume menu");
     
      lcd.setCursor(0,0);
      lcd.print("Volume          ");
      if(buttonState2 == 0){
        Vol = Vol+1;
        if(Vol == 4){
          Vol = 1;
        }
        Serial.println(Vol);
        if (Vol == 1){
          lcd.setCursor(0,1);
          lcd.print("Volume Up     ");
        }
        if (Vol == 2){
          lcd.setCursor(0,1);
          lcd.print("Volume Down   ");
        }
        if (Vol == 3){
          lcd.setCursor(0,1);
          lcd.print("To Main Menu    ");
        }
      }
      if(Vol == 1){
        if(buttonState1 == 0){
          //transmit code to switch volume up
          for (int i = 0; i<3; i++){
            irsend.sendSony(0x490,12);
            delay(40);
          }
          delay(100);
          Serial.println("transmitted");
          Serial.println("Transmit code to switch volume up");
        }
      }
      if(Vol == 2){
        if(buttonState1 == 0){
          //transmit code to switch volume down
          for (int i = 0; i<3; i++){
            irsend.sendSony(0xC90,12);
            delay(40);
          }
          delay(100);
          Serial.println("transmitted");
          Serial.println("Transmit code to switch volume down");
        }
      }
      if(Vol == 3){
        if(buttonState1 == 0){
          menuState = 0;
          lcd.clear();
          Serial.println("Back to main menu");
        }
      }
    }
    if (menuState == 4){
      buttonState1 = digitalRead(buttonPin1);
      buttonState2 = digitalRead(buttonPin2);
      delay(50);
      if (buttonState1 == 0){
        //transmit code o switch TV off
        Serial.println("power off");
        lcd.clear();
        power = 0;
      }
      if (buttonState2 == 0){
        menuState = 0;
      }
      delay(50);
    }
  }
  if(power == 0){
    buttonState1 = digitalRead(buttonPin1);
    buttonState2 = digitalRead(buttonPin2);
    if(buttonState1 == 0){
      power = 1;
    }
    else if(buttonState2 == 0){
      power = 1;
    }
    if(power == 1){
      //transmit code to turn power on
      for (int i = 0; i<3; i++){
        irsend.sendSony(0xA90,12);
        delay(20);
      }
      delay(100);
      Serial.println("transmitted");
//      for (int i = 0; i<3; i++){
//        irsend.sendSony(0xA90,12);
//        delay(40);
//      }
      Serial.println("power is on");
      lcd.print("Power ON");
      displayState = 1;
    }
  }
  if (buttonState1 + buttonState2 < 2){
    delay(200);
  }
  else{
    delay(100);
  }
}

No comments:

Post a Comment