Arduino IR Remote 程式-2
1) 利用上一篇的 Arduino IR Remote 程式抓取遙控器的10進制接收內碼
results value is HEX=807F00FF DEC= 2155806975, bits is 32, decode_type is 1
---Key "0"
results value is HEX=FFFFFFFF DEC= 4294967295, bits is 0, decode_type is 1
---Key release
results value is HEX=807F906F DEC= 2155843695, bits is 32, decode_type is 1
---Key "1"
results value is HEX=FFFFFFFF DEC= 4294967295, bits is 0, decode_type is 1
---Key release
2) 控制 "0" , "1" 設定輸出 ON/OFF
#include <IRremote.h>
//I2C + I2C LCD Library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
/*==========================================
SDA – 接 Arduino 的 Analog Pin 4 (Arduino Mega 為 Pin 20)
SCL – 接 Arduino 的 Analog Pin 5 (Arduino Mega 為 Pin 21)
GND – 接 GND
VCC – 接 +5V
==========================================*/
int RECV_PIN = 2; // 使用數位腳位2接收紅外線訊號
int RECV_OUT = 12; // 使用數位腳位12 控制ON/OFF
IRrecv irrecv(RECV_PIN); // 初始化紅外線訊號輸入
decode_results results; // 儲存訊號的結構
void setup()
{
Serial.begin(9600);
irrecv.blink13(true); // 設為true的話,當收到訊號時,腳位13的LED便會閃爍
irrecv.enableIRIn(); // 啟動接收
pinMode(RECV_OUT, OUTPUT); // sets the digital pin as output
digitalWrite(RECV_OUT, HIGH); //Set RECV_OUT HIGH = OFF LOW=ON
//**************************************
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight(); // 輸出初始化文字
lcd.setCursor(0, 0); // 設定游標位置在第1行行首
lcd.print("IR Remote Contro");
}
void loop() {
if (irrecv.decode(&results)) { // 接收紅外線訊號並解碼
Serial.print("results value is HEX="); // 輸出解碼後的資料
Serial.print(results.value, HEX);
Serial.print(" DEC= ");
Serial.print(results.value);
Serial.print(", bits is ");
Serial.print(results.bits);
Serial.print(", decode_type is ");
Serial.println(results.decode_type);
irrecv.resume(); // 準備接收下一個訊號
if (results.value==2155806975) { // KEY "1" HEX=807F00FF DEC= 2155806975
digitalWrite(RECV_OUT, LOW); //Set RECV_OUT
lcd.setCursor(0, 1); // 設定游標位置在第2行行首
lcd.print("IR SET ON ");
}
if (results.value==2155843695){ //KEY "0" HEX=807F906F DEC= 2155843695
digitalWrite(RECV_OUT, HIGH); //Set RECV_OUT
lcd.setCursor(0, 1); // 設定游標位置在第2行行首
lcd.print("IR SET OFF ");
}
}
}
results value is HEX=807F00FF DEC= 2155806975, bits is 32, decode_type is 1
---Key "0"
results value is HEX=FFFFFFFF DEC= 4294967295, bits is 0, decode_type is 1
---Key release
results value is HEX=807F906F DEC= 2155843695, bits is 32, decode_type is 1
---Key "1"
results value is HEX=FFFFFFFF DEC= 4294967295, bits is 0, decode_type is 1
---Key release
#include <IRremote.h>
//I2C + I2C LCD Library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
/*==========================================
SDA – 接 Arduino 的 Analog Pin 4 (Arduino Mega 為 Pin 20)
SCL – 接 Arduino 的 Analog Pin 5 (Arduino Mega 為 Pin 21)
GND – 接 GND
VCC – 接 +5V
==========================================*/
int RECV_PIN = 2; // 使用數位腳位2接收紅外線訊號
int RECV_OUT = 12; // 使用數位腳位12 控制ON/OFF
IRrecv irrecv(RECV_PIN); // 初始化紅外線訊號輸入
decode_results results; // 儲存訊號的結構
void setup()
{
Serial.begin(9600);
irrecv.blink13(true); // 設為true的話,當收到訊號時,腳位13的LED便會閃爍
irrecv.enableIRIn(); // 啟動接收
pinMode(RECV_OUT, OUTPUT); // sets the digital pin as output
digitalWrite(RECV_OUT, HIGH); //Set RECV_OUT HIGH = OFF LOW=ON
//**************************************
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight(); // 輸出初始化文字
lcd.setCursor(0, 0); // 設定游標位置在第1行行首
lcd.print("IR Remote Contro");
}
void loop() {
if (irrecv.decode(&results)) { // 接收紅外線訊號並解碼
Serial.print("results value is HEX="); // 輸出解碼後的資料
Serial.print(results.value, HEX);
Serial.print(" DEC= ");
Serial.print(results.value);
Serial.print(", bits is ");
Serial.print(results.bits);
Serial.print(", decode_type is ");
Serial.println(results.decode_type);
irrecv.resume(); // 準備接收下一個訊號
if (results.value==2155806975) { // KEY "1" HEX=807F00FF DEC= 2155806975
digitalWrite(RECV_OUT, LOW); //Set RECV_OUT
lcd.setCursor(0, 1); // 設定游標位置在第2行行首
lcd.print("IR SET ON ");
}
if (results.value==2155843695){ //KEY "0" HEX=807F906F DEC= 2155843695
digitalWrite(RECV_OUT, HIGH); //Set RECV_OUT
lcd.setCursor(0, 1); // 設定游標位置在第2行行首
lcd.print("IR SET OFF ");
}
}
}
沒有留言:
張貼留言