由于美的空调使用的不是简单的红外编码信号,因此在使用arduino红外库的时候不能直接调用其已有的编码规则,需要先了解美的空调的编码规则,再使用IRsend::sendRaw这个函数直接发送原始数据。本文建立在已经对红外发送有了了解并且搜到了美的空调的编码规则的基础上(编码规则在百度即可搜到https://wenku.baidu.com/view/c46594141ed9ad51f01df2c3.html)
一、编码规则
根据美的的编码规则,L A A' B B' C C' S L A A' B B' C C' O为最普通的开机码,其中L为引导码,A、B、C为决定温度,风速,模式的码,A',B',C'分别为A,B,C按位取反后的码,S为分隔码,O为终止码(均可在上面的链接上查到)
关机码与开机码稍有不同,如果想直接关机,可以直接复制下面的数组buf[300]
{4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,5220,4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,5220,4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,8000};
二、解码函数
如果要把每一个码都直接存起来就有点浪费内存了,其实可变的码也就A,B,C这三个字节,下面的函数实现了输入三个字节A,B,C,就把全局数组buf[200]用持续时间填满,之后直接使用sendRaw(buf,200,38)就可以开机并设置温度,风速,模式了。
void getcode(unsigned char A,unsigned char B,unsigned char C){unsigned char mask=0x80;unsigned int cnt=2;buf[0]=4400;buf[1]=4400;//发Afor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & A)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~A))buf[cnt++]=1660;else buf[cnt++]=550;}//发Bfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & B)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~B))buf[cnt++]=1660;else buf[cnt++]=550;}//发Cfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & C)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~C))buf[cnt++]=1660;else buf[cnt++]=550;}buf[cnt++]=550;buf[cnt++]=5220;buf[cnt++]=4400;buf[cnt++]=4400;//发Afor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & A)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~A))buf[cnt++]=1660;else buf[cnt++]=550;}//发Bfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & B)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~B))buf[cnt++]=1660;else buf[cnt++]=550;}//发Cfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & C)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~C))buf[cnt++]=1660;else buf[cnt++]=550;}buf[cnt++]=550;buf[cnt++]=8800;}
三、完整代码
需要的硬件电路:
- 一个按钮,一端接arduino的2号IO口,一端接GND;
- 一个红外发射头,串上限流电阻,一端接arduino3号IO口,一端接GND
功能:
- 按钮每次按下,轮流发射开机码(对应某个温度、风速)与关机码
#include <IRremote.h>unsigned char flag=1;
const unsigned char LED = 13;
const unsigned char Btn =2;
const unsigned char IR = 3;
unsigned int buf[]={4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,550,550,1660,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,5220,4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,550,550,1660,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,8000};IRsend irsend;
unsigned char state=LOW;
void setup() {// put your setup code here, to run once:pinMode(jieshou, INPUT);pinMode(LED, OUTPUT);pinMode(Btn,INPUT);Serial.begin(9600);pinMode(IR, OUTPUT);
}void loop() {if(digitalRead(Btn)==HIGH && state==LOW){//消抖delay(15);if(digitalRead(Btn)==HIGH){state=HIGH;if(flag & 1){sendMeidi_OPEN(0xB2,0x5F,0xD0); digitalWrite(LED,HIGH);flag++;//Serial.println(flag);}else {sendMeidi_CLOSE();digitalWrite(LED,LOW);flag++;//Serial.println(flag);} } } if(digitalRead(Btn)==LOW && state==HIGH){//消抖delay(15);if(digitalRead(Btn)==LOW){state=LOW;}}
}void sendMeidi_OPEN(unsigned char A,unsigned char B,unsigned char C){getcode(A,B,C); irsend.sendRaw(buf,200,38);
}
void getcode(unsigned char A,unsigned char B,unsigned char C){unsigned char mask=0x80;unsigned int cnt=2;buf[0]=4400;buf[1]=4400;//发Afor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & A)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~A))buf[cnt++]=1660;else buf[cnt++]=550;}//发Bfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & B)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~B))buf[cnt++]=1660;else buf[cnt++]=550;}//发Cfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & C)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~C))buf[cnt++]=1660;else buf[cnt++]=550;}buf[cnt++]=550;buf[cnt++]=5220;buf[cnt++]=4400;buf[cnt++]=4400;//发Afor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & A)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~A))buf[cnt++]=1660;else buf[cnt++]=550;}//发Bfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & B)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~B))buf[cnt++]=1660;else buf[cnt++]=550;}//发Cfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & C)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~C))buf[cnt++]=1660;else buf[cnt++]=550;}buf[cnt++]=550;buf[cnt++]=8800;Serial.println(cnt);
}
void sendMeidi_CLOSE(){unsigned int buf_close[]={4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,5220,4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,5220,4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,8000};irsend.sendRaw(buf_close,300,38);
}