1

I'm having an issue in controlling a RGB LED with Arduino and a remote control. When including the library IRRemote.h (version 2.2.3), the blue channel only works in on/off logic instead of PWM. If I remove the library, the PWM logic of the blue channel works normally and it does accept the whole range (0-255). I'm using an Arduino/Elegoo Uno R3 board. What could be causing the issue?

Below the code:

#include "IRremote.h"

int pinRic = 9; // Signal Pin of IR receiver to Arduino Digital Pin 11
IRrecv ricevitore(pinRic);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'
#define BLUE 3
#define GREEN 5
#define RED 6
#define IR_BPlus  0xFF3AC5  // 
#define IR_BMinus 0xFFBA45  // 
#define IR_ON     0xFF827D  // 
#define IR_OFF    0xFF02FD  // 
#define IR_R    0xFF1AE5  // 
#define IR_G    0xFF9A65  // 
#define IR_B      0xFFA25D  // 
#define IR_W    0xFF22DD  // 
#define IR_B1   0xFF2AD5  // 
#define IR_B2   0xFFAA55  // 
#define IR_B3   0xFF926D  // 
#define IR_B4   0xFF12ED  // 
#define IR_B5   0xFF0AF5  // 
#define IR_B6   0xFF8A75  // 
#define IR_B7   0xFFB24D  // 
#define IR_B8   0xFF32CD  // 
#define IR_B9   0xFF38C7  // 
#define IR_B10    0xFFB847  // 
#define IR_B11    0xFF7887  // 
#define IR_B12    0xFFF807  // 
#define IR_B13    0xFF18E7  // 
#define IR_B14    0xFF9867  // 
#define IR_B15    0xFF58A7  // 
#define IR_B16    0xFFD827  // 
#define IR_UPR    0xFF28D7  // 
#define IR_UPG    0xFFA857  // 
#define IR_UPB    0xFF6897  // 
#define IR_QUICK  0xFFE817  // 
#define IR_DOWNR  0xFF08F7  // 
#define IR_DOWNG  0xFF8877  // 
#define IR_DOWNB  0xFF48B7  // 
#define IR_SLOW   0xFFC837  // 
#define IR_DIY1   0xFF30CF  // 
#define IR_DIY2   0xFFB04F  // 
#define IR_DIY3   0xFF708F  // 
#define IR_AUTO   0xFFF00F  // 
#define IR_DIY4   0xFF10EF  // 
#define IR_DIY5   0xFF906F  // 
#define IR_DIY6   0xFF50AF  // 
#define IR_FLASH  0xFFD02F  // 
#define IR_JUMP3  0xFF20DF  // 
#define IR_JUMP7  0xFFA05F  // 
#define IR_FADE3  0xFF609F  // 
#define IR_FADE7  0xFFE01F  // 
#define incFact 30
int  brightFact = 0;
int lv[3];


void fade7()
{
#define delayTime 10 // fading time between colors

  lv[0] = 255; // choose a value between 1 and 255 to change the color.
  lv[1] = 0;
  lv[2] = 0;


  for (int i = 0; i < 255; i += 1) // fades out R bring G full when i=255
  {
    lv[0] -= 1;
    lv[1] += 1;

    analogWrite(RED, lv[0]);
    analogWrite(GREEN, lv[1]);
    delay(delayTime);
  }

  lv[0] = 0;
  lv[1] = 255;
  lv[2] = 0;

  for (int i = 0; i < 255; i += 1) // fades out G bring B full when i=255
  {
    lv[1] -= 1;
    lv[2] += 1;

    analogWrite(GREEN, lv[1]);
    analogWrite(BLUE, lv[2]);
    delay(delayTime);
  }

  lv[0] = 0;
  lv[1] = 0;
  lv[2] = 255;

  for (int i = 0; i < 255; i += 1) // fades out B bring R full when i=255
  {
    lv[2] -= 1;
    lv[0] += 1;

    analogWrite(BLUE, lv[2]);
    analogWrite(RED, lv[0]);
    delay(delayTime);
  }
}

void setColor(int r, int g, int b)
{
  Serial.println("About to write:");
  Serial.println(r);
  Serial.println(g);
  Serial.println(b);
  analogWrite(RED, r);
  analogWrite(GREEN, g);
  analogWrite(BLUE, b);
}

void  translateIR() // takes action based on IR code received
{
// describing Remote IR codes
  Serial.println(results.value, HEX);
  switch (results.value)
  {
    //case IR_BPlus: setBright(true);break;
    //case IR_BMinus: setBright(false);break;
    case IR_OFF: setColor(0, 0, 0); break;
    //COLORS
    case IR_R: setColor(255, 0, 0); break;
    case IR_G: setColor(0, 255, 0); break;
    case IR_B: setColor(0, 0, 255); break;
    case IR_W: setColor(255, 255, 255); break;
    case IR_B1: setColor(255, 20, 0); break;
    case IR_B2: setColor(0, 255, 20); break;
    case IR_B3: setColor(20, 0, 255); break;
    case IR_B4: setColor(255, 20, 255); break;
    case IR_B5: setColor(255, 90, 0); break;
    case IR_B6: setColor(0, 255, 90); break;
    case IR_B7: setColor(90, 0, 255); break;
    case IR_B8: setColor(255, 150, 255 ); break;
    case IR_B9: setColor(255, 191, 0); break;
    case IR_B10: setColor(0, 255, 191); break;
    case IR_B11: setColor(191, 0, 255); break;
    case IR_B12: setColor(120, 255, 255); break;
    case IR_B13: setColor(255, 255, 0); break;
    case IR_B14: setColor(0, 255, 255); break;
    case IR_B15: setColor(255, 0, 255); break;
    case IR_B16: setColor(150, 255, 255); break;

    //END COLORS
    case IR_FADE7: fade7();
  }// End Case

  delay(100); // Do not get immediate repeat
} //END translateIR

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  ricevitore.enableIRIn(); // Start the receiver*/

}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  if (ricevitore.decode(&results)) // have we received an IR signal?
  {
    translateIR();
    ricevitore.resume(); // receive the next value*/
  }
} /*--(end main loop )-- */
  • I give you an upvote for a decently written sketch. Not 100% perfect but better than many others. – user31481 Nov 26 '17 at 11:33
1

By default, the IRremote library uses Timer 2 on an ATmega328P. (Library documentation) This timer is also used for PWM on pins 3 and 11.
This means that you will have to use a different PWM pin for your blue LED (e.g. 9 or 10, controlled by Timer 1). Alternatively, you could edit the IRremote library to use Timer 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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