This is something I'm struggling with for a while and fail to find solid solution. The Arduino IR library provides method for recording arbitrary IR code and re-sending it. This gave me different results every time I recorded the IR remote, so I wrote a little code, based on interrupts, that try to record the IR rise/fall as accurately as possible. I am working with ATTiny85 running at 8Mhz on internal oscillator.
Here is the code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(4,3);
const int dataSize = 40;
int data[dataSize];
int dataLength[dataSize];
void setup() {
pinMode(0, OUTPUT); // 0 is IR receiver pin
mySerial.begin(9600);
mySerial.println("Start");
attachInterrupt(0,IR_ISR,CHANGE);
mySerial.println("Registered");
sei();
}
volatile unsigned long prev = micros();
volatile unsigned long curr = -1;
volatile unsigned short index = 0;
volatile bool rising = true;
void IR_ISR()
{
if (!rising)
{
dataLength[index] = micros() - curr;
rising = !rising;
return;
}
curr = micros();
data[index++] = (int)curr-prev;
prev = curr;
rising = !rising;
}
void loop() {
delay(5000);
for (int i=1; i<dataSize; i++)
{
mySerial.print(data[i]);
mySerial.print("(");
mySerial.print(dataLength[i]);
mySerial.print(")");
mySerial.print("; ");
}
mySerial.println();
index = 0;
}
Problem is that the milliseconds alter on each record of the remote control, here is an example of the output of 3 attempts of clicking at the same button of the remote control (I pasted only the first 5 IR modulations to avoid too much text, total is about 39):
Start
Registered14104(9496); 1184(672); 1184(616); 1160(656); 1176(672); ...
14096(9480); 1184(664); 1208(568); 1136(616); 1184(664); ...
14096(9384); 1216(576); 1136(616); 1184(664); 1208(560); ...
The format is: [microseconds from previous falling edge](microseconds from rise to fall);
As you can see, the results vary. According to the arduino.cc website, the micros() function has a 8 microseconds resolution on 8Mhz AVR's, but the difference between reads is bigger than that.
This is not reliable enough for me to solidly duplicate a remote control, and I'm looking for a resolution that will provide accurate results, as the magnitude of every deviation increases when the voltages are low (for example, with a 3v 2032 coin battery half discharged).