Hello, I've got a strange problem with IRRemote on pin 11. Basically, I want to change the brightness with a remote on a RGB LED, but everytime I try to use it on pin 11, it just stops working. Every other color works, just the one at pin 11 doesn't.
EDIT: Using IRremote v3.3.0
Here's the code:
#include <IRremote.h>
int red = 11;
int green = 10;
int blue = 9;
int IR = 2;
int colorR;
int brightness = 255;
IRrecv irRecriver(IR);
decode_results answer;
void LedOff() {
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
}
void LedOn() {
digitalWrite(red, HIGH);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
}
void setup() {
Serial.begin(9600);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(IR, INPUT);
irRecriver.enableIRIn();
}
void loop() {
if(irRecriver.decode(&answer)){
Serial.println(answer.value, HEX);
irRecriver.resume();
switch(answer.value){
case 0xFF30CF: //1
digitalWrite(red, HIGH);
colorR = 11;
break;
case 0xFF18E7: //2
digitalWrite(green, HIGH);
colorR = 10;
break;
case 0xFF7A85: //3
digitalWrite(blue, HIGH);
colorR = 9;
break;
case 0xFF10EF: //4
digitalWrite(red, LOW);
colorR = 0;
break;
case 0xFF38C7: //5
digitalWrite(green, LOW);
colorR = 0;
break;
case 0xFF5AA5: //6
digitalWrite(blue, LOW);
colorR = 0;
break;
case 0xFFE01F: //-
LedOff();
brightness = brightness - 50;
if (brightness < 0) {
brightness = 0;
}
analogWrite(colorR, brightness);
break;
case 0xFFA857: //+
LedOff();
brightness = brightness + 50;
if (brightness > 254) {
brightness = 255;
}
analogWrite(colorR, brightness);
break;
}
}
delay(100);
}
The IRremote library was recently upgraded and will not run older (pre version 3.0) code. You can delete the newer library from your scketchbook\libraries folder and install an older version of the library or modify the code to run on the newer version. Older versions of the library are available through the IDE library manager. Instructions on how to modify older code to work with the new library can be found here.
You would have seen a warning if you had had compiler warnings enabled in IDE Files, Preferences. serial monitor open when starting the code.
Thanks for the reply
I've already tried downgrading and it didn't work.
I'll try to upgrade the code to the new version, and see if it works
EDIT: Yes, I do have some errors when the sketch compiles
Yes, that looks like you do not have the library installed properly. Did you delete the IRremote library folder from your sketchbook\libraries folder? Then install the version desired? This page shows how to use the IDE library manager.
What Arduino are you using?
Your code compiles fine with no warnings or errors with version 2.80 of the library and seems to work. The program sees the remote and I can control the LEDs with my remote (codes modified to fit my remote).
Hi, I've tried reinstalling the library but I still have the same error, I guess I edited the code wrong. I'm using Arduino UNO.
Yes, I know that it compiles fine with the 2.80 version, but when you change the brightness on the pin 11 (on the RGB led (red for me)), the code stops working entirely, and in the serial console, it starts printing different numbers.
The brightness glitch was the only thing that was wrong, I think I didn't quite explain it in the first post.
I have wired up the circuit and using the posted code (modified with my key codes) I am seeing what you say. And just with the red LED (pin 11). Don't yet know why, though.
My suspicion is that the problem occurs, because the PWM for pin 11 uses timer 2 which is the same timer used by the library
<IRremote.h>.
I'm searching the web, to confirm about the pin 11 PWM,
and there is a conflict, but I haven't found confirmation yet.
That is a good idea. It happens when you have the LED on pin 11 and use the - button. From then on the IRremote returns seemingly random garbage if any thing.
I changed 11 to 6 (6 is on timer0) it all the places and now the code behaves the same for all the LEDs. Good catch @ ruilviana.
OP, I had to go through and find all the magic numbers. You name the pins so why don't you use the pin names in the code?
Hi, I didn't understand your answer when I first saw it, so you're saying that if I reconnect the pin (on the board) from pin 11 to pin 6 it would work?
Also, when you said that you had to find the pins colors, well it's on top of the code
Yes they are named at the top of the code, but thereafter you use the pin numbers instead of the names. That makes it so that to change the pin associated with red, one must go through the code and change all occurrences of 11 to 6. If you use the name, red in the code instead of the pin number, then you only have to change 11 to 6 in one place at the top of the code.
Hi, so I just reconnected the pin from 11 to 6, and it works.
Thank you so much and ruilaviana for solving my problem.
I'm marking this post as [SOLVED]