IR sensor - IF statement Issue

Hi dears!!
I’m a new Arduino addicted and after I went mad with PWM error on pins 9 and 10 (due to the new bootloader), now I’m in troubleshooting again.

At this time I wrote a test sketch to recognize the IR protocol of an old sharp videotape remote controller.

The overall code works fine and by serial monitor I see the correct value in the button variable. To turn on a led on digital pin 13, I put an if statement to recognize the value linked to power-on button on my remote controller, but when I press the button on my remote nothing happen. Then To debug the program I put a serial.println after the if statement and magically the led now turns on.

Some one could help me to let me know how it is possible?

unsigned int button = 0;
int old = 0;
int new_bit = 0;
unsigned int interval =0;
int sensorValue;

unsigned int on = 24994;

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}

void loop() {
  sensorValue = digitalRead(2);
  
    if(new_bit != sensorValue){
      new_bit = sensorValue;
      if(new_bit == 1){
        if(interval < 200){
          button = button << 1;
          interval = 0;    
        }else{
          button = button << 1;
          button++;
          interval = 0;             
        }
        old++;
      }
    }
    
  if (old >= 16){
    old = 0;  
    if (button == on){
      digitalWrite(13, HIGH);
    }
    
//    Serial.println(button, DEC); // If I put this 
                                   // instruction it works
    
    button = 0;
  }
  
  if (old > 0)    
  interval++;    
  
}

Thanks for your help!!!

Fabio

Replace the serial print with a delay(30) and you might also find it works.

Grumpy_Mike thanks for your reply, I'll test it this evening when I'll come back to home :)

In the mean time, why do you think a delay can solve the issue? I know serial print take time, but I don't understand why the pin 13 is not able to turn on if I don't wait N ms? The pin 13 never goes low.

I think this has to do with contact bounce on your input switches. The delay just holds things until the contacts have finished bouncing.

The pin 13 never goes low.

It is doing but then going high again straight after.

On pin 2 I have a IR sensor and there's not a switch.

This device filters out the 40khz carrier and send a square wave signal that follows the sharp IR protocol (if the distance between 2 high status is 1ms this is a logical 1 bit, if the distance is 2ms this is logical 0 bit).

The program recognizes very well the 15 bits sequence and store it in the button variable, then if the value is equal to decimal 24994 (110000110100010 binary), the led on pin 13 should turns on. In the main loop pin 13 never can goes low.

So, I don't understand the contact bounce matter in this scenario.

Change :- if (old >= 16){

to if (old >= 15){

The code if (old >= 16){ is correct because I need to skip the first bit, I need to count time between 2 rise transitions.

The first hint to use delay(30) works…but I don’t know why.