So I've just started learning Arduino, and am trying out different things, and have decided to try and use an IR remote to change the RGB values of an RGB LED.
I'm using an UNO Arduino and the Car Mp3 remote, but for some reason my code(I'm pretty sure its the code) doesn't work. The red brightness values on 85 and 170 cause it to 'freeze' and the other buttons fail to work, the green brightness values do the same, although sometimes don't and when I reset the Arduino they do what the red values do, and the blue values are completely different, the 255 one turning the light on and the 170, 85 and 0 brightness just turn the blue value off.
Sorry about what I suspect to be really painful code to anyone who knows that they're doing. Thanks.
#include <IRremote.h>
int irInput = 11;
int redPin = 6;
int greenPin = 5;
int bluePin = 3;
int redRemote = 0;
int greenRemote = 0;
int blueRemote =0;
IRrecv irrecv(irInput);
decode_results results;
void setup() {
// put your setup code here, to run once:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(irInput, INPUT);
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
irrecv.enableIRIn();
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (irrecv.decode(&results)){
switch(results.value){
case 0xFFE01F: // Minus because Zero doesn't work
analogWrite(redPin, 0);
}
irrecv.resume();
switch(results.value){
case 0xFF30CF: // One
analogWrite(redPin, 85);
}
irrecv.resume();
switch(results.value){
case 0xFF10EF: // Four
analogWrite(redPin, 170);
}
irrecv.resume();
switch(results.value){
case 0xFF42BD: // Seven
analogWrite(redPin, 255);
}
irrecv.resume();
switch(results.value){
case 0xFF9867: // 100+
analogWrite(greenPin, 0);
}
irrecv.resume();
switch(results.value){
case 0xFF18E7: // Two
analogWrite(greenPin, 85);
}
irrecv.resume();
switch(results.value){
case 0xFF38C7: // Five
analogWrite(greenPin, 170);
}
irrecv.resume();
switch(results.value){
case 0xFF4AB5: // Eight
analogWrite(greenPin, 255);
}
irrecv.resume();
switch(results.value){
case 0xFFB04F: // 200+
analogWrite(bluePin, 0);
}
irrecv.resume();
switch(results.value){
case 0xFF7A85: // Three
analogWrite(bluePin, 85);
}
irrecv.resume();
switch(results.value){
case 0xFF5AA5: // Six
analogWrite(bluePin, 170);
}
irrecv.resume();
switch(results.value){
case 0xFF52AD: // Nine
analogWrite(bluePin, 255);
}
irrecv.resume();
}
}
Why so complicated (and probably non functional) ?
#include <IRremote.h>
const byte irInput = 11;
const byte redPin = 6;
const byte greenPin = 5;
const byte bluePin = 3;
IRrecv irrecv(irInput);
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
irrecv.enableIRIn();
}
void loop() {
decode_results results;
if (irrecv.decode(&results)) {
switch (results.value) {
case 0xFFE01F: // Minus because Zero doesn't work
analogWrite(redPin, 0);
break;
case 0xFF30CF: // One
analogWrite(redPin, 85);
break;
case 0xFF10EF: // Four
analogWrite(redPin, 170);
break;
case 0xFF42BD: // Seven
analogWrite(redPin, 255);
break;
case 0xFF9867: // 100+
analogWrite(greenPin, 0);
break;
case 0xFF18E7: // Two
analogWrite(greenPin, 85);
break;
case 0xFF38C7: // Five
analogWrite(greenPin, 170);
break;
case 0xFF4AB5: // Eight
analogWrite(greenPin, 255);
break;
case 0xFFB04F: // 200+
analogWrite(bluePin, 0);
break;
case 0xFF7A85: // Three
analogWrite(bluePin, 85);
break;
case 0xFF5AA5: // Six
analogWrite(bluePin, 170);
break;
case 0xFF52AD: // Nine
analogWrite(bluePin, 255);
break;
}
irrecv.resume();
}
}
The idea of switch/case is that you have one switch followed by a load of cases. You don't need to write the switch statement again every time. See switch...case - Arduino Reference
I doubt if that is the cause of all your problems but fixing it will make the code a lot easier to follow.
Steve
slipstick:
I doubt if that is the cause of all your problems but fixing it will make the code a lot easier to follow.
It makes it a matter of lucky timing to reach any action,
because a received value is only compared to one possibility and then thrown away.
I had one switch cause at the start but I found that if I had a switch cause for each one individually they worked(only when the LED brightness was at 255 or 0). But yeah, its not suppose to need multiply switches but that is just what I found to work.
I did warn my code would be dreadful, and I think it lived up to that.
Some_Nerd:
I did warn my code would be dreadful, and I think it lived up to that.
It's not so bad, just a copy-and-paste that went shoot-yourself-in-the-foot.
Whandall:
Why so complicated (and probably non functional) ?
#include <IRremote.h>
const byte irInput = 11;
const byte redPin = 6;
const byte greenPin = 5;
const byte bluePin = 3;
IRrecv irrecv(irInput);
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
irrecv.enableIRIn();
}
void loop() {
decode_results results;
if (irrecv.decode(&results)) {
switch (results.value) {
case 0xFFE01F: // Minus because Zero doesn't work
analogWrite(redPin, 0);
break;
case 0xFF30CF: // One
analogWrite(redPin, 85);
break;
case 0xFF10EF: // Four
analogWrite(redPin, 170);
break;
case 0xFF42BD: // Seven
analogWrite(redPin, 255);
break;
case 0xFF9867: // 100+
analogWrite(greenPin, 0);
break;
case 0xFF18E7: // Two
analogWrite(greenPin, 85);
break;
case 0xFF38C7: // Five
analogWrite(greenPin, 170);
break;
case 0xFF4AB5: // Eight
analogWrite(greenPin, 255);
break;
case 0xFFB04F: // 200+
analogWrite(bluePin, 0);
break;
case 0xFF7A85: // Three
analogWrite(bluePin, 85);
break;
case 0xFF5AA5: // Six
analogWrite(bluePin, 170);
break;
case 0xFF52AD: // Nine
analogWrite(bluePin, 255);
break;
}
irrecv.resume();
}
}
I tried this, and while easier to read and understand(Thanks) it still had the same results as my original code, that being the buttons 1,2,4 or 5 stopped working when they were pressed and buttons 3 and 6 giving a blue value of 0.
Some_Nerd:
the buttons 1,2,4 or 5 stopped working when they were pressed
I don't understand what that means.
Some_Nerd:
buttons 3 and 6 giving a blue value of 0.
So the mapping of the codes to the actions seems to be messed up.
Print each received value in hex to the serial monitor and check your numbers.
Have you tried a very basic sketch that simply cycles round writing the various values to the LED pins with delays between? That will at least tell you if the analogWrites() don't do what you think they should (probably because the wiring is suspect) or if you're never actually getting to them.
Alternatively a few Serial.prints scattered round might help you to know exactly where in the code it is getting to.
Steve
Whandall:
I don't understand what that means.
So the mapping of the codes to the actions seems to be messed up.
Print each received value in hex to the serial monitor and check your numbers.
What I meant by "the buttons 1,2,4 or 5 stopped working when they were pressed" is that when I pressed the buttons on the remote it would cause the program to 'freeze' and other buttons that previous worked would not, until a reset, the buttons have RGB brightness values of 170 or 85, but if I changed the value to 255 it would work fine. As for the mapping of the blue buttons, they experience a similar thing, if their value is less than 255 they cause the blue value to go to 0, but if its 255 it works fine.
slipstick:
Have you tried a very basic sketch that simply cycles round writing the various values to the LED pins with delays between? That will at least tell you if the analogWrites() don't do what you think they should (probably because the wiring is suspect) or if you're never actually getting to them.
I ran a quick program that cycled through all the RGB brightness values and they worked perfectly.
slipstick:
Alternatively a few Serial.prints scattered round might help you to know exactly where in the code it is getting to.
I got each button to print what it is and they're all assigned correctly.
That does not make sense to me.
Whandall's right, that seems very strange. Can we see the current version of your program, with all the Serial.prints in please?
Steve
#include <IRremote.h>
const byte irInput = 11;
const byte redPin = 6;
const byte greenPin = 5;
const byte bluePin = 3;
IRrecv irrecv(irInput);
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
irrecv.enableIRIn();
Serial.begin(9600);
}
void loop() {
decode_results results;
if (irrecv.decode(&results)) {
switch (results.value) {
case 0xFFE01F: // Minus because Zero doesn't work
analogWrite(redPin, 0);
Serial.println("Minus");
break;
case 0xFF30CF: // One
analogWrite(redPin, 85);
Serial.println("One");
break;
case 0xFF10EF: // Four
analogWrite(redPin, 170);
Serial.println("Four");
break;
case 0xFF42BD: // Seven
analogWrite(redPin, 255);
Serial.println("Seven");
break;
case 0xFF9867: // 100+
analogWrite(greenPin, 0);
Serial.println("100+");
break;
case 0xFF18E7: // Two
analogWrite(greenPin, 85);
Serial.println("Two");
break;
case 0xFF38C7: // Five
analogWrite(greenPin, 170);
Serial.println("Five");
break;
case 0xFF4AB5: // Eight
analogWrite(greenPin, 255);
Serial.println("Eight");
break;
case 0xFFB04F: // 200+
analogWrite(bluePin, 0);
Serial.println("200+");
break;
case 0xFF7A85: // Three
analogWrite(bluePin, 85);
Serial.println("Three");
break;
case 0xFF5AA5: // Six
analogWrite(bluePin, 170);
Serial.println("Six");
break;
case 0xFF52AD: // Nine
analogWrite(bluePin, 255);
Serial.println("Nine");
break;
}
irrecv.resume();
}
}
I think the problem comes from the values of 170 and 85 in the analogWrite(), if I remove the analogWrites in the cases and just look at the prints the remote works fine and the doesn't freeze up, and if the analogWrite values are all either 255 or 0 it works fine as well.
Some_Nerd:
I ran a quick program that cycled through all the RGB brightness values and they worked perfectly.
Some_Nerd:
I think the problem comes from the values of 170 and 85 in the analogWrite(), if I remove the analogWrites in the cases and just look at the prints the remote works fine and the doesn't freeze up, and if the analogWrite values are all either 255 or 0 it works fine as well.
I see a contradiction there.
Whandall:
I see a contradiction there.
The RGB brightness worked in a code like this
int redPin = 6;
int greenPin = 5;
int bluePin = 3;
void setup() {
// put your setup code here, to run once:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(redPin, 0);
analogWrite(bluePin, 0);
analogWrite(greenPin, 0);
delay(1000);
analogWrite(redPin, 0);
analogWrite(bluePin, 255);
analogWrite(greenPin, 0);
delay(1000);
analogWrite(redPin, 0);
analogWrite(bluePin, 170);
analogWrite(greenPin, 0);
delay(1000);
analogWrite(redPin, 0);
analogWrite(bluePin, 85);
analogWrite(greenPin, 0);
delay(1000);
}
The idea being to check the LED worked and the analogWrite was working. But when I use the values 170 and 85 in the code with the remote it causes the problems.