#define IR PINB2
int fade=0;
void setup() 
  {
  Serial.begin(9600);
  //DDRB=0x3C;PORTB=0x00;
  DDRD=0x20;PORTD=0x20;
  //pinMode(PB4, OUTPUT);
  //pinMode(PB5, OUTPUT);
  }

void loop()
  {  
  int key = getIrKey();
  (key!=0)?Serial.println(PORTB,BIN):false;
  switch (key)
    {
    case (0x30C8):  PORTB^=0x04;break;
    case (0x18E0):  PORTB^=0x08;break;
    case (0x7A7E):  PORTB^=0x10;break;
    case (0x10E8):  PORTB^=0x20;break;
    case (0x4FB8):  PORTB=0x00;break;
    case (0x5DAA):  PORTB=0x3C;break;
    default:        break;
    }
  ((fade>255)||(fade<1))?fade=-fade:fade+=1;
  analogWrite(PB5,fade);
  analogWrite(PB6,fade);
  }

int getIrKey()
  {
  int len=pulseIn(IR,LOW);
  int key,temp;
  key=0;
  if(len>5000)
    {
    for(int i=1;i<=32;i++)
      {
      temp=pulseIn(IR,HIGH);
      if(temp>1000) key+=(1<<(i-17));
      }
    }
  if(key<0) key=-key;
  return key;
  }