Skip to content

Instantly share code, notes, and snippets.

@JorgenEvens
Last active November 21, 2015 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JorgenEvens/ae04055faabe8cf3f627 to your computer and use it in GitHub Desktop.
Save JorgenEvens/ae04055faabe8cf3f627 to your computer and use it in GitHub Desktop.
Code to read IR codes from a remote using 1838B sensor
#include <Arduino.h>
// Enable debug printing of signal
// #define DEBUG_SIGNAL
// Enable debug for HEX codes
// #define DEBUG_CODES
// Only store time that signal was up.
// #define STORE_DOWN
#define MAX_ANALOG (uint8_t)(-1)
#define MAX_WAIT 50 * 1000
#define MAX_LOOKBACK 100
#define STEP_DIM 32
// PIN configuration
#define REDPin 9
#define BLUEPin 10
#define GREENPin 11
#define IRPin 13
// IR Code mapping to remote
#define KEY_1 0xFF30CF
#define KEY_2 0xFF18E7
#define KEY_4 0xFF10EF
#define KEY_5 0xFF38C7
#define KEY_7 0xFF42BD
#define KEY_8 0xFF4AB5
#if MAX_LOOKBACK > 255
typedef uint16_t position_t;
#else
typedef uint8_t position_t;
#endif
#ifdef STORE_DOWN
#define DURATION(data, i) data[i].duration
class Reading;
typedef Reading reading_t;
class Reading {
public:
Reading() : up(true), duration(0) {};
bool up;
uint8_t duration;
};
#else
#define DURATION(data, i) data[i]
typedef uint8_t reading_t;
#endif
void reinit(reading_t* data, position_t dataLength);
uint32_t getCode(reading_t* data);
void printData(reading_t* data);
void handleCode(uint32_t code);
position_t pinPosition = 0;
uint8_t red = 127;
uint8_t green = 127;
uint8_t blue = 127;
void reinit(reading_t* data, position_t dataLength) {
pinPosition = 0;
#ifdef STORE_DOWN
data[pinPosition].up = true;
#endif
DURATION(data, pinPosition) = 0;
}
#ifdef DEBUG_SIGNAL
void printSeq(reading_t* data);
void printSeq(reading_t* data) {
for( position_t i = 0; i<pinPosition; i++ ) {
Serial.print(i);
Serial.print(". ");
#ifdef STORE_DOWN
Serial.print(data[i].up ? "ON" : "OFF");
Serial.print(" for ");
#endif
Serial.print(DURATION(data, i));
Serial.println("usec");
}
}
#endif
uint32_t getCode(reading_t* data) {
uint32_t out = 0;
for( position_t i=0; i<pinPosition; i++ ) {
#ifdef STORE_DOWN
if( !data[i].up ) continue;
#endif
out = ( out << 1 );
if( DURATION(data, i) > 10 )
out += 0x1;
}
return out;
}
uint32_t readIR() {
position_t dataLength = MAX_LOOKBACK;
unsigned long lastData = micros();
reading_t data[dataLength];
reinit(data, dataLength);
#ifndef STORE_DOWN
bool up = true;
#endif
while(true) {
bool isHigh = ( digitalRead(IRPin) == HIGH );
unsigned long uTime = micros();
uint16_t diff = uTime - lastData;
if( diff > MAX_WAIT || pinPosition+1 == dataLength ) {
if( pinPosition < 8 || ( pinPosition % 8 != 0 ) )
return 0;
#ifdef DEBUG_CODES
Serial.print("Code: 0x");
Serial.println(getCode(data), HEX);
#endif
#ifdef DEBUG_SIGNAL
printSeq(data);
#endif
return getCode(data);
}
#ifdef STORE_DOWN
if( isHigh != data[pinPosition].up ) {
lastData = uTime;
DURATION(data, pinPosition) = (uint8_t)(diff/100);
pinPosition++;
data[pinPosition].up = isHigh;
DURATION(data, pinPosition) = 0;
}
#else
if( isHigh != up ) {
lastData = uTime;
if( up && (diff/100) < 20 ) {
DURATION(data, pinPosition) = (uint8_t)(diff/100);
pinPosition++;
DURATION(data, pinPosition) = 0;
}
up = isHigh;
}
#endif
}
}
void handleCode(uint32_t code) {
uint8_t* clr = NULL;
uint8_t pin = 0;
int8_t change = -STEP_DIM;
// RED
if( code == KEY_1 || code == KEY_2 ) {
clr = &red;
pin = REDPin;
} else if( code == KEY_4 || code == KEY_5 ) {
clr = &green;
pin = GREENPin;
} else if( code == KEY_7 || code == KEY_8 ) {
clr = &blue;
pin = BLUEPin;
} else {
return;
}
if( code == KEY_2 || code == KEY_5 || code == KEY_8 )
change = ( *clr < MAX_ANALOG - STEP_DIM ) ? STEP_DIM : MAX_ANALOG - *clr;
else if(*clr < STEP_DIM)
change = -(*clr);
*clr += change;
analogWrite(pin, *clr);
Serial.print("R: ");
Serial.print(red);
Serial.print(", G: ");
Serial.print(green);
Serial.print(", B: ");
Serial.println(blue);
}
void setup() {
pinMode(IRPin, INPUT);
pinMode(REDPin, OUTPUT);
pinMode(GREENPin, OUTPUT);
pinMode(BLUEPin, OUTPUT);
analogWrite(REDPin, red);
analogWrite(GREENPin, green);
analogWrite(BLUEPin, blue);
Serial.begin(9600);
Serial.println("Ready");
}
void loop() {
handleCode(readIR());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment