1

I need to recognize a signal from an IR remote, not to use a library for working with IR remotes/receivers. How can when the button is clicked to display its name. I have PIC12F615 microcontroller with this code: code and 5 buttons connected to him and I need recognise which button pressed. I have some code but it gives different hex codes if I pressed the same button.

byte IRpin = 2;

volatile boolean remote = false;
volatile unsigned long irCode = 0;

void remoting ()
{
  if ( remote )
  {
    remote = false;
    unsigned long T;
    for ( byte n = 0; n < 32; n ++ )
    {
      do
      {
        T = pulseIn ( IRpin, HIGH, 2200 );
      }
      while ( T < 64 );
      bitWrite ( irCode, n, T > 1120 );
    }
  }
}

void setup ()
{
  Serial.begin ( 9600 );
  Serial.println ( "\n\tReady for keyboard reading!\n" );
  pinMode ( IRpin, INPUT_PULLUP );
  attachInterrupt ( digitalPinToInterrupt ( IRpin ), remoting, FALLING );
}

void loop ()
{
  if (irCode)
  {
    Serial.println (irCode, HEX);
    irCode = 0;
  }
  delay (40);
  remote = true;
}

Schema: enter image description here

11
  • 1
    the remote control may be outputting changing code
    – jsotola
    May 5 at 15:30
  • and how can I make it so that when the button is clicked to display its name
    – Good York
    May 5 at 16:29
  • please do not ask question in a comment ... edit your post and add your question
    – jsotola
    May 5 at 16:55
  • Do you have a PIC12F615 as in your question, or a Uno as in the tags and the schematic?
    – Nick Gammon
    May 5 at 22:33
  • You appear to be using pulseIn() in an interrupt service routine against the same pin which is used to initiate the interrupt. This is certainly odd. pulseIn() may be unreliable in an ISR and you should anyway not block an ISR. It is certainly a novel solution to attempt to interpret what appears to be NEC IR protocol generated by that PIC.
    – 6v6gt
    May 6 at 3:31

1 Answer 1

1

The function pulseIn() has this comment in the source code: "This function performs better with short pulses in noInterrupt() context" see: https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/wiring_pulse.c. It is not clear if your code violates the "short pulse" rule, however, blocking an Interrupt Service Routine (in your case, the external interrupt call back routine remoting() ) for the entire length of an IR data stream is certain to cause other problems, for example, with the millis() timer.

This is in the style of your code but avoids using interrupts. It has been tested against an NEC remote control and gives repeatable results. Your code fails the same test.

byte IRpin = 2;

unsigned long irCode = 0;

void setup ()
{
  Serial.begin ( 9600 );
  Serial.println ( "\n\tReady for keyboard reading!\n" );
  pinMode ( IRpin, INPUT_PULLUP );
}

void loop () {
  while ( digitalRead ( IRpin ) );  // wait until start of first pulse
  unsigned long T;
  uint8_t validPulses = 0 ;
  for ( byte n = 0; n < 36 ; n ++ ) // enough to drop long header
  {
    T = pulseIn ( IRpin, HIGH , 12000 );  // 12ms timeout
    if ( T < 4000 && T > 400 && validPulses < 32 ) {
      // valid pulse
      bitWrite ( irCode, validPulses++, T > 1120 );
    }
  }
  if (irCode > 0 ) Serial.println (irCode, HEX);
  else Serial.println ("no code");
  irCode = 0 ;
}

3
  • is it possible to do this through the interrupt on the pin or timer interrupt?
    – Good York
    May 6 at 11:15
  • @GoodYork Indeed there are and the one I would recommend in your case is to use the input capture mode of Timer 1. That would be non blocking, if done carefully. Does this exercise come with any other constraints such as which pin to use or that the use of pulseIn() is mandatory ? If so, add these all in to your original question. Anyway, with what you have got, you should be able to verify if the PIC is working.
    – 6v6gt
    May 6 at 11:31
  • Task: When writing a program, it is NOT allowed to use libraries for working with IR remotes/receivers. The signal from the receiver should be processed using an external interrupt. Your code works, here i have a code that works through timer 1 but it doesn't work
    – Good York
    May 6 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.