IR receiver sends inverted signal

I'm trying to make an arduino infrared remote control receiver and I'm getting incorrect results.
I have started with a basic program taken from Sensor tutorials - IR remote receiver/decoder tutorial that runs in a while loop and measures the length of the signals. From its output I understood that the remote key starts with a ON signal of ~9000 us followed by ~4000us OFF signal. I found an IR receiver code based on interrupts, posted by someone on his/her blog that was written to communicate with another arduino through IR. I made some changes to it to support receiving remote control keys.
Since I'm only at the beginning I decided to output every signal trough serial com. I didn't clean up the code since I'm more interested in making it work first. I'm posting the entire code for anyone that would like to help, but also with the idea that would give a starting point for someone that wants to do something similar.
Hardware: IR receiver connected to D2 on the arduino along with a pull-up resistor.
Software:

const byte LED_HIGH = 13;
byte readyToSend = false;
#define IR_PIN_BANK     PIND
#define IR_PIN          2

const byte isrBitLen = 80;
unsigned int signalsLen = 0;
unsigned int signals[isrBitLen];
unsigned int signalsOn[isrBitLen];
unsigned int signalsOff[isrBitLen];

void setup()
{
  pinMode (LED_HIGH, OUTPUT);  // so we can update the LED
  pinMode (IR_PIN, INPUT);    //read the IR
  digitalWrite(IR_PIN, HIGH);
  Serial.begin(9600);
  Serial.println("Starting");
//  TIMSK1|=0x01; // enabled global and timer overflow interrupt;
  TCCR1A = 0x00; // normal operation page 148 (mode0);
  TCNT1=0x0000; // 16bit counter register, set to 0
  TCCR1B = 0x02; // start timer/ set clock 8 prescalar - for 16mhz this means a overflow at every ~40 ms
  attachInterrupt (0, pinChange, CHANGE);  // attach interrupt handler



}

ISR(TIMER1_OVF_vect) {
  
  TIMSK1&=0x00;
  readyToSend = true;
  digitalWrite (LED_HIGH, HIGH^digitalRead(LED_HIGH));
}

// helper functions

unsigned long elapsedSince(unsigned long since, unsigned long now)
{
return since < now ? now-since : 0xFFFFFFFFUL - (now - since);
}

unsigned long elapsedSince(unsigned long since)
{
return elapsedSince( since, micros() );
}

// iSOBOT IR protocol timing
#define TimeStartOnJvc    9500
#define TimeStartOffJvc   4000
#define TimeZeroOnJvc      600
#define TimeZeroOffJvc     550
#define TimeOneOnJvc       600
#define TimeOneOffJvc       1600
#define TimeEnd             600
#define IsSignal(t,x) (true)    //for testing purpose
#define IsSignalOneOn(t)   IsSignal(t,TimeOneOnJvc)
#define IsSignalOneOff(t)   IsSignal(t,TimeOneOffJvc)
#define IsSignalZeroOn(t)   IsSignal(t,TimeZeroOnJvc)
#define IsSignalZeroOff(t)   IsSignal(t,TimeZeroOffJvc)
#define IsSignalStartOn(t)   IsSignal(t,TimeStartOnJvc)
#define IsSignalStartOff(t)   IsSignal(t,TimeStartOffJvc)
#define IsSignalEnd(t)       IsSignal(t,TimeEnd)

enum
{
ISR_IDLE,   // nothing is/was happening (quiet)
ISR_START,  //ready to receive
ISR_START_ON,  // start of sequence, was waiting for a header signal 
ISR_START_OFF,  // start of sequence, was waiting for a header signal off
ISR_BIT_ONE_ON, // transsmitting a bit (IR carrier turned on)
ISR_BIT_ONE_OFF, // transsmitting a bit (IR carrier turned on) - end of bit 1 transmission
ISR_BIT_ZERO_ON, // in an OFF bit slot (IR carrier turned off)
ISR_BIT_ZERO_OFF // in an OFF bit slot (IR carrier turned off) - end of transmission for bit 0
}
isrState = ISR_IDLE;

unsigned long isrLastTimeStamp;
unsigned long isrRcvCmd;
unsigned long isrNewCmd;

byte isrBitCnt;

void pinChange()
{
  
  TCNT1 = 0;      //reset timer1
  readyToSend = false;
// receiving a modulated IR signal makes the pin go low (active low)
byte bIrSignalOn = (! (IR_PIN_BANK & _BV(IR_PIN)));/*(PIND & (1<<2)) == 0;*/

unsigned elapsed;
{
 unsigned long timeStamp = micros();
 elapsed = elapsedSince(isrLastTimeStamp,timeStamp);
 isrLastTimeStamp = timeStamp;
}
if (signalsLen < isrBitLen)
{
  if (bIrSignalOn)
  {
    signalsOn[signalsLen] = elapsed;
  }
  else
  {
    signalsOff[signalsLen] = elapsed;
  }
//signals[signalsLen] = elapsed/100;
switch( isrState )
{
  case ISR_IDLE :
    if( bIrSignalOn ) 
    {
      isrState = ISR_START;
    }
    break;
  case ISR_START:
    signals[signalsLen] = elapsed;
    isrBitCnt = 0;
    isrNewCmd = 0;
    if(/*!bIrSignalOn && */IsSignalStartOn(elapsed) )
      isrState = ISR_START_ON; 
    else
      isrState = ISR_IDLE;
    break;
  case ISR_START_ON:
    signals[signalsLen] = elapsed;
    isrBitCnt = 0;
    isrNewCmd = 0;
    if(/*bIrSignalOn && */IsSignalStartOff(elapsed) )
      isrState = ISR_START_OFF; // bits are now rolling
    else
      isrState = ISR_IDLE;   // wrong timing of start or pin state
    break;
   case ISR_BIT_ONE_OFF:    
   case ISR_BIT_ZERO_OFF:    
   case ISR_START_OFF:
    signals[signalsLen] = elapsed;
//    if(bIrSignalOn)
//    {
      if (IsSignalOneOn(elapsed))
      {
        isrState = ISR_BIT_ONE_ON;
      }
      else if (IsSignalZeroOn(elapsed))
      {
        isrState = ISR_BIT_ZERO_ON;
      }
      else
      {
         isrState = ISR_IDLE; // bad state
      }
//    }
//    else isrState = ISR_IDLE; // bad state
    break;
  case ISR_BIT_ONE_ON:
  case ISR_BIT_ZERO_ON:
    signals[signalsLen] = elapsed;
//    if( !bIrSignalOn )
//    {
      
      if (isrState == ISR_BIT_ONE_ON && IsSignalOneOff(elapsed))
      {
        isrState = ISR_BIT_ONE_OFF;
        isrNewCmd |= 1;
      }
      else if (isrState == ISR_BIT_ONE_ON && IsSignalZeroOff(elapsed))
      {
        isrState = ISR_BIT_ZERO_OFF;
        isrNewCmd |= 0;
      }
      else
      {
         isrState = ISR_IDLE; // bad state
      }
      isrNewCmd <<= 1;
      isrBitCnt ++;
//    }
//    else isrState = ISR_IDLE; // bad state
    break;  
}
    TCNT1 = 0;      //reset timer1
}
else
{
   readyToSend = true;
}
    signalsLen++; 
   TIMSK1|=0x01; 
}

void loop()
{
  
  unsigned long rcv = 0;
  if (readyToSend)
 {
   if (signalsLen > 0)
   {
     TIMSK1&=0x00;
        Serial.print("signalsLen = ");
     Serial.println(signalsLen);
     for (int j = 0 ; j < signalsLen && j < isrBitLen; j++)
     {
       Serial.print(signals[j]);
        Serial.print('.');  
     }
     Serial.println("Cmd = ");
    Serial.println(isrNewCmd);
     
     Serial.println("On");
     for (int j = 0 ; j < signalsLen && j < isrBitLen; j++)
     {
       Serial.print(signalsOn[j]);
        Serial.print('.');  
     }
     Serial.println('.'); 
     
     Serial.println("Off");
     for (int j = 0 ; j < signalsLen && j < isrBitLen; j++)
     {
       Serial.print(signalsOff[j]);
        Serial.print('.');  
     }
     Serial.println('.'); 
     signalsLen=0;
   }
   readyToSend = false;
 } 
}

First output, after reset:

Starting
signalsLen = 1
0.Cmd =
0
On
13616..
Off
0..
signalsLen = 71
8996.4336.644.484.652.484.644.476.640.496.636.1592.636.496.616.1612.656.460.668.1564.640.1592.624.1604.652.1576.656.472.652.1580.672.452.676.1552.632.508.632.488.640.1588.656.1580.652.468.668.472.648.484.652.460.672.1560.668.1568.660.456.672.456.656.1588.640.1580.652.1588.656.1568.668.40160.8992.2152.640.Cmd =
4294967294
On
13616.4336.0.484.0.484.0.476.0.496.0.1592.0.496.0.1612.0.460.0.1564.0.1592.0.1604.0.1576.0.472.0.1580.0.452.0.1552.0.508.0.488.0.1588.0.1580.0.468.0.472.0.484.0.460.0.1560.0.1568.0.456.0.456.0.1588.0.1580.0.1588.0.1568.0.40160.0.2152.0..
Off
8996.0.644.0.652.0.644.0.640.0.636.0.636.0.616.0.656.0.668.0.640.0.624.0.652.0.656.0.652.0.672.0.676.0.632.0.632.0.640.0.656.0.652.0.668.0.648.0.652.0.672.0.668.0.660.0.672.0.656.0.640.0.652.0.656.0.668.0.8992.0.640..

Same remote key pressed the second time:

signalsLen = 1
45976.Cmd =
4294967294
On
45976..
Off
8996..
signalsLen = 67
8976.4352.656.476.640.488.640.484.628.500.668.1572.644.472.644.1600.640.476.612.1616.640.1592.644.1584.648.1584.652.476.628.1600.632.496.640.1588.656.472.648.480.640.1592.652.1576.656.468.656.464.656.484.652.472.692.1540.640.1592.652.472.644.484.652.1568.652.1580.656.1572.664.1576.656.Cmd =
4294967294
On
45976.4352.0.476.0.488.0.484.0.500.0.1572.0.472.0.1600.0.476.0.1616.0.1592.0.1584.0.1584.0.476.0.1600.0.496.0.1588.0.472.0.480.0.1592.0.1576.0.468.0.464.0.484.0.472.0.1540.0.1592.0.472.0.484.0.1568.0.1580.0.1572.0.1576.0..
Off
8976.0.656.0.640.0.640.0.628.0.668.0.644.0.644.0.640.0.612.0.640.0.644.0.648.0.652.0.628.0.632.0.640.0.656.0.648.0.640.0.652.0.656.0.656.0.656.0.652.0.692.0.640.0.652.0.644.0.652.0.652.0.656.0.664.0.656..

The issue here is the one signal command received before the actual key. It has a random length and after it the rest is inverted, meaning that the signal ~9000(first ex: 8996) should be ON and not OFF and so on.
Any input would be appreciated.
Thanks

  Serial.begin(9600);

Try changing to 115200 baud and test again. No point in having really slow comms. The buffer isn't that big.

Good suggestion, Nick. Thanks.
Done that and of course, no change.

There's an IR library. Any objection to using that?

I don't see why I can't use an IR lib. I only found one that uses 50us timer interrupt to check the status of the IR receiver pin and didn't like the idea of interrupting the program at such small intervals.
Please provide a link to the lib you had in mind.
Thanks

This is one I think I used recently:

Thanks again, Nick. Will give it a try.
I managed to understand why the signals were inverted, but there is one problem that I don't understand. If the timer1 overflows, the remote control key codes that were received so far are sent through serial and the timer1 is stopped. When the IR pin is changed, the timer1 is restarted - at least in theory. Immediately after the second key is pressed timer1 is overflown after the first change interrupt and not at the end of the transmission as it should. I'll give you an example: the timer1 is set at ~40ms, the first time it overflows is correct, at the end of the transmission, after 40ms have passed since the last interrupt change. When I press the second key, the timer overflows after the first change and I get the length of the entire time that passed since the last transmission, after which I get the correct key pressed - a 2 part serial transmission made of 1 code and the remote control code.
tcnt1 = 0 resets the overflow, right? Before I restart the timer I always reset it. What am I missing?

My 2p worth...

I've used the following code, based on Ken Shirriff's and using the library. It uses the (Philips RC5) remote's volume up and down keys to control a PWM'd led on pin 5.

(Btw, It seems that the pwm pin 3 is disabled when using the library- I know the library uses pin 3 to send IR, but even when not sending it seems the pin is unavailable. (I'd appreciate confirmation on that from someone...))

/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */
 
/* well now it controls the brightness of an LED
   LED is PWM'd on pin5
   *******   PWM on pin3 seems disabled when using IR?
   uses the volume key... up to brighten, down to dim
   under rc5, the keys have two toggled values
   down:    411 or c11 hex; 1041 or 3089 dec
   up:      410 or c10 hex; 1040 or 3088 dec
*/
               

#include <IRremote.h>

int RECV_PIN = 11;
int led_pin = 5;   // seems PWM not work on 3 with IR library
int led_bright = 130;  //half way to start
int led_step = 10;     // and change in pwm steps of 10

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(led_pin, OUTPUT);
  analogWrite(led_pin, led_bright);
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
    
    //now check if it's correct key and act...
    //down
    if (results.value == 1041 || results.value == 3089) {
      led_bright = led_bright - led_step;
      if (led_bright < 10) {
        led_bright = 10;
        }
      Serial.print("Going down to ");
      Serial.println(led_bright);
      analogWrite(led_pin, led_bright);
    }
    
    //up
    if (results.value == 1040 || results.value == 3088) {
      led_bright = led_bright + led_step;
      if (led_bright > 255) {
        led_bright = 255;
      }
      Serial.print("Going up to ");
      Serial.println(led_bright);
      analogWrite(led_pin, led_bright);
    }
  }
}

I'm sorry, I have the problem with my IR receiver. I press the same key, but result all time different:

6401FA83
Unknown encoding: 6401FA83 (32 bits)
Raw (68): 5532 4550 -4550 450 -1750 500 -1800 500 -1700 550 -550 600 -500 600 -550 550 -600 500 -600 550 -1750 550 -1650 600 -1600 600 -650 450 -600 550 -600 500 -600 500 -650 550 -550 600 -500 600 -1750 500 -1700 500 -600 500 -1750 600 -1650 600 -500 600 -1700 500 -1800 400 -650 600 -550 550 -1650 600 -550 600 -600 450 -1750 500

BEF7F265
Unknown encoding: BEF7F265 (32 bits)
Raw (68): 27590 4500 -4550 550 -1700 550 -1700 500 -1750 500 -650 450 -650 550 -600 500 -600 550 -550 600 -1700 500 -1750 450 -1750 550 -600 550 -550 600 -550 550 -600 500 -600 500 -650 450 -700 500 -1700 550 -1700 550 -600 550 -1700 450 -1750 550 -600 500 -1750 550 -1700 550 -650 400 -700 500 -1700 550 -650 450 -600 550 -1700 550

D9C9E5E0
Unknown encoding: D9C9E5E0 (32 bits)
Raw (68): -9770 4500 -4550 500 -1750 600 -1650 600 -1650 500 -650 500 -600 550 -550 550 -600 550 -550 600 -1700 550 -1700 450 -1800 500 -650 500 -600 550 -550 600 -500 600 -550 600 -550 450 -650 500 -1800 500 -1700 600 -500 600 -1650 600 -1700 450 -700 450 -1800 500 -1700 600 -500 600 -550 600 -1650 500 -650 500 -600 550 -1700 600

6E3B1E89
Unknown encoding: 6E3B1E89 (32 bits)
Raw (68): 29874 4550 -4550 500 -1700 600 -1650 600 -1800 350 -700 450 -600 550 -650 450 -600 600 -500 600 -1700 550 -1750 500 -1700 550 -600 500 -550 600 -550 600 -550 550 -550 600 -550 550 -650 450 -1700 550 -1700 600 -500 600 -1700 550 -1700 500 -650 500 -1750 500 -1700 600 -550 550 -550 600 -1700 500 -600 550 -600 500 -1700 600

DAEA83EC
Unknown encoding: DAEA83EC (32 bits)
Raw (68): 31146 4500 -4600 500 -1700 600 -1700 550 -1650 550 -600 500 -600 550 -600 500 -600 600 -550 550 -1700 550 -1700 500 -1750 550 -550 550 -600 550 -550 600 -550 550 -600 550 -600 500 -550 550 -1700 550 -1700 600 -550 550 -1700 550 -1700 500 -600 550 -1700 550 -1700 600 -550 550 -600 500 -1700 550 -600 550 -600 500 -1700 600

EE04ACCB
Unknown encoding: EE04ACCB (32 bits)
Raw (68): -14248 4450 -4600 550 -1650 550 -1800 500 -1750 500 -550 600 -550 550 -600 550 -550 550 -550 550 -1700 550 -1800 450 -1700 600 -550 550 -550 550 -600 500 -650 500 -600 550 -650 450 -600 550 -1700 550 -1650 550 -650 500 -1800 450 -1700 550 -600 550 -1700 550 -1700 500 -650 500 -650 450 -1750 550 -550 600 -550 550 -1650 550

6D701C27
Unknown encoding: 6D701C27 (32 bits)
Raw (68): -26388 4600 -4500 550 -1750 500 -1800 450 -1700 550 -600 550 -600 500 -600 550 -600 500 -600 500 -1750 550 -1650 600 -1700 550 -550 550 -700 400 -600 550 -600 550 -600 500 -600 550 -550 550 -1700 550 -1800 450 -600 550 -1700 550 -1700 500 -650 500 -1750 450 -1750 550 -600 550 -600 500 -1750 500 -600 550 -550 550 -1750 500

AB569F5F
Unknown encoding: AB569F5F (32 bits)
Raw (68): -8440 4350 -4600 500 -1700 550 -1700 550 -1700 550 -650 450 -600 550 -650 450 -700 450 -550 550 -1700 550 -1700 550 -1750 500 -650 500 -600 500 -600 550 -550 550 -600 550 -550 550 -700 400 -1800 500 -1750 500 -550 550 -1700 550 -1750 500 -600 550 -1800 450 -1700 550 -550 550 -600 550 -1750 500 -600 500 -650 500 -1650 600

C3F595A8
Unknown encoding: C3F595A8 (32 bits)
Raw (68): -27926 4500 -4550 550 -1800 450 -1700 550 -1700 450 -650 600 -550 550 -600 550 -550 550 -700 350 -1750 550 -1800 450 -1700 600 -550 550 -600 550 -800 300 -550 550 -600 500 -650 500 -850 200 -1800 550 -1700 550 -550 550 -1700 550 -1750 500 -600 550 -1700 550 -1700 500 -700 450 -650 450 -1800 500 -600 550 -550 550 -1700 500

82FC09E
Unknown encoding: 82FC09E (32 bits)
Raw (66): 19868 4450 -4550 500 -3950 550 -1700 550 -650 450 -650 500 -700 450 -700 450 -750 350 -1700 550 -1700 550 -1800 450 -650 500 -650 450 -650 450 -600 550 -550 550 -600 550 -550 550 -1800 500 -1850 350 -700 450 -1700 550 -1700 550 -700 450 -1750 450 -1800 450 -600 550 -550 550 -1700 600 -650 500 -600 450 -1850 400

FE6161C4
Unknown encoding: FE6161C4 (32 bits)
Raw (68): 4652 3650 -4700 400 -1800 450 -1700 550 -1750 450 -800 400 -600 500 -600 550 -600 500 -600 550 -1700 550 -1700 500 -1750 550 -600 500 -650 500 -550 550 -600 550 -550 550 -600 550 -650 450 -1750 500 -1850 400 -600 550 -1700 550 -1650 600 -600 500 -1700 550 -1700 600 -550 550 -550 550 -1750 500 -650 500 -550 550 -1750 500

2217DBD
Unknown encoding: 2217DBD (32 bits)
Raw (68): 30746 4500 -4550 550 -1700 550 -1700 550 -1650 600 -550 550 -600 550 -600 500 -600 550 -550 600 -1650 600 -1650 550 -1750 500 -600 500 -600 550 -600 550 -550 600 -550 550 -550 550 -600 550 -1700 550 -1750 500 -550 600 -1650 600 -1700 450 -650 550 -1800 450 -1700 550 -550 600 -550 550 -1750 500 -550 550 -600 550 -1700 550

What's wrong?

The problem was this my usb-adapter!!!
I changed it to another one (from iPad) and now IR-receiver works ok!