Take a new value,ONLY when the values changes

Hy ! :slight_smile:

This is my first post about arduino, so hello everyone !

I'm trying to display the string of the buttons of my IR remote.
When I push "POWER" for exemple it shows "POWER" (on the arduino serial monitor),SO it's working.

But, I want to shows the string only when I decide.

SO I wrote that it only shows the values when I enter ok.

BUT It keep the value of the first button I pressed. Let's imagine I press first "POWER" but i don't want to display it, so I repressed another button like "1" and I enter "ok". The problem is that in this case , it won't show "1" but "POWER".

How can program in order to continually receiving the latest value of the button ? SO If press 10 buttons and I enter ok after the 10th, it shows the value of the 10th buttton.(And not the first)

#include "IRremote.h"

char userInput;

int receiver = 11; // Signal Pin of IR receiver to Arduino Digital Pin 11

/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

/*-----( Function )-----*/
void translateIR() // takes action based on IR code received

// describing Remote IR codes 

{

  switch(results.value)

  {
  case 0xFFA25D: Serial.println("POWER"); break;
  case 0xFFE21D: Serial.println("FUNC/STOP"); break;
  case 0xFF629D: Serial.println("VOL+"); break;
  case 0xFF22DD: Serial.println("FAST BACK");    break;
  case 0xFF02FD: Serial.println("PAUSE");    break;
  case 0xFFC23D: Serial.println("FAST FORWARD");   break;
  case 0xFFE01F: Serial.println("DOWN");    break;
  case 0xFFA857: Serial.println("VOL-");    break;
  case 0xFF906F: Serial.println("UP");    break;
  case 0xFF9867: Serial.println("EQ");    break;
  case 0xFFB04F: Serial.println("ST/REPT");    break;
  case 0xFF6897: Serial.println("0");    break;
  case 0xFF30CF: Serial.println("1");    break;
  case 0xFF18E7: Serial.println("2");    break;
  case 0xFF7A85: Serial.println("3");    break;
  case 0xFF10EF: Serial.println("4");    break;
  case 0xFF38C7: Serial.println("5");    break;
  case 0xFF5AA5: Serial.println("6");    break;
  case 0xFF42BD: Serial.println("7");    break;
  case 0xFF4AB5: Serial.println("8");    break;
  case 0xFF52AD: Serial.println("9");    break;
  case 0xFFFFFFFF: Serial.println(" REPEAT ");break;  

  default: 
    Serial.println(" other button   ");

  }// End Case

  delay(500); // Do not get immediate repeat


} //END translateIR
void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode"); 
  irrecv.enableIRIn(); // Start the receiver

}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    if(Serial.available()> 0)
    {
      userInput=Serial.read();
      if(userInput == 'ok')
        {
          translateIR(); 
          irrecv.resume();// receive the next value
        }
    } 
   }  
}/* --(end main loop )-- */

And I don't want to press after that I have enter ok, I want my last value recorded, and when I enter ok it shows the value.

Thanks a lot, I hope that someone will replie :slight_smile:

whoops...

if(userInput == 'ok')

I think you misread, I wrote this ! The problem is about the value that I show.

Your program will never show anything.

What the other OP's are saying is that this line:

if(userInput == 'ok')

is an error, because you used single quotes. Serial.read() only reads a single character, so how can it be an 'o' and a 'k'? The only way to read multiple characters is to either build a string and use strcmp() when you're done, or use another Serial method to read everything at once, add a NULL to the end and them compare it.

Maybe have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

My bad, I was using the letter "g" the whole time but I changed it to make it more clear :confused:

Sorry !

void loop()
{
  if (irrecv.decode(&results))
  {
    if(Serial.available()> 0)
    {
      userInput=Serial.read();
      if(userInput == 'g')
        {
          translateIR(); 
          irrecv.resume();
        }
    } 
   }  
}

I feel dumb for this mistake, but my problem is definitely not resolved ! I reexplained : If press on "POWER" then on "PAUSE" and if I enter ok, it shows "POWER". But I want to show the last button that I have pressed, so "PAUSE".

Please my issue is absolutely not resolved ! :cry: :cry: :cry:

Move you serial testing outside the if where you test if a remote command was issued.

Code should be

Loop()
If there is an IR command, save its description in a label variable and get ready for next one
If I receive 'g' from Serial then display the label if there is one. (And possibly empty it)

Those if are NOT nested

I am not familiar with the library, but from the name of the function it looks like the code will only look for a new value after

         irrecv.resume();

If that is the case then the function needs to be called each time through loop() when a value has been received and not just when you enter 'g'

Thanks a lot it’s working like a charm ! :slight_smile:

#include "IRremote.h"
int data;
char userInput;
int receiver = 11; 

IRrecv irrecv(receiver);    
decode_results results;      


void translateIR()
 

{

  switch(data)

  {
  case 0xFFA25D: Serial.println("POWER"); break;
  case 0xFFE21D: Serial.println("FUNC/STOP"); break;
  case 0xFF629D: Serial.println("VOL+"); break;
  case 0xFF22DD: Serial.println("FAST BACK");    break;
  case 0xFF02FD: Serial.println("PAUSE");    break;
  case 0xFFC23D: Serial.println("FAST FORWARD");   break;
  case 0xFFE01F: Serial.println("DOWN");    break;
  case 0xFFA857: Serial.println("VOL-");    break;
  case 0xFF906F: Serial.println("UP");    break;
  case 0xFF9867: Serial.println("EQ");    break;
  case 0xFFB04F: Serial.println("ST/REPT");    break;
  case 0xFF6897: Serial.println("0");    break;
  case 0xFF30CF: Serial.println("1");    break;
  case 0xFF18E7: Serial.println("2");    break;
  case 0xFF7A85: Serial.println("3");    break;
  case 0xFF10EF: Serial.println("4");    break;
  case 0xFF38C7: Serial.println("5");    break;
  case 0xFF5AA5: Serial.println("6");    break;
  case 0xFF42BD: Serial.println("7");    break;
  case 0xFF4AB5: Serial.println("8");    break;
  case 0xFF52AD: Serial.println("9");    break;
  case 0xFFFFFFFF: Serial.println(" REPEAT");break;  

  default: 
    Serial.println(" other button   ");

  }

  delay(500); 


}
void setup()
{
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode"); 
  irrecv.enableIRIn();

}

void showData()
{
  if(Serial.available()>0)
  {
    userInput=Serial.read();
    if(userInput=='g')
    {
      translateIR();
      irrecv.resume();
    }
  }
}

void loop()
{
  if(irrecv.decode(&results))
  {
  data=results.value;
  Serial.print(data);
  irrecv.resume();
  }
  
showData();
 
}

I add the serial print to understand how it’s working :slight_smile:
I add a picture of my serial monitor for the interested ones !

You need to have irrecv.resume(); only once you have read the IR, not in the serial test

data needs to be of the same type as what you store into it... I don't think it's an int... (read I'm sure it's not) that's why you see negative numbers in your terminal