InduinoX User Guide - Working with TSOP IR Receiver - Part 2

We hope you had success with the first part of this write-up. If you have not read it, we suggest you go through it before reading ahead. InduinoX User Guide - Working with TSOP IR Receiver - Part 1

Now that we've used the library successfully, we will see how to code manually to receive from a Sony Remote. Later in the next part, we will see how to generate Sony Signals. Its advised that you read this and try this out completely before moving onto signal generation

Lets take a look at the sony signal train









Now Here's how we are going to go ahead with decoding
  1. Keep checking the TSOP pin (Pin 15) for a LOW pulse of duration in excess of 2ms, the moment you get such a signal proceed on to step 2
  2. Run a 'for' loop for 12 counts, during each iteration of the loop, get the current pulse duration using the pulseIn function. Check if the duration is greater than 1000ms (means its a '1') else its a '0'
  3. As soon as you detect a '1' or '0' add it to an appropriate binary to decimal conversion logic.


Here's the code, You can plug in the remote function into any of your programs, just remember to declare pin 15 as INPUT.
 void setup()  
 {  
  pinMode(15,INPUT); // TSOP is connected on the 15ht pin  
  Serial.begin(9600);  
 }  
 void loop()  
 {  
  int remote_val = remote();  
  if(remote_val>0)  
  {  
   Serial.println(remote_val);  
   delay(150); // A remote press will normally generate 3 signal trains. This is to avoid reading duplicates  
  }  
 }  
 int remote()  
 {  
  int value = 0;  
  int time = pulseIn(15,LOW);  
  if(time>2000) // Checking if the Start Bit has been received. Start Bit Duration is 2.4ms  
  {  
   for(int counter1=0;counter1<12;counter1++) // A loop to receive the next 12 bits  
   {  
    if(pulseIn(15,LOW)>1000) // checking the duration of each pulse, if it is a '1' then we use it in our binary to decimal conversion, '0's can be ignored.  
    {  
     value = value + (1<< counter1);// binary to decimail conversion. 1<< i is nothing but 2 raised to the power of i  
    }  
   }  
  }  
  return value;  
 }