#include <IRremote.h>
#include <Servo.h>
#define PIN_RECEIVER 12 // Signal Pin of IR receiver
#define PIN_SERVO 3
IRrecv receiver(PIN_RECEIVER);
Servo servo1;
void setup()
{
Serial.begin(9600);
receiver.enableIRIn(); // Start the receiver
servo1.attach(PIN_SERVO);
}
int rotation=90;
int flag=0;//不转 0 1转自动转
int zhuanfx=1;//1右转 0左转
void loop()
{
// Checks received an IR signal
if (receiver.decode()) {
flag=0;
if(receiver.decodedIRData.command==226) //menu键 左 右180 左0 右
{
flag=1;//1转自动转
}
/*if(flag=1)
{
for(rotation=0;rotation<=180;rotation++)
{
servo1.write(rotation);
delay(15);
if(rotation>=180)
{
for(rotation=180;rotation>=0;rotation--)
{
servo1.write(rotation);
delay(15);
}
}
}
}*/
/*rotation = rotation - 10;
if(rotation<0) rotation=0;
servo1.write(rotation);
Serial.println("NEXT");
rotation = rotation + 10;
if(rotation>180) rotation=180;
servo1.write(rotation);*/
if(receiver.decodedIRData.command==224)//224 “PREV” 键 左---
{
//左右转
Serial.println("PREV");
rotation = rotation - 10;
if(rotation<0) rotation=0;
servo1.write(rotation);
}
/*
if(receiver.decodedIRData.command==2)//2 +
{
flag=0;
//左右转
Serial.println("PREV");
rotation = rotation - 10;
if(rotation<0) rotation=0;
servo1.write(rotation);
if(receiver.decodedIRData.command==152)//152 -
{
flag=0;
//左右转
Serial.println("PREV");
rotation = rotation - 10;
if(rotation<0) rotation=0;
servo1.write(rotation);
*/
if(receiver.decodedIRData.command==144)//144 "NEXT"键 右 +++
{
Serial.println("NEXT");
rotation = rotation + 10;
if(rotation>180) rotation=180;
servo1.write(rotation);
}
if(receiver.decodedIRData.command==34)//34 "TEXT"键准备开始键入数字
{
int num = -1;
rotation = 0;
Serial.println("start INPUT number");
receiver.resume();
delay(10);
while(1)
{
if(receiver.decode())
{
if(receiver.decodedIRData.command == 168) break;//168 “play”键 数字输入结束
num = inputNum();//读入数字,0-9 非数字为-1
if(num!=1)
{
rotation = rotation * 10 +num;
Serial.println(rotation);
}
receiver.resume();
delay(10);
}
}
Serial.println("move point!");
servo1.write(rotation);
}
receiver.resume();
}
else
{
/*左右转*/
if(rotation>180 && zhuanfx==1) zhuanfx=0;
if(rotation<0 && zhuanfx==0) zhuanfx=1;
if(zhuanfx==1)
{
rotation = rotation + 10;
}
if(zhuanfx==0)
{
rotation = rotation - 10;
}
servo1.write(rotation);
delay(100);
}
}
}
int inputNum()
{
int num;
switch(receiver.decodedIRData.command)
{
case 104:
num=0;
break;
case 48:
num=1;
break;
case 14:
num=2;
break;
case 122:
num=3;
break;
case 16:
num=4;
break;
case 56:
num=5;
break;
case 90:
num=6;
break;
case 66:
num=7;
break;
case 74:
num=8;
break;
case 82:
num=9;
break;
default:
num=-1;
}
return num;
}/*
}
}
receiver.decode();
while(receiver.decodedIRData.command!=168)//168 "play"键 数字输入结束
{
num=inputNum(()); //读入数字,0-9 非数字为-1
if(num!=-1) rotation=rotation*10+num;
receiver.resume();
//Serial.println(rotation);
//receiver.resume();
delay(10);
receiver.decode();
}
Serial.println("move point!");
servol.write(rotation)
}
receiver.resume(); // Receive the next value
}
}
int inputNum()
{
int num;
switch(receiver.decodedIRData.command)
{
case 104:
num=0;
break;
case 104:
num=1;
break;
case 104:
num=2;
break;
case 104:
num=3;
break;
case 104:
num=4;
break;
case 104:
num=5;
break;
case 104:
num=6;
break;
case 104:
num=7;
break;
case 104:
num=8;
break;
case 104:
num=9;
break;
default:
num=-1;
}
return num;
}*/