Skip to content

Instantly share code, notes, and snippets.

@xspager
Last active February 9, 2018 03:06
Show Gist options
  • Save xspager/8e7e2af84ba0cc9c79762805ff96d97d to your computer and use it in GitHub Desktop.
Save xspager/8e7e2af84ba0cc9c79762805ff96d97d to your computer and use it in GitHub Desktop.
Arduino iRemote example for sending remote control commands to a LG Home Theater from the serial port
/*
* IRremote: IRsendDemo - demonstrates sending IR codes with IRsend
* An IR LED must be connected to Arduino PWM pin 3.
* Version 0.1 July, 2009
* Copyright 2009 Ken Shirriff
* http://arcfn.com
*/
#include <IRremote.h>
IRsend irsend;
// from https://github.com/z3t0/Arduino-IRremote/issues/263#issuecomment-274958604
/* ------------------------------------ */
// Reverse the order of bits in a byte
// Example: 01000000 -> 00000010
unsigned char ReverseByte(unsigned char b)
{
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return(b);
}
// Calculate 32 bit NECx code
unsigned long GenNECXCode(unsigned char p_Device, unsigned char p_SubDevice, unsigned char p_Function)
{
unsigned long ReverseDevice = (unsigned long)ReverseByte(p_Device);
unsigned long ReverseSubDevice = (unsigned long)ReverseByte(p_SubDevice);
unsigned long ReverseFunction = (unsigned long)ReverseByte(p_Function);
return((ReverseDevice << 24) | (ReverseSubDevice << 16) | (ReverseFunction << 8) | ((~ReverseFunction) & 0xFF));
}
/* ------------------------------------ */
int LG_BITS = 32;
#define LG_POWER_ON GenNECXCode(45,45,48)
#define LG_AV GenNECXCode(45,45,176)
#define LG_MODE_DVD GenNECXCode(45,45,241)
#define LG_POWER GenNECXCode(44,44,30)
void setup()
{
Serial.begin(9600);
}
void loop() {
/* for (int i = 0; i < 3; i++) {
irsend.sendLG(LG_POWER, LG_BITS);
delay(40);
}
*/
if(Serial.available() > 0) {
Serial.println("LG IR code:");
String str = Serial.readStringUntil('\n');
unsigned long command = GenNECXCode(44,44, str.toInt());
for (int i = 0; i < 3; i++) {
irsend.sendLG(command, LG_BITS);
delay(40);
}
}
delay(40); //5 second delay between each signal burst
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment