Multidigit Input from IR receiver to Arduino

In my remote I type 4 buttons, and arduino should combine all the four together and give me one result in the serial monitor.

Lets say I type "OK", '1', '5', '8', '2'

Arduino should give me this result in the Serial Monitor "OK1582" (As A Single Value)

Can someone help me with the code for LG REMOTES?

You are going to have to write a sketch to count the digits (say 4 total) and store them in an array as digit[0], digit[1], digit[2], digit[3]. Therefore you display results would be:

digit[0] *1000 + digit[1] *100 + digit[2] *10 + digit[3]

display the results.

You could also play with Strings, but numbers are safer to code.

what about entering OK last rather than assuming 4 numbers have been entered correctly -- 1582 OK

decimal values can easily be accumulated by multiplying the current value by 10 and adding the next corresponding input value.

that value should be reset to zero after the cmd is processed. is there a command to ignore?

Isn't that five buttons? Or did you mean to say "In my remote I press the 'OK' button followed by exactly 4 digit buttons".

To be honest I am beginner to codes...

Usually, one does an Internet search, finds some examples, and attempts to implement their own sketch to perform whatever they wish....

Arduino LG remote IR codes - Search (bing.com)

... write some code that prints the codes when received

i'm curious, what frequency do these IR receivers use. i think there's 38 kHz and at least one other

The "Sony" ones I worked with years ago were 38K-40K
Understanding Sony IR remote codes-LIRC_files-Arduino_library.pdf (1.2 MB)

From a Sony IR test on Mega 10 years ago:

/*
  IR remote control (Sony) detection for Arduino, M. Burnette
  Binary sketch size: 3,642 bytes (of a 32,256 byte maximum)
  
  ☺  20121230 MRB modifications to adapt to numeric input, avoid dupes,
      and to generally "behave" consistently
  ☺  Used with Electronic Goldmine IR detector MIM 5383H4
      http://www.goldmine-elec-products.com/prodinfo.asp?number=G16737
  ☺  IR detector:
      Pin 1: To pin 2 on Arduino
      Pin 2: GND
      Pin 3: 5V through 33 Ohm resistor
  
  ☺  This is based on pmalmsten's code found on the Arduino forum from 2007:
      http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1176098434/0

*/

int irPin     = 2;       //Sensor pin 1 wired to Arduino's pin 2
int statLED   = 13;      //Toggle the status LED every time Power is pressed
int start_bit = 2200;    //Start bit threshold (Microseconds)
int bin_1     = 1000;    //Binary 1 threshold (Microseconds)
int bin_0     = 400;     //Binary 0 threshold (Microseconds)

void setup() {
  pinMode(statLED, OUTPUT);
  digitalWrite(statLED, LOW);

  pinMode(irPin, INPUT);

  Serial.begin(9600);
  Serial.println("IR/Serial Initialized: ");
}

void loop() {
  int key = getIRKey();   //Fetch the key
  
  if(key != 0)            //Ignore keys that are zero
  {
    switch(key)
    {
      case 128: Serial.println("1"); break;
      case 129: Serial.println("2"); break;
      case 130: Serial.println("3"); break;
      case 131: Serial.println("4"); break;
      case 132: Serial.println("5"); break;
      case 133: Serial.println("6"); break;
      case 134: Serial.println("7"); break;
      case 135: Serial.println("8"); break;
      case 136: Serial.println("9"); break;
      case 137: Serial.println("0"); break;
      
      case 144: Serial.println("CH Up"); break;
      case 145: Serial.println("CH Down"); break;
      case 146: Serial.println("VOL Right"); break;
      case 147: Serial.println("VOL Left"); break;
      case 148: Serial.println("Mute"); break;
      case 165: Serial.println("AV/TV"); break;
      case 149: Serial.println("Power");
        //This toggles the statLED every time power button is hit
        if(digitalRead(statLED) != 1)
          digitalWrite(statLED, HIGH);
        else
          digitalWrite(statLED, LOW);
        break;

      //default: Serial.println(key); // for inspection of keycode
    }

    delay(500);    // avoid double key logging (adjustable)
  }
}

int getIRKey() {
  int data[12];
  int i;

  while(pulseIn(irPin, LOW) < start_bit); //Wait for a start bit
  
  for(i = 0 ; i < 11 ; i++)
    data[i] = pulseIn(irPin, LOW);      //Start measuring bits, I only want low pulses
  
  for(i = 0 ; i < 11 ; i++)             //Parse them
  {	    
    if(data[i] > bin_1)                 //is it a 1?
      data[i] = 1;
    else if(data[i] > bin_0)            //is it a 0?
      data[i] = 0;
    else
      return -1;                        //Flag the data as invalid; I don't know what it is! Return -1 on invalid data
  }

  int result = 0;
  for(i = 0 ; i < 11 ; i++)             //Convert data bits to integer
    if(data[i] == 1) result |= (1<<i);

  return result;                        //Return key number
} 




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