How do I wait for input signal from an IR reciver sensor using while loop?

Here is my code -

#include <LiquidCrystal.h> 
#include<IRremote.h>

IRrecv irrecv ( 8 );
decode_results results;

int Contrast=20;
LiquidCrystal lcd( 12, 11, 5, 4, 3, 2 ); 

bool got;
int val;

void setup()
{
    Serial.begin ( 9600 );
    analogWrite ( A0, Contrast );
    lcd.begin ( 16, 2 );

     lcd.setCursor ( 0, 0 );
     lcd.print ( " Power off " );
     delay ( 1000 );

     got = false;

     while ( got != true )
     {
      delay ( 1000 );
      if ( irrecv.decode( &results ) )
      {
         val = results.value;
       }

      delay ( 1000 );
      if ( val == 33441975 )
      {
        got = true;
        delay ( 1000 );
      }
      irrecv.resume();
      Serial.println ( results.value );
     }
     lcd.setCursor ( 0, 1 );
     lcd.print ( "Power on " );
} 
void loop()
{

}

Please tell if the while loop is correct, because I don’t think so as it does not work. It just prints 0’s and if it is wrong please supply me with a correct one.

IR reciver - VS1838
LCD - 2 * 16

Thank you!

Hint: The code in setup() is executed only one time then execution goes to loop().

wouldn't it make more sense to put the irrecv.decode() inside the while condition rather than a flag, "got", that is only ever set to true

It’s simple:

      while (! irrecv.decode( &results ) ) ;  // Do nothing until there is a successful decode
     
         val = results.value;

Let’s hope the first signal you successfully decode is the one you were waiting for.

If you wanted to wait for a PARTICULAR signal:

boolean resume()
{
  irrecv.resume();
  return false;
)
.
.
.

     // Do nothing until there is a successful and matching decode
      while ( (!irrecv.decode( &results )) && (results.value == 33441975 || resume())) ;

The trick here is boolean shortcutting. If the left side of an “AND” is false, the right side is not evaluated. If the left side of an ‘OR’ is true, the right side isn’t evaluated. This allows the irrecv.resume() to be called if a message has been received and decoded but the value is not correct

Thank you all! @johnwasser it now prints power off first but after about 500 milliseconds it prints power on without pressing the button on the IR remote...Please help!

dougp: Hint: The code in setup() is executed only one time then execution goes to loop().

Please explain more clearly. Thank you!

TheUNOGuy: Please explain more clearly. Thank you!

Really?

All your code is in the function setup. Once setup finishes,the Arduino executes function loop forever until powered off. You have nothing in loop. So after it prints “power on”, that’s all folks...

If you could see the Arduion’s main function it would look something like this...

void main()
{
 setup();
 while(true) loop();
}

Thank you everyone for your posts! It worked!

TheUNOGuy: Thank you all! @johnwasser it now prints power off first but after about 500 milliseconds it prints power on without pressing the button on the IR remote...Please help!

Sounds like there is a mistake in your sketch. If you want further help you should post your latest sketch.