Arduino IR receiver with interrupt

Hi,

I was trying to program an Arduino IR receiver, i found an IR library on the internet

But i found in the examples, that it uses polling method to get data from the IR sensor.

I am trying to program it using interrupt mode, where the Arduino will get interrupted whenever there is any data available for read from the IR sensor.

Can anyone guide me how to do it.

I am trying to program it using interrupt mode, where the Arduino will get interrupted whenever there is any data available for read from the IR sensor.

The first thing that occurs to me is whether you actually need to use an interrupt in the first place. What else is going on in the program that would preclude the use of polling instead of an interrupt ? Seeing your code would help considerably.

The code will control animations of multiple LEDs, there are multiple animation in the code, what i want is on press of a button from my remote, the arduino will change the animation mode.

I guess that you are using delay() and for loops to achieve the animations, but you have not posted your code, so who knows.

If you are using delay() and for loops then you need to get rid of them so that you can read the IR sensor each time through loop().

Have a look at Several things at the same time for examples of how to use millis() for timing.

#define G 9
#define B 10
#define R 11
#define interruptPin 2
#define alternate_delay 100
#define rand_delay 200

int mode;
void setup() {
  mode=1;
  pinMode(interruptPin, INPUT);
  //pinMode(R, OUTPUT);
  //pinMode(G, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(interruptPin), mode_change_isr, RISING);
}

void pwm_anim()
{
  // fade in from min to max in increments of 5 points:
  analogWrite(R, 0);
  analogWrite(G, 0);
  analogWrite(B, 0);
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
      analogWrite(R, fadeValue);
    // wait for 40 milliseconds to see the dimming effect
    delay(40);
  }
  
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
      analogWrite(G, fadeValue);
    // wait for 40 milliseconds to see the dimming effect
    delay(40);
  }
  
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
      analogWrite(B, fadeValue);
    // wait for 40 milliseconds to see the dimming effect
    delay(40);
  }
  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
      analogWrite(R, fadeValue);
    // wait for 40 milliseconds to see the dimming effect
    delay(40);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
      analogWrite(G, fadeValue);
    // wait for 40 milliseconds to see the dimming effect
    delay(40);
  }
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
      analogWrite(B, fadeValue);
    // wait for 40 milliseconds to see the dimming effect
    delay(40);
  }
}

void alternate_flash()
{
    digitalWrite(R, HIGH);
    delay(alternate_delay);
    digitalWrite(R, LOW);
    digitalWrite(G, HIGH);
    delay(alternate_delay);
    digitalWrite(G, LOW);
    digitalWrite(B, HIGH);
    delay(alternate_delay);
    digitalWrite(B, LOW);
}

void two_color_anim()
{
   for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
   // sets the value (range from 0 to 255):
     analogWrite(R, fadeValue);
     analogWrite(G, fadeValue);
   // wait for 40 milliseconds to see the dimming effect
   delay(40);
  }
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
      analogWrite(R, fadeValue);
      analogWrite(G, fadeValue);
    // wait for 40 milliseconds to see the dimming effect
    delay(40);
  }
   for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
   // sets the value (range from 0 to 255):
     analogWrite(R, fadeValue);
     analogWrite(B, fadeValue);
   // wait for 40 milliseconds to see the dimming effect
   delay(40);
  }
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
      analogWrite(R, fadeValue);
      analogWrite(B, fadeValue);
    // wait for 40 milliseconds to see the dimming effect
    delay(40);
  }
   for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
   // sets the value (range from 0 to 255):
     analogWrite(B, fadeValue);
     analogWrite(G, fadeValue);
   // wait for 40 milliseconds to see the dimming effect
   delay(40);
  }
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
      analogWrite(B, fadeValue);
      analogWrite(G, fadeValue);
    // wait for 40 milliseconds to see the dimming effect
    delay(40);
  }
}

void loop()
{
  interrupts();
  switch(mode)
  {
    case 1: pwm_anim();
            break;
    case 2: alternate_flash();
            break;
    case 3: two_color_anim();
            break;
    case 4: rand_anim();
            break;
    case 5: only_red();
            break;
    case 6: only_green();
            break;
    case 7: only_blue();
            break;
    case 8: red_green();
            break;
    case 9: red_blue();
            break;
    case 10:green_blue();
            break;
    case 11:all();
            break;
  }
}

void rand_anim()
{
  switch(random(1, 4))
  {
    case 1: digitalWrite(R, HIGH);
            delay(rand_delay);
            digitalWrite(R,LOW);
            digitalWrite(G, LOW);
            digitalWrite(B, LOW);
            break;
    case 2: digitalWrite(R, LOW);
            digitalWrite(G, HIGH);
            delay(rand_delay);
            digitalWrite(G, LOW);
            digitalWrite(B, LOW);
            break;
    case 3: digitalWrite(R, LOW);
            digitalWrite(G, LOW);
            digitalWrite(B, HIGH);
            delay(rand_delay);
            digitalWrite(B, LOW);
            break;
  }
}

void only_red()
{
  digitalWrite(R, HIGH);
  digitalWrite(G, LOW);
  digitalWrite(B, LOW);
}
void only_green()
{
  digitalWrite(R, LOW);
  digitalWrite(G, HIGH);
  digitalWrite(B, LOW);  
}

void only_blue()
{
  digitalWrite(R, LOW);
  digitalWrite(G, LOW);
  digitalWrite(B, HIGH);
}

void red_green()
{
  digitalWrite(R, HIGH);
  digitalWrite(G, HIGH);
  digitalWrite(B, LOW);
}

void red_blue()
{
  digitalWrite(R, HIGH);
  digitalWrite(G, LOW);
  digitalWrite(B, HIGH);
}

void green_blue()
{
  digitalWrite(R, LOW);
  digitalWrite(G, HIGH);
  digitalWrite(B, HIGH);
}

void all()
{
  digitalWrite(R, HIGH);
  digitalWrite(G, HIGH);
  digitalWrite(B, HIGH);
}

void mode_change_isr()
{
  noInterrupts();
  if(mode < 11)
  {
    ++mode;
  }
  else if(mode >= 11)
  {
    mode=1;
  }
  /*if(a==1)
  {
    digitalWrite(R, HIGH);
    digitalWrite(G, LOW);
    a=2;
  }
  else
  {
    digitalWrite(R, LOW);
    digitalWrite(G, HIGH);
    a=1;
  }
  interrupts();*/
}

Here is the code.

There is also a button on the board which can also be used to change the mode, on press which will interrupt the arduino to change the mode

  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
  {
    // sets the value (range from 0 to 255):
    analogWrite(R, fadeValue);
    analogWrite(G, fadeValue);
    // wait for 40 milliseconds to see the dimming effect
    delay(40);
  }

As I suspected.

This post on our blog provides an example sketch of how it can be done using interrupts. It is more accurate than IRremote, in terms of timing & can handle longer signals.

Air Conditioners: Recording long Infrared Remote control signals with Arduino