Noob C questions

Hello and thanks fof looking in advance.
I am not sure if i have found a bug or just an issue with my programming.

Code in question

#include <IRremote.h>

int RECV_PIN = 4;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long lastCode;
const int ledPin2 =  LED_BUILTIN;// the number of the LED pin
int ledState2 = LOW;     
void setup()
{
  Serial.begin(115200);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(ledPin2, OUTPUT);
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    Serial.println(results.value, DEC);
//    lastCode = results.value;
      // BELOW IS REPEAT CODE ACTION
   if (results.value == 4294967295) {
      results.value = lastCode;}
          // ABOVE IS REPEAT CODE ACTION
   if (results.value == 550720173) {
    Serial.println("Small Power button press");
    lastCode = results.value;}
   if (results.value == 550726293) {
      Serial.println("Other button 9 press");
      if (ledState2 == LOW) {
        ledState2 = HIGH;
      } else {
        ledState2 = LOW;
      }
      lastCode = results.value;
      digitalWrite(ledPin2, ledState2);
    }

      

    irrecv.resume(); // Receive the next value
  }
  
}

If i use the remote (NEC IR chip) destined for this code it works fine. The repeat code does what its suppose to however if i use a different remote that uses the NEC chip IR chip as well to control volume the 0xFFFFFFFF repeat code gets sent and the arduino responds even though it shouldn’t.

Output from correct remote single press:

20D352AD
550720173
Small Power button press
20D36A95
550726293
Other button 9 press

Output from correct remote multiple press:

20D352AD
550720173
Small Power button press
20D36A95
550726293
Other button 9 press
20D36A95
550726293
Other button 9 press
FFFFFFFF
4294967295
Other button 9 press
FFFFFFFF
4294967295
Other button 9 press

Output from INCORRECT remote multiple press:

8C23D
574013
FFFFFFFF
4294967295
Other button 9 press
FFFFFFFF
4294967295
Other button 9 press
FFFFFFFF
4294967295
Other button 9 press

The last code that was triggered by the correct remote gets triggered again with the incorrect remote because they both use the same IR encoding chip that uses fffff for repeat.

how can i fix it?

I did come up with this idea whilst i was writing this.
How can i make my code repeat the loop ( if (results.value == 550720173) {
Serial.println(“Small Power button press”) ) x amount of time depending how long its pressed for. Then i could in theory remove the “Repeat code action.”

Thanks again for looking at my post.

Don’t you want to assign to lastCode every time you receive a new code, not just when it happens to match something?

#include <IRremote.h>

int RECV_PIN = 4;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long lastCode;
const int ledPin2 =  LED_BUILTIN;// the number of the LED pin
int ledState2 = LOW;
void setup()
{
  Serial.begin(115200);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(ledPin2, OUTPUT);
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    Serial.println(results.value, DEC);
    //    lastCode = results.value;
    // BELOW IS REPEAT CODE ACTION
    if (results.value == 4294967295) {
      results.value = lastCode;
    } else {
      lastCode = results.value;
    }
    // ABOVE IS REPEAT CODE ACTION
    if (results.value == 550720173) {
      Serial.println("Small Power button press");
      //lastCode = results.value;
    }
    if (results.value == 550726293) {
      Serial.println("Other button 9 press");
      if (ledState2 == LOW) {
        ledState2 = HIGH;
      } else {
        ledState2 = LOW;
      }
      //lastCode = results.value;
      digitalWrite(ledPin2, ledState2);
    }

    irrecv.resume(); // Receive the next value
  }
}
   if (results.value == 4294967295) 
   {
      results.value = lastCode;   <———<<<<   very odd  ;-)
   }

Learn about formatting your code with the key combination

blh64:
Don’t you want to assign to lastCode every time you receive a new code, not just when it happens to match something?

#include <IRremote.h>

int RECV_PIN = 4;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long lastCode;
const int ledPin2 =  LED_BUILTIN;// the number of the LED pin
int ledState2 = LOW;
void setup()
{
 Serial.begin(115200);
 irrecv.enableIRIn(); // Start the receiver
 pinMode(ledPin2, OUTPUT);
}

void loop() {
 if (irrecv.decode(&results)) {
   Serial.println(results.value, HEX);
   Serial.println(results.value, DEC);
   //    lastCode = results.value;
   // BELOW IS REPEAT CODE ACTION
   if (results.value == 4294967295) {
     results.value = lastCode;
   } else {
     lastCode = results.value;
   }
   // ABOVE IS REPEAT CODE ACTION
   if (results.value == 550720173) {
     Serial.println(“Small Power button press”);
     //lastCode = results.value;
   }
   if (results.value == 550726293) {
     Serial.println(“Other button 9 press”);
     if (ledState2 == LOW) {
       ledState2 = HIGH;
     } else {
       ledState2 = LOW;
     }
     //lastCode = results.value;
     digitalWrite(ledPin2, ledState2);
   }

irrecv.resume(); // Receive the next value
 }
}

Yes thank you so much. It works as it should. ish
All though I don’t understand how its now working as there is no reference to “lastCode” any more. I’m sure there is something very simple I have over looked. Maybe its referenced in “IRremote.h”

when I press the power button with your code it shows up twice in the serial console and the second instance is all ready printing FFFFFFFF
I just tried adding a delay with Millis with no success.

Here is new code:

#include <IRremote.h>

int RECV_PIN = 4;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long lastCode;
const int ledPin2 =  LED_BUILTIN;// the number of the LED pin
int ledState2 = LOW;
unsigned long startMillis;
const unsigned long period = 20000;
bool timerRunning = false;
void setup()
{
  Serial.begin(115200);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(ledPin2, OUTPUT);
}

void loop() {
  if ( timerRunning ) {
    if ( millis() - startMillis >= period ) {
      timerRunning = false;
    }
  }
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    Serial.println(results.value, DEC);
    // BELOW IS REPEAT CODE ACTION
    if (results.value == 4294967295) {
      results.value = lastCode;
    } else {
      lastCode = results.value;
    }
    // ABOVE IS REPEAT CODE ACTION
    if (results.value == 550720173) {
      Serial.println("Small Power button press");
      startMillis = millis();
      timerRunning = true;
    }
    if (results.value == 550726293) {
      Serial.println("Other button 9 press");
      if (ledState2 == LOW) {
        ledState2 = HIGH;
      } else {
        ledState2 = LOW;
      }
      digitalWrite(ledPin2, ledState2);
    }

    irrecv.resume(); // Receive the next value
  }
}

And output after button is pressed once. What is strange is it didn’t do that before

20D352AD
550720173
Small Power button press
FFFFFFFF
4294967295
Small Power button press

larryd:

   if (results.value == 4294967295) 

{
      results.value = lastCode;  <———<<<<  very odd  :wink:
  }





Learn about formatting your code with the key combination <CTRL><T>

Ok I’m starting to see now why you would say that.
In my defence I am only using what I’ve self taught over the years looking at other ppls code.

No problem, it takes time.

@sparkie37

Your sketch in post #3 still has:

if (results.value == 4294967295)
{
results.value = lastCode; // <------<<<< No
}

The sample code below demonstrates a few ideas that may be of interest to you.
Notice the repeat function is demonstrated also.

The NEC key pad may be different than yours, but you should be able to convert to your values for your keypad.

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

#include "IRremote.h"

//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 = 2;   //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 >= 250)
  {
    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 >= 250)
  {
    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 <= 300)
  {
    //is it time to increase/decrease the intensity
    if (millis() - TimerIncDec >= 200)
    {
      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
//======================================================================

In this modified sketch from post #3, what is not working as you want?

//Version 1

#include <IRremote.h>

int RECV_PIN = 4;                         //sparkie37 remote
//int RECV_PIN = 2;                       //LarryD Remote

IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long lastCode;

const int ledPin2 =  LED_BUILTIN;// the number of the LED pin
int ledState2 = LOW;


//***********************************************************************
void setup()
{
  Serial.begin(115200);
  // Start the receiver
  irrecv.enableIRIn(); 
  
  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin2, ledState2);
}

//***********************************************************************
void loop()
{
  if (irrecv.decode(&results))
  {
    Serial.println(results.value, HEX);
    Serial.println(results.value, DEC);

    //******************************************
    // BELOW IS REPEAT CODE ACTION
    //if (results.value != 0xFFFFFFFF)        
    {
      lastCode = results.value;
    }

    //******************************************
    if (lastCode == 0x20D352AD)      //550720173 = 0x20D352AD  "sparkie37 remote"
    //if (lastCode == 0xFF629D)      //LarryD Remote
    {
      Serial.println("Small Power button press");
    }

    //******************************************
    if (lastCode == 0x20D36A95)      //550726293 = 0x20D36A95  "sparkie37 remote"
    //if (lastCode == 0xFFA857)      //LarryD Remote
    {
      Serial.println("Other button 9 press");
      if (ledState2 == LOW)
      {
        ledState2 = HIGH;
      }
      
      else
      {
        ledState2 = LOW;
      }
      
      digitalWrite(ledPin2, ledState2);
    }

    // Receive the next value
    irrecv.resume(); 
    
  } //END of   if (irrecv.decode(&results))
  
} //END of loop()

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

larryd:
In this modified sketch from post #3, what is not working as you want?

//Version 1

#include <IRremote.h>

int RECV_PIN = 4;                        //sparkie37 remote
//int RECV_PIN = 2;                      //LarryD Remote

IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long lastCode;

const int ledPin2 =  LED_BUILTIN;// the number of the LED pin
int ledState2 = LOW;

//***********************************************************************
void setup()
{
  Serial.begin(115200);
  // Start the receiver
  irrecv.enableIRIn();
 
  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin2, ledState2);
}

//***********************************************************************
void loop()
{
  if (irrecv.decode(&results))
  {
    Serial.println(results.value, HEX);
    Serial.println(results.value, DEC);

//******************************************
    // BELOW IS REPEAT CODE ACTION
    //if (results.value != 0xFFFFFFFF)       
    {
      lastCode = results.value;
    }

//******************************************
    if (lastCode == 0x20D352AD)      //550720173 = 0x20D352AD  “sparkie37 remote”
    //if (lastCode == 0xFF629D)      //LarryD Remote
    {
      Serial.println(“Small Power button press”);
    }

//******************************************
    if (lastCode == 0x20D36A95)      //550726293 = 0x20D36A95  “sparkie37 remote”
    //if (lastCode == 0xFFA857)      //LarryD Remote
    {
      Serial.println(“Other button 9 press”);
      if (ledState2 == LOW)
      {
        ledState2 = HIGH;
      }
     
      else
      {
        ledState2 = LOW;
      }
     
      digitalWrite(ledPin2, ledState2);
    }

// Receive the next value
    irrecv.resume();
   
  } //END of  if (irrecv.decode(&results))
 
} //END of loop()

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

It responds as if I pressed and held down the power button when in reality on have only pressed it once.

If you have some time, suggest you review the sample code in post #6 for some new techniques.

Tell us ‘exactly’ how you want things to work in your sketch.

//Version 2

#include <IRremote.h>

int RECV_PIN = 4;
const unsigned long powerButton  = 0x20D352AD;
const unsigned long otherButton  = 0x20D36A95;

//int RECV_PIN = 2;
//const unsigned long powerButton = 0xFF629D;
//const unsigned long otherButton = 0xFFA857;

IRrecv irrecv(RECV_PIN);
decode_results results;

const int ledPin2 =  LED_BUILTIN;
int ledState2     = LOW;

//***********************************************************************
void setup()
{
  Serial.begin(115200);
  irrecv.enableIRIn();

  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin2, ledState2);

}

//***********************************************************************
void loop()
{
  if (irrecv.decode(&results))
  {
    Serial.println(results.value, HEX);
    Serial.println(results.value, DEC);
    
    //******************************************
    switch (results.value)
    {
      //****************
      case 0xFFFFFFFF:
        {
        }
        break;

      //****************
      case powerButton:
        {
          Serial.println("Small Power button press");
        }
        break;

      //****************
      case otherButton:
        {
          Serial.println("Other button 9 press");
          ledState2 = !ledState2;

          digitalWrite(ledPin2, ledState2);
        }
        break;
    }

    //******************************************
    // Receive the next value
    irrecv.resume();

  } //END of   if (irrecv.decode(&results))

} //END of loop()

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