Skip to content

Instantly share code, notes, and snippets.

@angp
Forked from splawski/Digispark_IR_Receiver
Last active August 29, 2015 14:11
Show Gist options
  • Save angp/0a4f3e5e596705d3cbd9 to your computer and use it in GitHub Desktop.
Save angp/0a4f3e5e596705d3cbd9 to your computer and use it in GitHub Desktop.
//Origional code from http://www.ediy.com.my/index.php/blog/item/74-digispark-infrared-receiver
//Modified by Daniel Splawski 02/11/2013
//Uncomment DigiKeyboard statments for debugging
//#include <DigiKeyboard.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(0,1); // RX, TX
int irPin = 2; //Sensor pin connect to digital pin2 (ATINY85 pin7)
int start_bit = 3000; //Start bit threshold (Microseconds)
int repeat_bit = 1700; //Repeat bit threshold (Microseconds)
int bin_1 = 900; //Binary 1 threshold (Microseconds)
int bin_0 = 400; //Binary 0 threshold (Microseconds)
const byte BIT_PER_BLOCK = 32;
String dataString = "";
void setup()
{
pinMode(irPin, INPUT);
mySerial.begin(9600);
}
void loop()
{
//DigiKeyboard.update(); // keep updating the keyboard
//DigiKeyboard.sendKeyStroke(0);
int key = getIRKey(); //Fetch the key
if(key >= -1 && key <= 26) //Ignore keys that are outside of range
{
switch(key)
{
case -1:break; //No change in the dataString so the previous output will be repeated.
case 0: dataString = "1"; break;
case 1: dataString = "2"; break;
case 2: dataString = "3"; break;
case 4: dataString = "4"; break;
case 5: dataString = "5"; break;
case 6: dataString = "6"; break;
case 8: dataString = "7"; break;
case 9: dataString = "8"; break;
case 10: dataString = "9"; break;
case 12: dataString = "a"; break;
case 13: dataString = "b"; break;
case 14: dataString = "c"; break;
case 16: dataString = "d"; break;
case 17: dataString = "e"; break;
case 18: dataString = "f"; break;
case 20: dataString = "g"; break;
case 21: dataString = "h"; break;
case 22: dataString = "i"; break;
case 24: dataString = "j"; break;
case 25: dataString = "k"; break;
case 26: dataString = "l"; break;
}
mySerial.println(dataString);
//DigiKeyboard.println(dataString);
}
}
// decode infrared signal
int getIRKey() {
int data[BIT_PER_BLOCK];
int i;
int test;
test = pulseIn(irPin, HIGH);
if(test > start_bit) //tests for the 4.5 ms start bit
{
for(i = 0 ; i < BIT_PER_BLOCK ; i++)
data[i] = pulseIn(irPin, HIGH); //Start measuring bits, I only want HIGH pulses
delay(100);
for(i = 0 ; i < BIT_PER_BLOCK ; i++) //Parse them
{
if(data[i] > bin_1) //is it a 1?
data[i] = 1;
else if(data[i] > bin_0) //is it a 0?
data[i] = 0;
else
return -2; //Flag the data as invalid; Return -2 on invalid data
}
//based on NEC protocol, command data started from bit 16
//and end with bit 24 (8 bits long)
int result = 0;
for(i = 16 ; i < 24; i++) {
if(data[i] == 1) result |= (1<<i-16); //Convert data bits to integer
}
return result; //Return key number
}
else if(test > repeat_bit) return -1; //Tests for the 2.5 ms repeat bit and sends -1 if true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment