[Programming code]
/*
* IRrecord: record and play back IR signals as a minimal
* An IR detector/demodulator must be connected to the input RECV_PIN.
* An IR LED must be connected to the output PWM pin 3.
* A button must be connected to the input BUTTON_PIN; this is the
* send button.
* A visible LED can be connected to STATUS_PIN to provide status.
*
* The logic is:
* If the button is pressed, send the IR code.
* If an IR code is received, record it.
*
* Version 0.11 September, 2009
* Copyright 2009 Ken Shirriff
* http://arcfn.com
*
*
* Version 0.2 October, 2015
* Modified to remove button function
* This module acts a IR Repeater to just
* "Read the IR Code " and "Send the IR Code" immediately
* By En-Lin Chen / Stonez56
*
*/
#include <IRremote.h>
int RECV_PIN = 2;
int STATUS_PIN = 13;
IRrecv irrecv(RECV_PIN);
IRsend irsend;
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(STATUS_PIN, OUTPUT);
}
// Storage for the recorded code
int codeType = -1; // The type of code
unsigned long codeValue; // The code value if not raw
unsigned int rawCodes[RAWBUF]; // The durations if raw
int codeLen; // The length of the code
int toggle = 0; // The RC5/6 toggle state
// Stores the code for later playback
// Most of this code is just logging
void storeCode(decode_results *results) {
codeType = results->decode_type;
int count = results->rawlen;
if (codeType == UNKNOWN) {
Serial.println("Received unknown code, saving as raw");
codeLen = results->rawlen - 1;
// To store raw codes:
// Drop first value (gap)
// Convert from ticks to microseconds
// Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
for (int i = 1; i <= codeLen; i++) {
if (i % 2) {
// Mark
rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
Serial.print(" m");
}
else {
// Space
rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
Serial.print(" s");
}
Serial.print(rawCodes[i - 1], DEC);
}
Serial.println("");
}
else {
if (codeType == NEC) {
Serial.print("Received NEC: ");
}
else if (codeType == SONY) {
Serial.print("Received SONY: ");
}
else if (codeType == RC5) {
Serial.print("Received RC5: ");
}
else if (codeType == RC6) {
Serial.print("Received RC6: ");
}
else {
Serial.print("Unexpected codeType ");
Serial.print(codeType, DEC);
Serial.println("");
}
Serial.println(results->value, HEX);
codeValue = results->value;
codeLen = results->bits;
}
}
void sendCode(int repeat) {
if(codeValue == 0xFFFFFFFF) return; //ignore FFFFFFF IR code
if (codeType == NEC) {
irsend.sendNEC(codeValue, codeLen);
Serial.print("Sent NEC ");
Serial.println(codeValue, HEX);
}
else if (codeType == SONY) {
irsend.sendSony(codeValue, codeLen);
Serial.print("Sent Sony ");
Serial.println(codeValue, HEX);
}
else if (codeType == RC5 || codeType == RC6) {
if (!repeat) {
// Flip the toggle bit for a new button press
toggle = 1 - toggle;
}
// Put the toggle bit into the code to send
codeValue = codeValue & ~(1 << (codeLen - 1));
codeValue = codeValue | (toggle << (codeLen - 1));
if (codeType == RC5) {
Serial.print("Sent RC5 ");
Serial.println(codeValue, HEX);
irsend.sendRC5(codeValue, codeLen);
}
else {
irsend.sendRC6(codeValue, codeLen);
Serial.print("Sent RC6 ");
Serial.println(codeValue, HEX);
}
}
else if (codeType == UNKNOWN /* i.e. raw */) {
// Assume 38 KHz
irsend.sendRaw(rawCodes, codeLen, 38);
Serial.println("Sent raw");
}
}
void loop() {
if (irrecv.decode(&results)) {
/* Receive IR code */
Serial.println("Pressed!");
digitalWrite(STATUS_PIN, HIGH);
storeCode(&results);
irrecv.resume(); // resume receiver
digitalWrite(STATUS_PIN, LOW);
delay(50);
/* Send IR code */
Serial.println("Sending!");
digitalWrite(STATUS_PIN, HIGH);
sendCode(true);
digitalWrite(STATUS_PIN, LOW);
delay(50); // Wait a bit between retransmissions
irrecv.enableIRIn(); // Re-start the receiver
Serial.println("--------------------------------");
}
}
After programming code is copied into Arduino IDE, upload it to your Arduino Pro Mini.
Here shows you the IR command received through the serial port monitor.
First, solder IR emitter LED to the extended long wires. Although not showing at the photo below, an 100 Omh resistor is needed on the data pin 2.
Follow IR Repeater Setup Illustration in Tutorial Part 1
- Place IR BOX next to your TV and plug the USB adapter
- Pull the IR emitter with the wire through the wall into the living room
- Make sure the IR emitter is aiming towards the IR receiver on the Set Top Box
- I glue the IR emitter LED next to my couch and it working fine
- Done
END.
==================================================
[程式碼請參考英文版內文]
打開 Serial Monitor 的方式: Arduino -> Tools->Serial Monitor 參考下圖:
(IR 接收解碼為:FFA252D 搖控器類型 NEC)
這個模組需要一個 5V 的電源長期供電才能運作,所以我找來一個手機的 5V 充電器來供電。
依照教學的 Part 1 紅外線轉發器安裝方法圖示:
- 把 IR BOX 放在房間適當位置固定並插入電源
- 把長長的電源線和 IR 發射LED 穿過牆壁,
- 確認IR 發射LED 拉到能正對著機上盒的紅外接收位置 (Set Top Box)
- 我用膠槍把 IR LED 固定在沙發旁邊。
- 完成
如果你也想要自己 DIY 一個話,可以自己開始動手了!!
全文完.



















Hi Nikhil B,
I just found the include function does not carry the remote library name.
Please replace:
#include
with
#include <IRremote.h>
Interesting idea! However, most of tablets do not have IR built-in.
How about check the most recent IR Rremote.h library to see if that could help.
https://github.com/z3t0/Arduino-IRremote