IRremote libary with 24keyRGB remote

Hello,

I've a question about the IRremote libary. How (and it) is it possible to check if there is been a new button pressed during a subroutine.

I'm wondering for this because of the fade and smooth routine in my code. Right now if i press the button the routine runs ones and then i've to press the button again. I want that it keeps running until I press an other button on te remote. The part of keeping it running can I do with a while loop, but then I can't get it out of that routine.

I hope that someone can give me a clue.

My code is based on the one in this link.

Kind Regards,
John

PS: @the mods, this is my first post here, if it's in the wrong section feel free to move it to the right section.

Code:

/*
*  IR RGB LED Remote Control
*  Receiver type: TSOP1736/38
*  Autor: John de Graaf
*  Date: 12/4/2015
*/

#include <IRremote.h>
#include <IRremoteInt.h>

int RECV_PIN = 4;
int R_PIN = 6;
int G_PIN = 9;
int B_PIN = 10;

#define ON                0XFFF0C41643
#define OFF               0xFFE721C0DB
#define BRIGHTNESS_UP     0xFFE5CFBD7F
#define BRIGHTNESS_DOWN   0xFFA23C94BF
#define FLASH             0xFF7EC31EF7
#define STROBE            0xFFFA3F159F
#define FADE              0xFFDC0197DB
#define SMOOTH            0xFF9716BE3F

#define RED               0xFF97483BFB
#define GREEN             0XFF86B0E697
#define BLUE              0xFF9EF4941F
#define WHITE             0xFFA3C8EDDB

#define ORANGE            0xFF5BE75E7F
#define YELLOW_DARK       0xFFD7E84B1B
#define YELLOW_MEDIUM     0xFF2A89195F
#define YELLOW_LIGHT      0xFF488F3CBB

#define GREEN_LIGHT       0XFFF377C5B7
#define GREEN_BLUE1       0XFFEE4ECCFB
#define GREEN_BLUE2       0XFFF63C8657
#define GREEN_BLUE3       0XFF13549BDF

#define BLUE_RED          0XFFC101E57B
#define PURPLE_DARK       0XFF51E43D1B
#define PURPLE_LIGHT      0XFF44C407DB
#define PINK              0XFF35A9425F

unsigned long rgb = 0;
byte r, g, b;
byte r1, g1, b1;
byte onoff = 2;
byte dim = 100, time = 100;
long currentMillis;


IRrecv irrecv(RECV_PIN);

decode_results results;


void setup()
{
  irrecv.enableIRIn(); // Initiolise IR recveiver
  Serial.begin(9600);
  pinMode(R_PIN, OUTPUT);
  pinMode(G_PIN, OUTPUT);
  pinMode(B_PIN, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}

void RGB(unsigned long amount) {
  r = amount >> 16;
  g = (amount >> 8) & 0xFF;
  b = amount & 0xFF;
}

//-------------------------------------------------------------

void timer(int interval) {
  currentMillis = millis();

  while (millis() - currentMillis < interval) {

    if (irrecv.decode(&results)) {
      if (results.value != 0xFFFFFFFF)
        return;
    }
    irrecv.resume(); // Receive the next value
  }//while end
}

void flash() {
}

void strobe() {
}

void fade() {
  r = 255;
  g = 0;
  b = 0;
  for ( int i = 0 ; i < 255 ; i++ ) {
    r--;
    g++;
    colortoled();
    timer(time);
  }

  r = 0;
  g = 255;
  b = 0;
  for ( int i = 0 ; i < 255 ; i++ ) {
    g--;
    b++;
    colortoled();
    timer(time);
  }

  r = 0;
  g = 0;
  b = 255;
  for ( int i = 0 ; i < 255 ; i++ ) {
    r++;
    b--;
    colortoled();
    timer(time);
  }


  if (irrecv.decode(&results)) {
    if (results.value != 0xFFFFFFFF)
      return;
  }

  irrecv.resume(); // Receive the next value
}


void smooth() {

  r = 255;
  g = 0;
  b = 0;
  for ( int i = 0 ; i < 255 ; i++ ) {
    g++;
    colortoled();
    timer(time);
  }

  r = 255;
  g = 255;
  b = 0;
  for ( int i = 0 ; i < 255 ; i++ ) {
    r--;
    colortoled();
    timer(time);
  }

  r = 0;
  g = 255;
  b = 0;
  for ( int i = 0 ; i < 255 ; i++ ) {
    b++;
    colortoled();
    timer(time);
  }

  r = 0;
  g = 255;
  b = 255;
  for ( int i = 0 ; i < 255 ; i++ ) {
    g--;
    colortoled();
    timer(time);
  }

  r = 0;
  g = 0;
  b = 255;
  for ( int i = 0 ; i < 255 ; i++ ) {
    r++;
    colortoled();
    timer(time);
  }

  r = 255;
  g = 0;
  b = 255;
  for ( int i = 0 ; i < 255 ; i++ ) {
    b--;
    colortoled();
    timer(time);
  }



  if (irrecv.decode(&results)) {
    if (results.value != 0xFFFFFFFF)
      return;
  }

  irrecv.resume(); // Receive the next value
}

//-------------------------------------------------------------

void colortoled() {
  Serial.println(results.value , HEX);
  Serial.println(r , DEC);
  Serial.println(g , DEC);
  Serial.println(b , DEC);
  Serial.println(dim);
  Serial.println('-');

  if (r != 0 | onoff == 1) {
    r1 = r;
  }
  if (g != 0 | onoff == 1) {
    g1 = g;
  }
  if (b != 0 | onoff == 1) {
    b1 = b;
  }

  analogWrite(R_PIN, r * dim / 100);
  analogWrite(G_PIN, g * dim / 100);
  analogWrite(B_PIN, b * dim / 100);
}

void loop() {
  if (irrecv.decode(&results)) {
    if ( results.value != 0xFFFFFFFF) {
      switch (results.value) {
        case OFF :
          r = g = b = 0;
          onoff = 2;
          break;
        case ON :
          r = r1;
          g = g1;
          b = b1;
          onoff = 1;
          break;
      }

      if (onoff == 1) {
        switch (results.value) {
          case BRIGHTNESS_UP :
            if (dim < 100) {
              dim = dim + 10;
            } else {
              dim = 100;
            }
            break;
          case BRIGHTNESS_DOWN :
            if (dim > 0) {
              dim = dim - 10;
            } else {
              dim = 0;
            }
            break;
          case RED           : RGB(0x00FF0000); break;
          case GREEN         : RGB(0x0000FF00); break;
          case BLUE          : RGB(0x000000FF); break;
          case WHITE         : RGB(0x00FFFFFF); break;
          case ORANGE        : RGB(0x00FF3000); break;
          case YELLOW_DARK   : RGB(0x00FF7000); break;
          case YELLOW_MEDIUM : RGB(0x00FFAA00); break;
          case YELLOW_LIGHT  : RGB(0x00FFD400); break;
          case GREEN_LIGHT   : RGB(0x0000FF30); break;
          case GREEN_BLUE1   : RGB(0x0000AAAA); break;
          case GREEN_BLUE2   : RGB(0x0000CCCC); break;
          case GREEN_BLUE3   : RGB(0x0000FFFF); break;
          case BLUE_RED      : RGB(0x00100080); break;
          case PURPLE_DARK   : RGB(0x005000BF); break;
          case PURPLE_LIGHT  : RGB(0x007A00BF); break;
          case PINK          : RGB(0x00FF00FF); break;
          case FLASH         : flash();         break; 
          case STROBE        : strobe();        break;  
          case FADE          : fade();          break;  
          case SMOOTH        : smooth();        break; 
        }
      }

      colortoled();

      irrecv.resume(); // Receive the next value
    }
  }
}

Hi,

The real, but unwelcome, answer is that you should not have written your sketch like that, or used that sketch, in the first place. The way its written is not going to lend itself well to want you want. Loops with, in effect, delay commands in them.

But before you embark on a re-write, try this:

Replace each of these lines:

for ( int i = 0 ; i < 255 ; i++ ) {

With this:

for ( int i = 0 ; i < 255 && results.value == 0xFFFFFFFF ; i++ ) {

I've never used the ir library, so I might have got that wrong. But the idea is to cut short all the loops once an ir code has been received. The above will cut a loop short if a code is received during the loop, and it will also stop the remaining loops in the sequence from running also.

Paul

Hello Paul,

Thanks for your reply.

I get the idea wat you want, however when I try it, it doesn't work. The led goes to a color and then doesn't start fading. I can press an other button and than the led goes to that color.

I've tried the adress of the smooth and fade key also, but they give the same result.

Kind regards,
John

Sorry John, perhaps someone else can help.

Oke thnx, I hope so.

I'm thinking that the problem is in the while loop of the timer because when I check te serial monitor I see the same IR code.

So it seems that it doesn't check if a button is pressed there. But i don't know how to solve it, because it's the same code as in the main loop.

DC0197DB
90
0
165
100
-
DC0197DB
91
0
164
100
-
DC0197DB
92
0
163
100
-
DC0197DB
93
0
162
100
-
DC0197DB
94
0
161
100
-
DC0197DB
95
0
160
100
-

Kind regards,
John

while (millis() - currentMillis < interval)

What is the value for "interval" here?

Hello,

Thanks for your reply. Interval is the value that comes from the other routine.
Timer(time) and time = 100

So interval is 100.

Kind regards,
John

Hi John,

My project about ir controls is diff from yours

Sorry that I do not quite undertstand the code

Hope someone will step in to assist you.

Good luck!

Hello,

Thanks for your reply.
I hope so too.
Otherwise I'm thinking of buying another arduino, and let that one check if there is something send, and make a pin high, an let the main arduino check in the subroutine if that pin is high, and when it is return to the main loop. I know it doubles the costs, but spending 3$ more isn't that big problem.

I've tried that with a wire, and that works, so I hope that it wil also work with an arduino.

Kind regards,
John

Comparing your code for receiving IR codes to the IR receive demo included with the IR library and your not doing it quite the same way. In some code your always doing a .resume even if .decode is false.
Also the logic of your code is not ideal as if you press remote while code is in timer() then it reads the result and exits but does not pass this result back to the calling routing (unless it stays present until an irrecv.resume() is executed)
Maybe if you have a single function that reads the IR remote that is called from all the relevant places (loop() & timer) and depending on the result (if any) falls back to the loop where it is acted on.

Ok, try this in addition to my earlier suggested changes:

void timer(int interval) {
  currentMillis = millis();

  while (millis() - currentMillis < interval) {

    if (irrecv.decode(&results)) {
      if (results.value != 0xFFFFFFFF)
        return;
    }
    else
    {
      results.value = 0xFFFFFFFF;
    }
    irrecv.resume(); // Receive the next value
  }//while end
}

Hello Riva and Paul,

Both of you thanks for the reply.

Riva, I've downloaded the code from the site which I gave and I hadn't changed that part. But I will check it out.

Paul, Thanks for the code, when I execute it, the led starts fading, but I can't interrupt it.

Kind regards,
John

LooneyTunes15:
Paul, Thanks for the code, when I execute it, the led starts fading, but I can't interrupt it.

Did you make that change and my earlier suggested changes? Post the sketch with the changes made please and I will check it.

Hello Paul,

Thanks for your reply.

Sorry, I've misunderstand you (English isn't my native language, I'm Dutch). When I combine the changes to the code underneed, the fade option goes to a blue led, the smooth to a magnenta (both not fading) when I press the button.

Kind regards,
John

/*
*  IR RGB LED Remote Control
*  Receiver type: TSOP1736/38
*  Autor: John de Graaf
*  Date: 12/4/2015
*/

#include <IRremote.h>
#include <IRremoteInt.h>

int RECV_PIN = 4;
int R_PIN = 6;
int G_PIN = 9;
int B_PIN = 10;

#define ON                0XFFF0C41643
#define OFF               0xFFE721C0DB
#define BRIGHTNESS_UP     0xFFE5CFBD7F
#define BRIGHTNESS_DOWN   0xFFA23C94BF
#define FLASH             0xFF7EC31EF7
#define STROBE            0xFFFA3F159F
#define FADE              0xFFDC0197DB
#define SMOOTH            0xFF9716BE3F

#define RED               0xFF97483BFB
#define GREEN             0XFF86B0E697
#define BLUE              0xFF9EF4941F
#define WHITE             0xFFA3C8EDDB

#define ORANGE            0xFF5BE75E7F
#define YELLOW_DARK       0xFFD7E84B1B
#define YELLOW_MEDIUM     0xFF2A89195F
#define YELLOW_LIGHT      0xFF488F3CBB

#define GREEN_LIGHT       0XFFF377C5B7
#define GREEN_BLUE1       0XFFEE4ECCFB
#define GREEN_BLUE2       0XFFF63C8657
#define GREEN_BLUE3       0XFF13549BDF

#define BLUE_RED          0XFFC101E57B
#define PURPLE_DARK       0XFF51E43D1B
#define PURPLE_LIGHT      0XFF44C407DB
#define PINK              0XFF35A9425F

unsigned long rgb = 0;
byte r, g, b;
byte r1, g1, b1;
byte onoff = 2;
byte dim = 100, time = 100;
long currentMillis;


IRrecv irrecv(RECV_PIN);

decode_results results;


void setup()
{
  irrecv.enableIRIn(); // Initiolise IR recveiver
  Serial.begin(9600);
  pinMode(R_PIN, OUTPUT);
  pinMode(G_PIN, OUTPUT);
  pinMode(B_PIN, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}

void RGB(unsigned long amount) {
  r = amount >> 16;
  g = (amount >> 8) & 0xFF;
  b = amount & 0xFF;
}

//-------------------------------------------------------------

void timer(int interval) {
  currentMillis = millis();

  while (millis() - currentMillis < interval) {

    if (irrecv.decode(&results)) {
      if (results.value != 0xFFFFFFFF)
        return;
    }
    else
    {
      results.value = 0xFFFFFFFF;
    }
    irrecv.resume(); // Receive the next value
  }//while end
}



void flash() {
}

void strobe() {
}

void fade() {
  r = 255;
  g = 0;
  b = 0;
  for ( int i = 0 ; i < 255 && results.value == 0xFFFFFFFF ; i++ ) {
    r--;
    g++;
    colortoled();
    timer(time);
  }

  r = 0;
  g = 255;
  b = 0;
  for ( int i = 0 ; i < 255 && results.value == 0xFFFFFFFF ; i++ ) {
    g--;
    b++;
    colortoled();
    timer(time);
  }

  r = 0;
  g = 0;
  b = 255;
  for ( int i = 0 ; i < 255 && results.value == 0xFFFFFFFF ; i++ ) {
    r++;
    b--;
    colortoled();
    timer(time);
  }


  if (irrecv.decode(&results)) {
    if (results.value != 0xFFFFFFFF)
      return;
  }

  irrecv.resume(); // Receive the next value
}


void smooth() {

  r = 255;
  g = 0;
  b = 0;
  for ( int i = 0 ; i < 255 && results.value == 0xFFFFFFFF ; i++ ) {
    g++;
    colortoled();
    timer(time);
  }

  r = 255;
  g = 255;
  b = 0;
  for ( int i = 0 ; i < 255 && results.value == 0xFFFFFFFF ; i++ ) {
    r--;
    colortoled();
    timer(time);
  }

  r = 0;
  g = 255;
  b = 0;
  for ( int i = 0 ; i < 255 && results.value == 0xFFFFFFFF ; i++ ) {
    b++;
    colortoled();
    timer(time);
  }

  r = 0;
  g = 255;
  b = 255;
  for ( int i = 0 ; i < 255 && results.value == 0xFFFFFFFF ; i++ ) {
    g--;
    colortoled();
    timer(time);
  }

  r = 0;
  g = 0;
  b = 255;
  for ( int i = 0 ; i < 255 && results.value == 0xFFFFFFFF ; i++ ) {
    r++;
    colortoled();
    timer(time);
  }

  r = 255;
  g = 0;
  b = 255;
  for ( int i = 0 ; i < 255 && results.value == 0xFFFFFFFF ; i++ ) {
    b--;
    colortoled();
    timer(time);
  }



  if (irrecv.decode(&results)) {
    if (results.value != 0xFFFFFFFF)
      return;
  }

  irrecv.resume(); // Receive the next value
}

//-------------------------------------------------------------

void colortoled() {
  Serial.println(results.value , HEX);
  Serial.println(r , DEC);
  Serial.println(g , DEC);
  Serial.println(b , DEC);
  Serial.println(dim);
  Serial.println('-');

  if (r != 0 | onoff == 1) {
    r1 = r;
  }
  if (g != 0 | onoff == 1) {
    g1 = g;
  }
  if (b != 0 | onoff == 1) {
    b1 = b;
  }

  analogWrite(R_PIN, r * dim / 100);
  analogWrite(G_PIN, g * dim / 100);
  analogWrite(B_PIN, b * dim / 100);
}

void loop() {
  if (irrecv.decode(&results)) {
    if ( results.value != 0xFFFFFFFF) {
      switch (results.value) {
        case OFF :
          r = g = b = 0;
          onoff = 2;
          break;
        case ON :
          r = r1;
          g = g1;
          b = b1;
          onoff = 1;
          break;
      }

      if (onoff == 1) {
        switch (results.value) {
          case BRIGHTNESS_UP :
            if (dim < 100) {
              dim = dim + 10;
            } else {
              dim = 100;
            }
            break;
          case BRIGHTNESS_DOWN :
            if (dim > 0) {
              dim = dim - 10;
            } else {
              dim = 0;
            }
            break;
          case RED           : RGB(0x00FF0000); break;
          case GREEN         : RGB(0x0000FF00); break;
          case BLUE          : RGB(0x000000FF); break;
          case WHITE         : RGB(0x00FFFFFF); break;
          case ORANGE        : RGB(0x00FF3000); break;
          case YELLOW_DARK   : RGB(0x00FF7000); break;
          case YELLOW_MEDIUM : RGB(0x00FFAA00); break;
          case YELLOW_LIGHT  : RGB(0x00FFD400); break;
          case GREEN_LIGHT   : RGB(0x0000FF30); break;
          case GREEN_BLUE1   : RGB(0x0000AAAA); break;
          case GREEN_BLUE2   : RGB(0x0000CCCC); break;
          case GREEN_BLUE3   : RGB(0x0000FFFF); break;
          case BLUE_RED      : RGB(0x00100080); break;
          case PURPLE_DARK   : RGB(0x005000BF); break;
          case PURPLE_LIGHT  : RGB(0x007A00BF); break;
          case PINK          : RGB(0x00FF00FF); break;
          case FLASH         : flash();         break; 
          case STROBE        : strobe();        break;  
          case FADE          : fade();          break;  
          case SMOOTH        : smooth();        break; 
        }
      }

      colortoled();

      irrecv.resume(); // Receive the next value
    }
  }
}

Hello,

Small update: I've solved the problem by using 2 arduino nano's.
One for the control, the other one for checking if there is an new code send.

Kind regards,
John