Skip to content

Instantly share code, notes, and snippets.

@taqpan
Created July 12, 2014 17:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taqpan/e84bcc72ef9b81ddb6fc to your computer and use it in GitHub Desktop.
Save taqpan/e84bcc72ef9b81ddb6fc to your computer and use it in GitHub Desktop.
IR-signal sender for Arduino
#define PIN_IR_OUT 4
#define PIN_LED_OUT 2
#define SIGNAL_LEN 7
// AEHA format
#define SIGNAL_LEADER_HIGH_TIME 3200
#define SIGNAL_LEADER_LOW_TIME 1600
#define SIGNAL_ON_HIGH_TIME 400
#define SIGNAL_ON_LOW_TIME 1200
#define SIGNAL_OFF_HIGH_TIME 400
#define SIGNAL_OFF_LOW_TIME 400
#define SIGNAL_TRAILER_HIGH_TIME 400
#define SIGNAL_TRAILER_LOW_TIME 8800
#define SIGNAL_LEN 7
typedef struct _SignalMap {
char chr;
byte signal[SIGNAL_LEN];
} SignalMap;
SignalMap signalMap[] = {
// power-on, cooling, auto air vol, 28 deg C
{'C', { 0x28, 0x61, 0x3d, 0x10, 0xef, 0xbc, 0x43 } },
// power-off
{'c', { 0x28, 0x61, 0x3d, 0x10, 0xef, 0xac, 0x53 } },
// terminator
{'\0', { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } }
};
// 38Khz sending
void send_carrier_frequency(unsigned long high_length, unsigned long low_length)
{
unsigned long end_time = micros() + high_length;
while (end_time > micros()) {
digitalWrite(PIN_IR_OUT, HIGH);
delayMicroseconds(9);
digitalWrite(PIN_IR_OUT, LOW);
delayMicroseconds(6);
}
delayMicroseconds(low_length);
}
void send_signal(byte* data) {
// leader
send_carrier_frequency(SIGNAL_LEADER_HIGH_TIME, SIGNAL_LEADER_LOW_TIME);
// real data
for (int i = 0; i < SIGNAL_LEN; i++) {
byte pattern = data[i];
for (int j = 0; j < 8; j++) {
if ((pattern >> j) & 0x1 == 0x1) {
send_carrier_frequency(SIGNAL_ON_HIGH_TIME, SIGNAL_ON_LOW_TIME);
} else {
send_carrier_frequency(SIGNAL_OFF_HIGH_TIME, SIGNAL_OFF_LOW_TIME);
}
}
}
// trailer
send_carrier_frequency(SIGNAL_TRAILER_HIGH_TIME, SIGNAL_TRAILER_LOW_TIME);
}
void setup()
{
Serial.begin(9600);
pinMode(PIN_IR_OUT, OUTPUT);
pinMode(PIN_LED_OUT, OUTPUT);
Serial.print("setup\n");
}
void loop()
{
digitalWrite(PIN_IR_OUT, LOW);
digitalWrite(PIN_LED_OUT, LOW);
if (Serial.available()) {
int c = Serial.read();
for (int i = 0; i < signalMap[i].chr != '\0'; i++) {
if (signalMap[i].chr == c) {
digitalWrite(PIN_LED_OUT, HIGH);
send_signal(signalMap[i].signal);
digitalWrite(PIN_LED_OUT, LOW);
Serial.print("OK\n");
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment