Skip to content

Instantly share code, notes, and snippets.

@GeorgeDewar
Created April 24, 2014 11:03
Show Gist options
  • Save GeorgeDewar/11250466 to your computer and use it in GitHub Desktop.
Save GeorgeDewar/11250466 to your computer and use it in GitHub Desktop.
/*
* 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
*/
#include <IRremote.h>
int RECV_PIN = 11;
int BUTTON_PIN = 12;
int STATUS_PIN = 13;
IRrecv irrecv(RECV_PIN);
IRsend irsend;
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, HIGH);
pinMode(STATUS_PIN, OUTPUT);
}
// Storage for the recorded code
unsigned char* data;
int nbits;
// Stores the code for later playback
// Most of this code is just logging
void storeCode(decode_results *results) {
if(results->decode_type != FUJITSU){
Serial.print("Received non-Fujitsu code");
return;
}
int count = results->rawlen;
Serial.print("Received code, raw length ");
Serial.print(count, DEC);
Serial.print(", ");
Serial.print(results->bits, DEC);
Serial.println(" bits");
// Print the array in binary
for(int i=0; i<DATA_SIZE; i++){
for (unsigned long mask = 0x80; mask; mask >>= 1) {
Serial.print(mask&results->data[i]?'1':'0');
}
Serial.print(" ");
}
Serial.println("");
int temp = (results->data[8] >> 4) + 16;
int mode = (results->data[9] & 0x0F);
int fan = (results->data[10] & 0x0F);
Serial.print("Temp: ");
Serial.println(temp, DEC);
Serial.print("Mode: ");
Serial.println(mode, DEC);
Serial.print("Fan: ");
Serial.println(fan, DEC);
data = results->data;
nbits = results->bits;
}
void sendCode() {
irsend.sendFujitsu(data, nbits);
Serial.println("Sent!");
}
int lastButtonState;
void loop() {
// If button pressed, send the code.
int buttonState = !digitalRead(BUTTON_PIN);
if (lastButtonState == HIGH && buttonState == LOW) {
Serial.println("Released");
irrecv.enableIRIn(); // Re-enable receiver
}
if (buttonState) {
Serial.println("Pressed, sending");
digitalWrite(STATUS_PIN, HIGH);
sendCode();
digitalWrite(STATUS_PIN, LOW);
delay(50); // Wait a bit between retransmissions
}
else if (irrecv.decode(&results)) {
digitalWrite(STATUS_PIN, HIGH);
storeCode(&results);
irrecv.resume(); // resume receiver
digitalWrite(STATUS_PIN, LOW);
}
lastButtonState = buttonState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment