I could not get my ATTiny to work with the 'IRremote.h' library. Also the library was pretty big.
So I made my own code.
The code gets triggered by a 'FALLING' signal, and then paused the Arduino code and start reading the data, and resumes when done. the received data is stored in 'IRData '
All lines are commented, so you should be able to find your way around the code easily.
How to reproduce?
I've used an 1838 IR receiver on a VMA317 board
I've used a cheap ebay LED controller (4x11 buttons) that runs on the NEC protecol
IR s (data pin) to pin 2 on the Arduino
Change '#define DEBUGMODE false' to true if you want more debug info (for if you want to change it to a diffrent protocol for example)
int PI_IRSensor = 2; //The pin where the IR sensor is connected to
volatile int IRData = 0; //This is where we store the (valid) IRData until it's processed
void setup() { //This code will be called once on startup
pinMode(PI_IRSensor, INPUT); //Set the IR pin to be an input (just to be sure)
Serial.begin(9600); //Start the connection with the pc
attachInterrupt(digitalPinToInterrupt(PI_IRSensor), infraredRead, FALLING); //When the IR sensor signal is pulled low, call the loop to read the data
}
void loop() { //This code will keep on looping over like a 'While(1)'
if (IRData != 0) { //If there is IRData
Serial.println(String("[data=") + IRData + String("]")); //Print the IRData to the pc
IRData = 0; //Reset IRData value (since we have processed it)
}
//Your normal code here, will keep on running unles there is IR data to be checked
}
void infraredRead() { //This loop will be called when the pin is pulled low (and we need to read the data)
//Code Written by JelleWho
#define DEBUGMODE false //Set to true to get feedback about below code
/*
Example of debud data: (NEC contoller and pressing on/off button)
<>[bin=0000010111111010]
<E1[data=-16576]
What happens:
'<' Start of this codeloop
'>' Stop of this code loop (succesfull)
'[bin=X]' Binary data
'E1' Program started but could not find start loop
'[data=y]' IR data converted from binary to int
'E2' Button is keep being pressed and we found 0xFFFFF (a startpulse with no data)
*/
#if DEBUGMODE
String IRDataRawBinary = ""; //Raw binary sensor data
Serial.print(String("<")); //Tell the pc we are starting retrieving data (be aware this has a lower prior, and could be delayed in real world sending)
#endif
const int LowOrHighPoint = 1050; //Turnover time point of low/high High =(1500 - 1700) Low = (400 - 600) So we are right at the middle
const int TimeOut = 2000; //Maximum time in microseconds of normal bits (it's to much time to be called high)
const unsigned int start_bit = 4000; //Minimum time in microseconds of startbit
const int BeginAt = 17; //Real data begins here (the first real bit)
const int TheLength = 16 + BeginAt; //The length of the data we need. If a to long delay (1s) between '<' and '>' try lowering TheLength. If '<E1<E1' or more retrieved, try higher TheLength.
int data[TheLength]; //Create a array to put the bits in
if (pulseIn(PI_IRSensor, HIGH, 100000) < start_bit) { //Wait for the line to be HIGH again (100ms delay). and then measure THE LENGTH8
//(TODO FIXME THIS COULD HAVE A 4MIN LONG PULSE! (if we only retrieve a low signal and then it stays high)
#if DEBUGMODE
Serial.print(String("E1")); //Feedback; Timeout waiting on start pulse
#endif
return; //Stop if we have a timeout
}
//=====START PULSE DETECTED===== and now at the low at the end of that pulse
int i = 0; //A counter
while (i < TheLength - 1) { //Do the next part as much as we are expected bits
i++; //Increade the counter
data[i] = pulseIn(PI_IRSensor, HIGH); //Start measur length of the HIGH pulse
if (data[i] >= TimeOut) { //If the pin was longer HIGH than it could be (and thus it was not data)
#if DEBUGMODE
Serial.print(String("E2")); //Feedback; Timeout waiting on data pulse
#endif
return;
}
}
int a = 0; //A new counter (this is where to store the new data at in the array)
i = BeginAt; //Where the real data geginds at (so where we need to start reading from)
while (i < TheLength) { //While there is still data to be read
a++; //Increase the counter
i++; //Increase the counter
if (data[i] > LowOrHighPoint) { //is it a 1? (1500 - 1700)
data[a] = 1; //Set in the new array the data of this bit HIGH
#if DEBUGMODE
IRDataRawBinary += "1"; //DEBUG Save the state so we can read it out later
#endif
} else if (data[i] < LowOrHighPoint) { //is it a 0? (400 - 600)
data[a] = 0; //Set in the new array the data of this bit LOW
#if DEBUGMODE
IRDataRawBinary += "0"; //DEBUG Save the state so we can read it out later
#endif
}
}
unsigned int result = 0; //Create a int to store the result in
i = 0; //(reset) int a as a new counter (this is the bit we are going to save to the result)
while (i <= TheLength) { //Do while we still have bits to put in the result
i++; //Increase the counter
if (data[i] == 1) { //If the data is a '1'
result |= (1 << i); //Bitshift it into the result
}
}
IRData = result; //Put the result into the data as a whole (So other code can use it)
#if DEBUGMODE
Serial.print(String(">")); //Tell the pc we stopped retrieving data
Serial.println(String("[bin=") + IRDataRawBinary + String("]")); //Print the IRData to the pc
#endif
}
Image of the data (default HIGH)