IR sensor decoder project

Hello,
This is my first post as you see - just got my first arduino uno this week, very happy with it.
I bought it to make a small but yet a bit challenge project - decoding my Toshiba TV remote. Now I’m working on the power button. It all works fine, but not entirely.
Little bit about IR:
As you maybe know, IR decoding could be a complicated process. IR pulse (the most common of these) is a binary code modulated on a 38KHz INFRA RED pulse. My target is to demodulate that code, compare it to a recorded code which I stored from the beginning, and let the sketch decide whenever the decoded code fits to the one I want - or it’s another code.

The whole project is a bit like a big lesson to me at the arduino language - my past experience is more at the at89c5131 atmel chip, which was the heart of my final-project at school, programmed in C language of the 5131. Now it’s very different, since in arduino it’s a LOT more simple to program.

The sketch I use called IR commander (with the “ircodes.h” included) right from ladyada.net
You can find the whole tutorial here: Sensor tutorials - IR remote receiver/decoder tutorial
The last sketch in the tutorial is the one I use. The different is the original sketch prints in the serial monitor “play” whenever
The play button is pressed in the apple remote. I changed the sketch a bit (I first recorded the power button code of the Toshiba remote) and now when I press power button in the Toshiba remote, the arduino prints “play” in the serial monitor and toggle a LED connected to pin 12 (with a resistor).

My ir sensor is not the one they use in the tutorial. It’s VS1838B. The datasheet can be downloaded here:
http://www.datasheet.co.kr/download.php?id=700398

So, you wonder what’s wrong…the trouble is that the arduino prints out “PLAY REWIND FARWORD” randomly it decodes ir codes which never been transmitted to it.
It looks like all the 3 “IF” conditions are TRUE all together. How’s that possible?

Take a look on the relevant sketch:

void loop(void) {
  int numberpulses;
  
  numberpulses = listenForIR();
  
  Serial.print("Heard ");
  Serial.print(numberpulses);
  Serial.println("-pulse long IR signal");
  if (IRcompare(numberpulses, ApplePlaySignal)) {
    Serial.println("PLAY");
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState); 
    
  }
    if (IRcompare(numberpulses, AppleRewindSignal)) {
    Serial.println("REWIND");
  }
    if (IRcompare(numberpulses, AppleForwardSignal)) {
    Serial.println("FORWARD");
  }
}

here is a print-screen of the serial monitor during the problem

and here is the whole code i use-

short explain about the fuctions:
int listenForIR(void) - creates the temporarely array of the ir code from the ir sensor.
boolean IRcompare(int numpulses, int Signal) -really not shure about this one. it looks like its meant for comparing the recieved code with whe one stored in the ircodes.h, but still havent realized yet what the “signal” array doing there.
void printpulses(void) - not in use for now. its for printing the result of the compared arrays.

/* Raw IR commander
 
 This sketch/program uses the Arduno and a PNA4602 to 
 decode IR received.  It then attempts to match it to a previously
 recorded IR signal
 
 Code is public domain, check out www.ladyada.net and adafruit.com
 for more tutorials! 
 */

// We need to use the 'raw' pin reading methods
// because timing is very important here and the digitalRead()
// procedure is slower!
//uint8_t IRpin = 2;
// Digital pin #2 is the same as Pin D2 see
// http://arduino.cc/en/Hacking/PinMapping168 for the 'raw' pin mapping
#define IRpin_PIN      PIND
#define IRpin          2

// the maximum pulse we'll listen for - 65 milliseconds is a long time
#define MAXPULSE 65000

// what our timing resolution should be, larger is better
// as its more 'precise' - but too large and you wont get
// accurate timing
#define RESOLUTION 20 

// What percent we will allow in variation to match the same code
#define FUZZINESS 20

// we will store up to 100 pulse pairs (this is -a lot-)
uint16_t pulses[100][2];  // pair is high and low pulse 
uint8_t currentpulse = 0; // index for pulses we're storing

#include "ircodes.h"
const int ledPin =  12;      // the number of the LED pin
int ledState = LOW;   


void setup(void) {
  Serial.begin(9600);
  Serial.println("Ready to decode IR!");
    pinMode(ledPin, OUTPUT);      
}

void loop(void) {
  int numberpulses;
  
  numberpulses = listenForIR();
  
  Serial.print("Heard ");
  Serial.print(numberpulses);
  Serial.println("-pulse long IR signal");
  if (IRcompare(numberpulses, ApplePlaySignal)) {
    Serial.println("PLAY");
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState); 
    
    
  }
    if (IRcompare(numberpulses, AppleRewindSignal)) {
    Serial.println("REWIND");
  }
    if (IRcompare(numberpulses, AppleForwardSignal)) {
    Serial.println("FORWARD");
  }
}

boolean IRcompare(int numpulses, int Signal[]) {
  
  for (int i=0; i< numpulses-1; i++) {
    int oncode = pulses[i][1] * RESOLUTION / 10;
    int offcode = pulses[i+1][0] * RESOLUTION / 10;
    
    /*
    Serial.print(oncode); // the ON signal we heard
    Serial.print(" - ");
    Serial.print(Signal[i*2 + 0]); // the ON signal we want 
    */
    
    // check to make sure the error is less than FUZZINESS percent
    if ( abs(oncode - Signal[i*2 + 0]) <= (Signal[i*2 + 0] * FUZZINESS / 100)) {
      //Serial.print(" (ok)");
    } else {
      //Serial.print(" (x)");
      // we didn't match perfectly, return a false match
      return false;
    }
    
    /*
    Serial.print("  \t"); // tab
    Serial.print(offcode); // the OFF signal we heard
    Serial.print(" - ");
    Serial.print(Signal[i*2 + 1]); // the OFF signal we want 
    */
    
    if ( abs(offcode - Signal[i*2 + 1]) <= (Signal[i*2 + 1] * FUZZINESS / 100)) {
      //Serial.print(" (ok)");
    } else {
      //Serial.print(" (x)");
      // we didn't match perfectly, return a false match
      return false;
    }
    
    //Serial.println();
  }
  // Everything matched!
  return true;
}

int listenForIR(void) {//???? ?? ????? ????? ?? ????? ??????? ??????? IR. ?????? ?puls
  currentpulse = 0;
  
  while (1) {
    uint16_t highpulse, lowpulse;  // temporary storage timing
    highpulse = lowpulse = 0; // start out with no pulse length
  
//  while (digitalRead(IRpin)) { // this is too slow!
    while (IRpin_PIN & (1 << IRpin)) {
       // pin is still HIGH

       // count off another few microseconds
       highpulse++;
       delayMicroseconds(RESOLUTION);

       // If the pulse is too long, we 'timed out' - either nothing
       // was received or the code is finished, so print what
       // we've grabbed so far, and then reset
       if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
         return currentpulse;
       }
    }
    // we didn't time out so lets stash the reading
    pulses[currentpulse][0] = highpulse;
  
    // same as above
    while (! (IRpin_PIN & _BV(IRpin))) {
       // pin is still LOW
       lowpulse++;
       delayMicroseconds(RESOLUTION);
       if ((lowpulse >= MAXPULSE)  && (currentpulse != 0)) {
         return currentpulse;
       }
    }
    pulses[currentpulse][1] = lowpulse;

    // we read one high-low pulse successfully, continue!
    currentpulse++;
  }
}

void printpulses(void) {
  Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
  for (uint8_t i = 0; i < currentpulse; i++) {
    Serial.print(pulses[i][0] * RESOLUTION, DEC);
    Serial.print(" usec, ");
    Serial.print(pulses[i][1] * RESOLUTION, DEC);
    Serial.println(" usec");
  }
  
  // print it in a 'array' format
  Serial.println("int IRsignal[] = {");
  Serial.println("// ON, OFF (in 10's of microseconds)");
  for (uint8_t i = 0; i < currentpulse-1; i++) {
    Serial.print("\t"); // tab
    Serial.print(pulses[i][1] * RESOLUTION / 10, DEC);
    Serial.print(", ");
    Serial.print(pulses[i+1][0] * RESOLUTION / 10, DEC);
    Serial.println(",");
  }
  Serial.print("\t"); // tab
  Serial.print(pulses[currentpulse-1][1] * RESOLUTION / 10, DEC);
  Serial.print(", 0};");
}

here is the ircodes.h:

/******************* our codes ************/

int ApplePlaySignal[] = {
// ON, OFF (in 10's of microseconds)
992, 438,
58, 54,
56, 48,
62, 50,
62, 48,
62, 48,
62, 50,
60, 160,
60, 52,
58, 162,
58, 158,
62, 160,
60, 162,
58, 158,
62, 160,
60, 46,
64, 158,
62, 50,
60, 158,
62, 50,
60, 52,
58, 162,
58, 50,
60, 52,
58, 52,
58, 162,
58, 52,
58, 160,
60, 162,
58, 48,
62, 162,
58, 158,
62, 160,
60, 3910,
888, 214};

int AppleForwardSignal[] = {
// ON, OFF (in 10's of microseconds)
    908, 444,
    64, 50,
    66, 162,
    64, 162,
    64, 162,
    64, 52,
    64, 162,
    64, 162,
    64, 162,
    64, 164,
    62, 164,
    64, 162,
    64, 52,
    62, 52,
    64, 52,
    64, 50,
    64, 164,
    64, 50,
    64, 164,
    64, 162,
    64, 50,
    66, 50,
    66, 50,
    64, 50,
    66, 50,
    64, 52,
    64, 50,
    66, 160,
    66, 50,
    64, 162,
    66, 50,
    64, 162,
    64, 50,
    66, 3938,
    906, 214,
    66, 3014,
    906, 214,
    64, 0};

int AppleRewindSignal[] = {
// ON, OFF (in 10's of microseconds)
    908, 442,
    66, 48,
    66, 162,
    66, 160,
    66, 160,
    66, 50,
    66, 160,
    66, 160,
    66, 160,
    68, 158,
    68, 160,
    66, 160,
    66, 50,
    66, 48,
    66, 50,
    66, 48,
    66, 162,
    66, 160,
    66, 48,
    68, 48,
    66, 160,
    66, 50,
    66, 50,
    66, 48,
    66, 50,
    66, 48,
    68, 48,
    66, 160,
    66, 50,
    66, 160,
    66, 50,
    66, 160,
    66, 48,
    68, 3936,
    906, 214,
    66, 0};

Make sure you've looked at this: http://www.sbprojects.com/knowledge/ir/index.php