I am upgrading a dumb device to a smart device but unfortunately the device in question does not have any physical buttons, only an infrared remote, therefore I am attempting to use an Arduino to bit bang the raw NEC output of the IR codes directly but I am a bit lost in the actual transmission.
I have a digital PWM pin connected directly to the signal line where the IR receiver was desoldered from, and the following function to send the command, but when i send the hex code 0xB34C4BBA
, I receive 0x43B2AC74
. Is anyone able to guide me in where i went wrong?
void simulateIRSignal(unsigned long code) {
const unsigned int headerMark = 9000; // Header mark duration in microseconds
const unsigned int headerSpace = 4500; // Header space duration in microseconds
const unsigned int bitMark = 562; // Bit mark duration in microseconds
const unsigned int oneSpace = 1687; // Space duration for a logical 1 in microseconds
const unsigned int zeroSpace = 562; // Space duration for a logical 0 in microseconds
const unsigned int gap = 562; // Gap duration between transmissions in microseconds
// Send the header mark and space
digitalWrite(IR_LINE, HIGH);
delayMicroseconds(headerMark);
digitalWrite(IR_LINE, LOW);
delayMicroseconds(headerSpace);
// Send the 32 bits of the code
for (int i = 0; i < 32; i++) {
// Get the current bit value
unsigned long bitValue = (code & (1UL << i)) != 0;
// Send the bit mark
digitalWrite(IR_LINE, HIGH);
delayMicroseconds(bitMark);
digitalWrite(IR_LINE, LOW);
// Send the corresponding space
if (bitValue) {
delayMicroseconds(oneSpace); // Logical 1 space
} else {
delayMicroseconds(zeroSpace); // Logical 0 space
}
}
// Send the trailing gap
digitalWrite(IR_LINE, LOW);
delayMicroseconds(gap);
}
To follow along, take two Arduinos, flash one with the RecieveDump example sketch for the IRremote Library, and connect pin 2 of that Arduino to a PWM pin on the other. Upload this function to the second Arduino, and send an IR code using the above code. Sending an IR code through this function will result in a dump on the console of the second Arduino. Don't forget to connect the two grounds as I had done at first.