IR Code problem

Hello

Im new in this forum and I hope I can get help from you guys to learn more abt Arduino

My problem is that I used a copy for IR remote and tested it , everything worked fine but then I added a loop to check if i keep pressing a button from a TV remote for like 1 sec and release my finger the loop should breaks but in my situation the loop doesnt break even if i release my finger

this is the code:

#include <IRremote.h>

const int RECV_PIN = 3;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
}

void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
while(results.value==0xE0E020DF){
Serial.println("hello");
irrecv.resume();

}
irrecv.resume();
}
}

can u help me solve this problem

Thanks

I forgot to mention i press anthor buttons but the loop still wont break

thx

Explain what you think is happening here:

while( results.value==0xE0E020DF )
{
Serial.println("hello" );
irrecv.resume( );
}

.

LarryD:
Explain what you think is happening here:

while( results.value==0xE0E020DF )
{
Serial.println("hello" );
irrecv.resume( );
}

compare both results ,, if they are equal then print hello

then irrecv.resume( ) will take anthor value from the remote and do the comparison again

Explain what does this does:

#include <IRremote.h>
 
const int RECV_PIN = 3;
 
IRrecv irrecv(RECV_PIN);
decode_results results;
 
void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();  
}
 
void loop() {
  if (irrecv.decode(&results)) 
  {
    Serial.println(results.value, HEX);
    if(results.value==0xE0E020DF)
    {
      Serial.println("hello");
    }
    irrecv.resume();
  }

}

LarryD:
Explain what does this does:

#include <IRremote.h>

const int RECV_PIN = 3;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); 
}

void loop() {
  if (irrecv.decode(&results))
  {
    Serial.println(results.value, HEX);
    if(results.value==0xE0E020DF)
    {
      Serial.println("hello");
    }
    irrecv.resume();
  }

}

Thanks for your respocne.

I know that if condition will work , but I wonder in my code why while loop will not break even if i press some other buttons since the value changed and the condition is not true anymore.

I'm sorry if my question seems naive but these simple things could help me get strong basics.

You're not reading a new value into the results variable so once you get that hex value it never changes. Since irrecv.decode returns a boolean you can use it to test whether a value was read from IR, if you put that together with the test for a specific value you can remove the if statement and make it part of the while test:

void loop() {

  while ((irrecv.decode(&results) && (results.value == 0xE0E020DF)) {
  Serial.println(results.value, HEX);
    Serial.println("hello");
    irrecv.resume();
  }
  irrecv.resume();

}

If it was my code I would prefer to use the built-in loop function to my favor and not have a tight while loop inside it, as in LarryD's example. That way the program would be free to do other things.

Be aware though, that you might not get the same key code over and over while holding down a remote button depending on the protocol that the remote uses.

You have to add a 'break' to the while loop as 'results.value' retains 0xE0E020DF

    while (results.value == 0xE0E020DF)
    {
      Serial.println("hello");
      irrecv.resume();
      break;
    }

'if()' makes more sense here.

.

Thanks for the explanation .

I tried this but nothing is shown in the serial monitor !

See post number 7.

LarryD:
You have to add a 'break' to the while loop as 'results.value' retains 0xE0E020DF

    while (results.value == 0xE0E020DF)

{
      Serial.println("hello");
      irrecv.resume();
      break;
    }





'if()' makes more sense here.


.

LarryD:
You have to add a 'break' to the while loop as 'results.value' retains 0xE0E020DF

    while (results.value == 0xE0E020DF)

{
     Serial.println("hello");
     irrecv.resume();
     break;
   }





'if()' makes more sense here.


.

My idea later is to use zumo robot , so for example if I keep holding forward button the robot will move forward until i release the button so it stops and so on for other directions . Can you help me figure a way to do that ?

Thanks

Here is an example that uses IRremote library.

//**********************************************************************
 
#include "IRremote.h"
//LarryD

//Sketch to demonstrate using an IR hand remote to control Arduino outputs.
//The remote used here uses the NEC protocol.
//This remote sends the button code (example 0xFF629D) then a repeat code 0xFFFFFFFF
//The repeat code is re-sent as long as an IR remote button is pressed.
//The IR receiver used is the TSOP4838
 
/*  17 IR button remote layout   http://i.imgur.com/X1KIdqI.png
 
   ^
< OK  >  
   v
1  2  3 
4  5  6
7  8  9
*  0  # 
 
*/
 
 
const byte RECV_PIN = 7;   //IR receive pin
 
IRrecv irrecv(RECV_PIN);   //create instance of 'IRrecv'
decode_results results;    //create instance of 'decode_results'
 
unsigned long   TimerUp;   //UP arrow on the remote
boolean         TimerUpFlag = false;
 
unsigned long   TimerDown; //DOWN arrow on the remote
boolean         TimerDownFlag = false;
 
unsigned long TimerIntensity;
unsigned long TimerIncDec;
boolean intesityUpFlag   = false;
boolean intesityDownFlag = false;
int brightness;
 
unsigned long   dummy;
unsigned long * TimerPtr = &dummy; //pointer to the current timer
 
const byte upLED        = 13;  //turns on as long as the UP button is pressed
const byte downLED      = 12;  //turns on as long as the DOWN button is pressed
const byte leftLED      = 11;  //toggles on/off
const byte rightLED     = 10;  //toggles on/off
const byte intensityLED =  9;  //a LED which can have its intensity adjusted
 
 
//                           s e t u p ( )
//**********************************************************************
void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); //start receive
 
  pinMode(upLED, OUTPUT);
  pinMode(downLED, OUTPUT);
  pinMode(leftLED, OUTPUT);
  pinMode(rightLED, OUTPUT);
  pinMode(intensityLED, OUTPUT); //this is a PWM output pin
 
} //                    E N D  O F  s e t u p ( )
 
 
 
//                            l o o p ( )
//**********************************************************************
void loop()
{
  if (irrecv.decode(&results)) //is there IR remote button code
  {
    //Serial.println(results.value);
    processButton(); //process button press
    irrecv.resume(); //restart for next button press
  }
 
  //**********************                                //Turn off upLED
  //if timing is enabled, is it time to stop
  if (TimerUpFlag && millis() - TimerUp >= 250ul)
  {
    TimerUpFlag = false; //disable timing
    TimerPtr = &dummy;   //pointer to dummy timer
    digitalWrite(upLED, LOW);
  }
 
  //**********************                                //Turn off downLED
  //if timing is enabled, is it time to stop
  if (TimerDownFlag && millis() - TimerDown >= 250ul)
  {
    TimerDownFlag = false; //disable timing
    TimerPtr = &dummy;     //pointer to dummy timer
    digitalWrite(downLED, LOW);
  }
 
  //**********************                                //LED intensity
  //are we still within the adjustment time
  if (millis() - TimerIntensity <= 300ul)
  {
    //is it time to increase/decrease the intensity
    if (millis() - TimerIncDec >= 200ul)
    {
      TimerIncDec = millis();
 
      if (intesityUpFlag == true) //Increase
      {
        brightness += 5;
        if (brightness > 255)
        {
          brightness = 255;
        }
      }
      else if (intesityDownFlag == true) //Decrease
      {
        brightness -= 5;
        if (brightness < 0)
        {
          brightness = 0;
        }
      }
 
      analogWrite(intensityLED, brightness);
      //Serial.println(brightness); //debug
    }
  }
  //stop increasing/decreasing intensity
  else
  {
    intesityUpFlag = false;
    intesityDownFlag = false;
  }
 
  //************************************
  //Other non blocking code goes here
  //************************************
 
} //                   E N D  O F  l o o p ( )
 
 
 
//======================================================================
//                       F U N C T I O N S
//======================================================================
 
//                   p r o c e s s B u t t o n ( )
//**********************************************************************
//process IR remote button presses
void processButton()
{
  switch (results.value)
  {
    //**********************
    case 0xFF629D:                                           //UP Arrow
      {
        Serial.println("UP");
        TimerPtr = &TimerUp;  //point to this timer
        TimerUpFlag = true;   //enable timing
        digitalWrite(upLED, HIGH);
        TimerUp = millis();
      }
      break;
 
    //**********************
    case 0xFFA857:                                           //DOWN Arrow
      {
        Serial.println("DOWN");
        TimerPtr = &TimerDown;  //point to this timer
        TimerDownFlag = true;   //enable timing
        digitalWrite(downLED, HIGH);
        TimerDown = millis();
      }
      break;
 
    //**********************
    case 0xFF22DD:                                           //LEFT Arrow
      {
        Serial.println("LEFT");
        digitalWrite(leftLED, !digitalRead(leftLED));   //Toggle LED
      }
      break;
 
    //**********************
    case 0xFFC23D:                                           //RIGHT Arrow
      {
        Serial.println("RIGHT");
        digitalWrite(rightLED, !digitalRead(rightLED)); //Toggle LED
      }
      break;
 
    //**********************
    case 0xFF42BD:                                           // * button
      {
        Serial.println("*");
        TimerPtr = &TimerIntensity;  //point to this timer
        intesityUpFlag = true;       //enable intensity up adjustment
        intesityDownFlag = false;
        TimerIncDec = millis();
        TimerIntensity = millis();
      }
      break;
 
    //**********************
   case 0xFF52AD:                                           // # button
      {
        Serial.println("#");
        TimerPtr = &TimerIntensity;  //point to this timer
        intesityDownFlag = true;     //enable intensity down adjustment
        intesityUpFlag = false;
        TimerIncDec = millis();
        TimerIntensity = millis();
      }
      break;
 
    //**********************
    case 0xFF02FD:
      Serial.println("OK");
      break;
 
    //**********************
    case 0xFF6897:
      Serial.println("1");
      break;
 
    //**********************
    case 0xFF9867:
      Serial.println("2");
      break;
 
    //**********************
    case 0xFFB04F:
      Serial.println("3");
      break;
 
    //**********************
    case 0xFF30CF:
      Serial.println("4");
      break;
 
    //**********************
    case 0xFF18E7:
      Serial.println("5");
      break;
 
    //**********************
    case 0xFF7A85:
      Serial.println("6");
      break;
 
    //**********************
    case 0xFF10EF:
      Serial.println("7");
      break;
 
    //**********************
    case 0xFF38C7:
      Serial.println("8");
      break;
 
    //**********************
    case 0xFF5AA5:
      Serial.println("9");
      break;
 
    //**********************
    case 0xFF4AB5:
      Serial.println("0");
      break;
 
    //**********************
    case 0xFFFFFFFF: //Repeat code
      {
        Serial.println("REPEAT");
        *TimerPtr = millis();       //reset the current timer
      }
      break;
 
  } // END switch case
 
} //             E N D  o f  p r o c e s s B u t t o n ( )
 
//**********************************************************************
 
//======================================================================
//                        E N D  O F  C O D E
//======================================================================

LarryD:
Here is an example that uses my IR NEC library.

//**********************************************************************

#include "IRremote.h"
//LarryD

//Sketch to demonstrate using an IR hand remote to control Arduino outputs.
//The remote used here uses the NEC protocol.
//This remote sends the button code (example 0xFF629D) then a repeat code 0xFFFFFFFF
//The repeat code is re-sent as long as an IR remote button is pressed.
//The IR receiver used is the TSOP4838

/*  17 IR button remote layout  Imgur: The magic of the Internet

^
< OK  > 
  v
1  2  3
4  5  6
7  8  9
*  0  #

*/

const byte RECV_PIN = 7;  //IR receive pin

IRrecv irrecv(RECV_PIN);  //create instance of 'IRrecv'
decode_results results;    //create instance of 'decode_results'

unsigned long  TimerUp;  //UP arrow on the remote
boolean        TimerUpFlag = false;

unsigned long  TimerDown; //DOWN arrow on the remote
boolean        TimerDownFlag = false;

unsigned long TimerIntensity;
unsigned long TimerIncDec;
boolean intesityUpFlag  = false;
boolean intesityDownFlag = false;
int brightness;

unsigned long  dummy;
unsigned long * TimerPtr = &dummy; //pointer to the current timer

const byte upLED        = 13;  //turns on as long as the UP button is pressed
const byte downLED      = 12;  //turns on as long as the DOWN button is pressed
const byte leftLED      = 11;  //toggles on/off
const byte rightLED    = 10;  //toggles on/off
const byte intensityLED =  9;  //a LED which can have its intensity adjusted

//                          s e t u p ( )
//**********************************************************************
void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); //start receive

pinMode(upLED, OUTPUT);
  pinMode(downLED, OUTPUT);
  pinMode(leftLED, OUTPUT);
  pinMode(rightLED, OUTPUT);
  pinMode(intensityLED, OUTPUT); //this is a PWM output pin

} //                    E N D  O F  s e t u p ( )

//                            l o o p ( )
//**********************************************************************
void loop()
{
  if (irrecv.decode(&results)) //is there IR remote button code
  {
    //Serial.println(results.value);
    processButton(); //process button press
    irrecv.resume(); //restart for next button press
  }

//**********************                                //Turn off upLED
  //if timing is enabled, is it time to stop
  if (TimerUpFlag && millis() - TimerUp >= 250ul)
  {
    TimerUpFlag = false; //disable timing
    TimerPtr = &dummy;  //pointer to dummy timer
    digitalWrite(upLED, LOW);
  }

//**********************                                //Turn off downLED
  //if timing is enabled, is it time to stop
  if (TimerDownFlag && millis() - TimerDown >= 250ul)
  {
    TimerDownFlag = false; //disable timing
    TimerPtr = &dummy;    //pointer to dummy timer
    digitalWrite(downLED, LOW);
  }

//**********************                                //LED intensity
  //are we still within the adjustment time
  if (millis() - TimerIntensity <= 300ul)
  {
    //is it time to increase/decrease the intensity
    if (millis() - TimerIncDec >= 200ul)
    {
      TimerIncDec = millis();

if (intesityUpFlag == true) //Increase
      {
        brightness += 5;
        if (brightness > 255)
        {
          brightness = 255;
        }
      }
      else if (intesityDownFlag == true) //Decrease
      {
        brightness -= 5;
        if (brightness < 0)
        {
          brightness = 0;
        }
      }

analogWrite(intensityLED, brightness);
      //Serial.println(brightness); //debug
    }
  }
  //stop increasing/decreasing intensity
  else
  {
    intesityUpFlag = false;
    intesityDownFlag = false;
  }

//************************************
  //Other non blocking code goes here
  //************************************

} //                  E N D  O F  l o o p ( )

//======================================================================
//                      F U N C T I O N S
//======================================================================

//                  p r o c e s s B u t t o n ( )
//**********************************************************************
//process IR remote button presses
void processButton()
{
  switch (results.value)
  {
    //**********************
    case 0xFF629D:                                          //UP Arrow
      {
        Serial.println("UP");
        TimerPtr = &TimerUp;  //point to this timer
        TimerUpFlag = true;  //enable timing
        digitalWrite(upLED, HIGH);
        TimerUp = millis();
      }
      break;

//**********************
    case 0xFFA857:                                          //DOWN Arrow
      {
        Serial.println("DOWN");
        TimerPtr = &TimerDown;  //point to this timer
        TimerDownFlag = true;  //enable timing
        digitalWrite(downLED, HIGH);
        TimerDown = millis();
      }
      break;

//**********************
    case 0xFF22DD:                                          //LEFT Arrow
      {
        Serial.println("LEFT");
        digitalWrite(leftLED, !digitalRead(leftLED));  //Toggle LED
      }
      break;

//**********************
    case 0xFFC23D:                                          //RIGHT Arrow
      {
        Serial.println("RIGHT");
        digitalWrite(rightLED, !digitalRead(rightLED)); //Toggle LED
      }
      break;

//**********************
    case 0xFF42BD:                                          // * button
      {
        Serial.println("*");
        TimerPtr = &TimerIntensity;  //point to this timer
        intesityUpFlag = true;      //enable intensity up adjustment
        intesityDownFlag = false;
        TimerIncDec = millis();
        TimerIntensity = millis();
      }
      break;

//**********************
  case 0xFF52AD:                                          // # button
      {
        Serial.println("#");
        TimerPtr = &TimerIntensity;  //point to this timer
        intesityDownFlag = true;    //enable intensity down adjustment
        intesityUpFlag = false;
        TimerIncDec = millis();
        TimerIntensity = millis();
      }
      break;

//**********************
    case 0xFF02FD:
      Serial.println("OK");
      break;

//**********************
    case 0xFF6897:
      Serial.println("1");
      break;

//**********************
    case 0xFF9867:
      Serial.println("2");
      break;

//**********************
    case 0xFFB04F:
      Serial.println("3");
      break;

//**********************
    case 0xFF30CF:
      Serial.println("4");
      break;

//**********************
    case 0xFF18E7:
      Serial.println("5");
      break;

//**********************
    case 0xFF7A85:
      Serial.println("6");
      break;

//**********************
    case 0xFF10EF:
      Serial.println("7");
      break;

//**********************
    case 0xFF38C7:
      Serial.println("8");
      break;

//**********************
    case 0xFF5AA5:
      Serial.println("9");
      break;

//**********************
    case 0xFF4AB5:
      Serial.println("0");
      break;

//**********************
    case 0xFFFFFFFF: //Repeat code
      {
        Serial.println("REPEAT");
        *TimerPtr = millis();      //reset the current timer
      }
      break;

} // END switch case

} //            E N D  o f  p r o c e s s B u t t o n ( )

//**********************************************************************

//======================================================================
//                        E N D  O F  C O D E
//======================================================================

I check the code , It's well written and professional .

I almost understood everything except this condition if (TimerDownFlag && millis() - TimerDown >= 250ul) and the use of pointer .

Thanks for your quick response :slight_smile:

That line turns off the LED after 1/4 second (after no pushing).

The pointer is used to access 'different' timing variables as needed.

.

Thank you very much :slight_smile: :slight_smile: