How to enter a multi-digit value from an IR remote and store the final value

since I expect I can enter the value '11', '22', 33' etc., and this would obviously imply 2 presses

What happens in-between when you press 1, 1, 1 ?

There will either be a 'no key', or 'key-released' message from the remote, or you can implement a timeout to clear prevKey (e.g. 100mS) ... then the next press will register as a newKey.

lastchancename:

since I expect I can enter the value '11', '22', 33' etc., and this would obviously imply 2 presses

What happens in-between when you press 1, 1, 1 ?

There will either be a 'no key', or 'key-released' message from the remote, or you can implement a timeout to clear prevKey (e.g. 100mS) ... then the next press will register as a newKey.

For my remote:
short press, it does not send any value other than the button value,
Long press, it sends 0xFFFFFFFF after the button value.

I've spent the morning testing, reconnecting, and re-writing the code.
I rewrote for a 2-digit final number to keep it simple, planning to restore to 3-digit when all working properly.

It seems that at the point I came, I need a debouncing for my remote, as I can get all 2-digit presses accumulated correctly at almost any attempt.

The newKey!=prevKey statement stopped the accumulation (i-e- I could only register 1 digit),
so I replaced this line with
if ( index < MaxChars ), which allows me to see the 2-digit complex number.

Increasing the delay time from 100 to 160 after the IRrecv helped a bit.

I even tried to add a flag bool, that was 'true' after IRrecv, then 'false' before each case's break. Without luck.

I don't expect to have someone to finish my code, but my lack of experience makes me stop at this point.
So any hint is more than welcome.

My updated (almost working) code, for 2-digit accumulation,
follows:

#include <IRremote.h>
/*
   Variables
*/
int RECV_PIN = 11;
const int MaxChars = 2;
char strValue[MaxChars + 1];   // String for 3 digits + null char
char ch;
int index = 0;
int accumulVal = 0;
int newKey = 0;
int prevKey = 0;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  Serial.begin(9600);
  // In case the interrupt driver crashes on setup, give a clue
  // to the user what's going on.
  Serial.println("Enabling IRin");
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Enabled IRin");
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, DEC);
    irrecv.resume(); // Receive the next value
  }
  delay(150);

  newKey = results.value;

  // switch / case function

  if ( index < MaxChars ) {   // check if a key and different than previous key
    switch (newKey)
    {
      case 0x20DF8877 :  // Button '1' on my remote
        strValue[index++] = '1';
        break;   // assign number to button press

      case 0x20DF48B7 :  // Button '2' on my remote
        strValue[index++] = '2';
        break;

      case 0x20DFC837 :  // Button '3' on my remote
        strValue[index++] = '3';
        break;

      case 0x20DF28D7 :  // Button '4' on my remote
        strValue[index++] = '4';
        break;

      case 0x20DFA857 :  // Button '5' on my remote
        strValue[index++] = '5';
        break;

      case 0x20DF6897 :  // Button '6' on my remote
        strValue[index++] = '6';
        break;

      case 0x20DFE817 :  // Button '7' on my remote
        strValue[index++] = '7';
        break;

      case 0x20DF18E7 :  // Button '8' on my remote
        strValue[index++] = '8';
        break;

      case 0x20DF9867 :  // Button '9' on my remote
        strValue[index++] = '9';
        break;

      case 0x20DF08F7 :  // Button '0' on my remote
        strValue[index++] = '0';
        break;

      case 0xFFFFFFFF : // Key depressed long
        prevKey = 0;    // clear
        break;

    }  // end of cases

  }


  /*
     Accumulation
  */


  else
  {
    // here when the buffer is full or on the first non digit
    strValue[index] = 0;          // terminate the string with a 0
    accumulVal = atoi(strValue);  // use atoi to convert string to an int
    index = 0;                    // reset index to receive other data
  }

  Serial.print("Accumulated value: ");
  Serial.println(accumulVal, DEC);

} // loop end

The delay() after receiving a key may be masking the FFFFFF immediately following a valid key, so see if you can implement the delay() only after FFFFFF, and after 'repeated' key values.

If you're still working on this project, I'm also new and willing to bounce ideas around to learn. Let me know.

I ended up with this code. It work perfect for me. You can select to use OK button (if you have one) as a termination, or you can use cmdCount to terminate command on 1, 2, 3 +++ number of commands.

#include <IRremote.h>

const int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;
String cmd = ""; // Decoded command received
int cmdCount = 0; // Number(count) of command received
int cmdEnd = 0; // Mark command end - OK pressed

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

}

void loop(){
if (irrecv.decode(&results)){

    if (results.value == 0XFFFFFFFF)
      results.value = 0;    //Truncate repeted key press

    switch(results.value){
      case 0x219E8877:    //0
      cmdCount++;
      cmd = cmd + "0";
      break ;  
      case 0x219E18E7:    //1
      cmdCount++;
      cmd = cmd + "1";
      break ;
      case 0x219E9867:    //2
      cmdCount++;
      cmd = cmd + "2";
      break ;
      case 0x219E708F:    //3
      cmdCount++;
      cmd = cmd + "3";
      break ;
      case 0x219E38C7:    //4
      cmdCount++;
      cmd = cmd + "4";
      break ;
      case 0x219EB847:    //5
      cmdCount++;
      cmd = cmd + "5";
      break ;
      case 0x219E7A85:    //6
      cmdCount++;
      cmd = cmd + "6";
      break ;
      case 0x219EBA45:    //7
      cmdCount++;
      cmd = cmd + "7";
      break ;
      case 0x219E3AC5:    //8
      cmdCount++;
      cmd = cmd + "8";
      break ;
      case 0x219EFA05:    //9
      cmdCount++;
      cmd = cmd + "9";
      break ;
      case 0x219E10EF:    //OK pressed
      Serial.println("End");
      Serial.println(cmd);
      Serial.println(cmdCount);
      cmd = "";
      cmdCount = 0;  
    }
    
    irrecv.resume(); 

}
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.