Собственно код программы
#include <IRremote.h>
#include <IRremoteInt.h>
/*
* Default is Arduino pin D11.
* You can change this to another available Arduino Pin.
* Your IR receiver should be connected to the pin defined here
*/
int RECV_PIN = 2;
volatile unsigned long in_signal = 0;
IRrecv irrecv(RECV_PIN);
decode_results results;
IRsend irsend;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
irsend.enableIROut(38);
irrecv.enableIRIn(); // Start the receiver
}
void dump(decode_results *results) {
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
int count = results->rawlen;
if (results->decode_type == UNKNOWN) {
Serial.print("Unknown encoding: ");
}
else if (results->decode_type == NEC) {
Serial.print("Decoded NEC: ");
}
else if (results->decode_type == SONY) {
Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5) {
Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6) {
Serial.print("Decoded RC6: ");
}
else if (results->decode_type == PANASONIC) {
Serial.print("Decoded PANASONIC - Address: ");
Serial.print(results->address, HEX);
Serial.print(" Value: ");
}
else if (results->decode_type == LG) {
Serial.print("Decoded LG: ");
}
else if (results->decode_type == JVC) {
Serial.print("Decoded JVC: ");
}
else if (results->decode_type == AIWA_RC_T501) {
Serial.print("Decoded AIWA RC T501: ");
}
else if (results->decode_type == WHYNTER) {
Serial.print("Decoded Whynter: ");
}
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 = 1; i < count; i++) {
if (i & 1) {
Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
}
else {
Serial.write('-');
Serial.print((unsigned long) results->rawbuf[i]*USECPERTICK, DEC);
}
Serial.print(" ");
}
Serial.println();
}
void loop() {
decode_results results;
if (irrecv.decode(&results)) {
in_signal=results.value;
Serial.println(results.value, HEX);
dump(&results);
if (in_signal==0xE0E006F9){ //кнопка вверх
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
irsend.sendNEC(0x807F22DD, 32);
delay(200);
digitalWrite(13, LOW); // turn the LED on (HIGH is the voltage level)
in_signal=0;
irrecv.enableIRIn();
};
if (in_signal==0xE0E08679){ //кнопка вниз
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
irsend.sendNEC(0x807F12ED, 32);
delay(200);
digitalWrite(13, LOW); // turn the LED on (HIGH is the voltage level)
in_signal=0;
irrecv.enableIRIn();
};
if (in_signal==0xE0E046B9){ //кнопка вправо
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
irsend.sendNEC(0x807F02FD, 32);
delay(200);
digitalWrite(13, LOW); // turn the LED on (HIGH is the voltage level)
in_signal=0;
irrecv.enableIRIn();
};
if (in_signal==0xE0E0A659){ //кнопка влево
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
irsend.sendNEC(0x807F32CD, 32);
delay(200);
digitalWrite(13, LOW); // turn the LED on (HIGH is the voltage level)
in_signal=0;
irrecv.enableIRIn();
};
if (in_signal==0xE0E016E9){ //кнопка центр
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
irsend.sendNEC(0x807F609F, 32);
delay(200);
digitalWrite(13, LOW); // turn the LED on (HIGH is the voltage level)
in_signal=0;
irrecv.enableIRIn();
};
if (in_signal==0xE0E0B44B){ //кнопка ексит
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
irsend.sendNEC(0x807FC03F, 32);
delay(200);
digitalWrite(13, LOW); // turn the LED on (HIGH is the voltage level)
in_signal=0;
irrecv.enableIRIn();
};
irrecv.resume();
}
}
Добавлено в 19:05 Особенности:
1. Для отладки оставлена посылка в монитор порта принятых кодов пульта
2. Зелёный светодиод 13 вывода загорается при отсылке ИК-команды и тухнет через 0.2 сек.
3. Зелёный светодиод TX(rx?) мигает при приёме ИК-команд (само получилось)
4. После команды irsend НЕОБХОДИМО вызвать команду irrecv.enableIRln(); иначе приёмник перестаёт работать.
5. Приёмник можно садить на разные ноги ардуины (кажется, не на любые), передатчик - заточен в библиотеке на 3 вывод.
6. Ещё один вариант использования устройства - считать коды с полудохлого (изношены резинки, разбит корпус, просто неудобный) пульта и использовать для управления новый удобный пульт с другой системой команд (но с достаточным числом кнопок)
Заметная задержка исполнения команд меня не напрягает, но можно уменьшить (сейчас 0.2 сек)