#include <Servo.h>
#include <IRremote.hpp>

#define DECODE_NEC
const int IR_RECEIVE_PIN = 4;
int ir_command = -1;

Servo servo;
int servo_pin = 3;
int degree=0;
int dir=1;

int pot_pin = A0;

void setup() {
  Serial.begin(115200);
  IrReceiver.begin(IR_RECEIVE_PIN);
  printActiveIRProtocols(&Serial);
  servo.attach(servo_pin);
  pinMode(pot_pin, INPUT);
  servo.write(90);
}

void loop() {
  if (IrReceiver.decode()) {
    IrReceiver.resume();
    Serial.println(IrReceiver.decodedIRData.command,HEX);
    if (IrReceiver.decodedIRData.command == 0x68) {
      //0号键
      servo.write(0);
    } else if (IrReceiver.decodedIRData.command == 0x30) {
      //1号键
      servo.write(90);
    } else if (IrReceiver.decodedIRData.command == 0x18) {
      //2号键
      servo.write(180);
    } else if (IrReceiver.decodedIRData.command == 0x7a) {
      //4号键
      ir_command = 3;
    } else if (IrReceiver.decodedIRData.command == 0x10) {
      //4号键
      ir_command = 4;
      degree=servo.read();
    } else if (IrReceiver.decodedIRData.command == 0x38) {
      //5号键
      ir_command = 5;
    }
  }
  if (ir_command == 3) {
    servo.write(map(analogRead(pot_pin), 0, 1023, 0, 180));
  } else if (ir_command == 4) {
    degree+=dir;
    if(degree>180){
      dir=-1;
    }
    if(degree<0){
      dir=1;
    }
    Serial.println(degree);
    delay(5);
    servo.write(degree);
  } else if (ir_command == 5) {
    //什么也不用
  }
}