红外学习遥控
Language | English |
---|
目的接收红外遥控器的发射编码,并保存起来,然后可以读取并发射相同红外信号。 原理利用红外接收传感器接收红外遥控器发射的编码,保存到EEPROM,然后可以通过读取EEPROM发射相同的红外信号。 设备
搭建硬件
软件调试
#define EEPROM_write(address, var) eeprom_write_block((const void *)&(var), (void*)(address),sizeof(var)) //EEPROM写入
#define EEPROM_read(address, var) eeprom_read_block((void*)&(var),(const void *)(address),sizeof(var)) //EEPROM读取
IRrecv irrecv(RECV_PIN); //红外接收
decode_results results; //编码读取结果
IRsend irsend; //红外发射
int RECV_PIN = 10; //红外接收引脚
int num = 0;
unsigned long readValue;
Serial.begin(9600);
irrecv.enableIRIn(); //启动红外接收
Serial.println("Please enter the num: 1.learn IR code 2.Send IR code");
while(!Serial.available());
num = Serial.read();
if(num == '1') //学习红外编码
{
Serial.println("please enter the learn num: 1~9");
while(!Serial.available());
num = Serial.read()-48;
if(num>=1&&num<=9)
{
Serial.println("please send the IR;");
while(!irrecv.decode(&results)); //识别红外编码
EEPROM_write(num,results.value); //将红外编码写入到EEPROM中
Serial.print("input IR code:");
Serial.println(results.value, HEX);
Serial.println("IR learning Done");
irrecv.resume(); // Receive the next value
}
else
{
Serial.println("invalid num");
}
}
if(num == '2')
{
Serial.println("please enter the send num: 1~9");
while(!Serial.available());
num = Serial.read()-48;
if(num>=1&&num<=9)
{
EEPROM_read(0,readValue); //从EEPROM中读取数据
irsend.sendSony(readValue, 32); //发射红外信号
Serial.print("output IR code:");
Serial.println(readValue, HEX);
Serial.println("IR sending Done");
}
else
{
Serial.println("invalid num");
}
}
程序结果可以接受和保存红外遥控器发射的红外编码,并发射出相同的红外信号。 视频 |