|
本帖最后由 peckbyte 于 2015-1-31 12:19 编辑
买了2对315m超再生模块,一直都找不到好的库来编码解码,后来在网上看到一位大侠提示用红外线arduino的类库可以操作315m,于是花了些时间研究了一下,发现可以完美的使用在315m超再生模块。
IRremote库主要的改动主要有一下几处:
1.删除了其他的编码解码模式,只保留了常见的NEC模式,这也是大多数家电红外线遥控器最常见的模式;
2.在IRremoteInt.h文件中改动:#define TOLERANCE 50 // percent tolerance in measurements <<---增加了容错率
3.关键改动,多次试验发现解码不成功,发现红外线发射时,arduino接收的数据BUFFER[0]会有额外的一个数据,所以解码程序是从buffer[1]处开始分析数据,而315模块没有额外的这个数据,所以我们要改动解码程序,buffer[0]开始解码。
实验用例子:发送程序(注意,库文件我改了个名字:IRremote315) - #include <IRremote315.h>
- #include <IRremoteInt315.h>
- IRsend irsend; // 定义 IRsend 物件来发射红外线讯号
-
- void setup()
- {
- pinMode(3, OUTPUT);
- }
-
- void loop()
- {
- Serial.print("SendIR: ");
- irsend.sendNEC(0x765585, 32); // 记得换成你遥控器的红外线编码
- delay(3000); // 等待3秒
- }
复制代码
接收程序: - #include <IRremote315.h>
- #include <IRremoteInt315.h>
- int RECV_PIN = 11;
- int LED1 = 2;
- long on1 = 0x00FF906F;
- int sta=1;
- IRrecv irrecv(RECV_PIN);
- decode_results results;
- // Dumps out the decode_results structure.
- // Call this after IRrecv::decode()
- // void * to work around compiler issue
- //void dump(void *v) {
- // decode_results *results = (decode_results *)v
- void dump(decode_results *results) {
- int count = results->rawlen;
- if (results->decode_type == UNKNOWN)
- {
- Serial.println("Could not decode message");
- }
- else
- {
- if (results->decode_type == NEC)
- {
- Serial.print("Decoded NEC: ");
- }
-
- Serial.print(results->value, HEX);
- Serial.print(" (");
- Serial.print(results->bits, DEC);
- Serial.println(" bits)");
- }
- Serial.print("Raw (");
- Serial.print(count, DEC);
- Serial.print("): ");
- for (int i = 0; i < count; i++)
- {
- if ((i % 2) == 1) {
- Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
- }
- else
- {
- Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
- }
- Serial.print(" ");
- }
- Serial.println("");
- }
- void setup()
- {
- pinMode(RECV_PIN, INPUT);
- pinMode(LED1,OUTPUT);
- pinMode(13, OUTPUT);
- Serial.begin(9600);
-
- irrecv.enableIRIn(); // Start the receiver
- }
- int on = 0;
- unsigned long last = millis();
- void loop()
- {
- if (irrecv.decode(&results))
- {
- // If it's been at least 1/4 second since the last
- // IR received, toggle the relay
- if (millis() - last > 250)
- {
- on = !on;
- // digitalWrite(8, on ? HIGH : LOW);
- digitalWrite(13, on ? HIGH : LOW);
- dump(&results);
- }
- if (results.value == on1 )
- {
- if(sta==1)
- {digitalWrite(LED1, HIGH);
- sta=0;
- }
- else
- {digitalWrite(LED1, LOW);
- sta=1;
- }
-
- }
- last = millis();
- irrecv.resume(); // Receive the next value
- }
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|