void loop()
{
decode_results results;
if (!irrecv.decode(&results))
return;
// If the LEDs are OFF, the only valid action is to turn them
// ON.
if (!leds_are_on) {
if (results.value == CODE_ON_OFF) {
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
leds_are_on = true;
}
else {
irrecv.resume();
return;
}
}
// The LEDs are ON: all codes are valid.
switch (results.value) {
case CODE_ON_OFF: // turn OFF
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
leds_are_on = false;
break;
case CODE_LESS_RED:
r_bright = constrain(r_bright - 255/steps, 0, 255);
analogWrite(RED_PIN, r_bright);
break;
case CODE_MORE_RED:
r_bright = constrain(r_bright + 255/steps, 0, 255);
analogWrite(RED_PIN, r_bright);
break;
// etc...
}
irrecv.resume();
}
void loop()
{
decode_results results;
if (!irrecv.decode(&results))
return;
// If the LEDs are OFF, the only valid action is to turn them
// ON.
if (!leds_are_on) {
if (results.value == CODE_ON_OFF) {
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
leds_are_on = true;
}
else {
irrecv.resume();
return;
}
}
// The LEDs are ON: all codes are valid.
switch (results.value) {
case CODE_ON_OFF: // turn OFF
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
leds_are_on = false;
break;
case CODE_LESS_RED:
r_bright = constrain(r_bright - 255/steps, 0, 255);
analogWrite(RED_PIN, r_bright);
break;
case CODE_MORE_RED:
r_bright = constrain(r_bright + 255/steps, 0, 255);
analogWrite(RED_PIN, r_bright);
break;
// etc...
}
irrecv.resume();
}
void loop()
{
decode_results results;
if (!irrecv.decode(&results))
return;
// If the LEDs are OFF, the only valid action is to turn them
// ON.
if (!leds_are_on) {
if (results.value == CODE_ON_OFF) {
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
leds_are_on = true;
}
irrecv.resume();
return;
}
// The LEDs are ON: all codes are valid.
switch (results.value) {
case CODE_ON_OFF: // turn OFF
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
leds_are_on = false;
break;
case CODE_LESS_RED:
r_bright = constrain(r_bright - 255/steps, 0, 255);
analogWrite(RED_PIN, r_bright);
break;
case CODE_MORE_RED:
r_bright = constrain(r_bright + 255/steps, 0, 255);
analogWrite(RED_PIN, r_bright);
break;
// etc...
}
irrecv.resume();
}
Edit: After reading your code, I have a few suggestions for making it clearer are more efficient:
- Declare all the constants with the
const
keyword, at the beginning of your program. A common convention is to write them in all caps with underscores, to distinguish them from variables. - Avoid "magic numbers" within your code (e.g. the IR codes), and use properly named constants instead.
before
is hard to understand, give it a name that makes sense. I suggestleds_are_on
. And make it abool
, because it can only be eithertrue
orfalse
.- One way to avoid deeply nested loops is to use
return
statements to get out ofloop()
as soon as you know you have nothing to do.
With these suggestions, I would start the program like this:
#include <IRremote.h>
// Pinout.
const int RECV_PIN = 12; // data out of IR receiver
const int RED_PIN = 11;
const int GREEN_PIN = 10;
const int BLUE_PIN = 9;
// Number of steps between black and full brightness.
const int STEPS = 5;
// IR codes.
const uint32_t CODE_ON_OFF = 0xb54a3ac5;
const uint32_t CODE_LESS_RED = 0xb54a8a75;
const uint32_t CODE_MORE_RED = 0xb54a7a85;
const uint32_t CODE_LESS_GREEN = 0xb54ab24d;
const uint32_t CODE_MORE_GREEN = 0xb54a32cd;
const uint32_t CODE_LESS_BLUE = 0xb54a0af5;
const uint32_t CODE_MORE_BLUE = 0xb54aca35;
const uint32_t CODE_YELLOW = 0xb54aa956;
const uint32_t CODE_PURPLE = 0xb54a2ad5;
const uint32_t CODE_AQUA = 0xb54aea15;
bool leds_are_on;
int r_bright;
int g_bright;
int b_bright;
IRrecv irrecv(RECV_PIN);
and the loop()
would be like this:
void loop()
{
decode_results results;
if (!irrecv.decode(&results))
return;
// If the LEDs are OFF, the only valid action is to turn them
// ON.
if (!leds_are_on) {
if (results.value == CODE_ON_OFF) {
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
leds_are_on = true;
}
else {
irrecv.resume();
return;
}
}
// The LEDs are ON: all codes are valid.
switch (results.value) {
case CODE_ON_OFF: // turn OFF
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
leds_are_on = false;
break;
case CODE_LESS_RED:
r_bright = constrain(r_bright - 255/steps, 0, 255);
analogWrite(RED_PIN, r_bright);
break;
case CODE_MORE_RED:
r_bright = constrain(r_bright + 255/steps, 0, 255);
analogWrite(RED_PIN, r_bright);
break;
// etc...
}
irrecv.resume();
}
Edit: After reading your code, I have a few suggestions for making it clearer are more efficient:
- Declare all the constants with the
const
keyword, at the beginning of your program. A common convention is to write them in all caps with underscores, to distinguish them from variables. - Avoid "magic numbers" within your code (e.g. the IR codes), and use properly named constants instead.
before
is hard to understand, give it a name that makes sense. I suggestleds_are_on
. And make it abool
, because it can only be eithertrue
orfalse
.- One way to avoid deeply nested loops is to use
return
statements to get out ofloop()
as soon as you know you have nothing to do.
With these suggestions, I would start the program like this:
#include <IRremote.h>
// Pinout.
const int RECV_PIN = 12; // data out of IR receiver
const int RED_PIN = 11;
const int GREEN_PIN = 10;
const int BLUE_PIN = 9;
// Number of steps between black and full brightness.
const int STEPS = 5;
// IR codes.
const uint32_t CODE_ON_OFF = 0xb54a3ac5;
const uint32_t CODE_LESS_RED = 0xb54a8a75;
const uint32_t CODE_MORE_RED = 0xb54a7a85;
const uint32_t CODE_LESS_GREEN = 0xb54ab24d;
const uint32_t CODE_MORE_GREEN = 0xb54a32cd;
const uint32_t CODE_LESS_BLUE = 0xb54a0af5;
const uint32_t CODE_MORE_BLUE = 0xb54aca35;
const uint32_t CODE_YELLOW = 0xb54aa956;
const uint32_t CODE_PURPLE = 0xb54a2ad5;
const uint32_t CODE_AQUA = 0xb54aea15;
bool leds_are_on;
int r_bright;
int g_bright;
int b_bright;
IRrecv irrecv(RECV_PIN);
and the loop()
would be like this:
void loop()
{
decode_results results;
if (!irrecv.decode(&results))
return;
// If the LEDs are OFF, the only valid action is to turn them
// ON.
if (!leds_are_on) {
if (results.value == CODE_ON_OFF) {
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
leds_are_on = true;
}
else {
irrecv.resume();
return;
}
}
// The LEDs are ON: all codes are valid.
switch (results.value) {
case CODE_ON_OFF: // turn OFF
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
leds_are_on = false;
break;
case CODE_LESS_RED:
r_bright = constrain(r_bright - 255/steps, 0, 255);
analogWrite(RED_PIN, r_bright);
break;
case CODE_MORE_RED:
r_bright = constrain(r_bright + 255/steps, 0, 255);
analogWrite(RED_PIN, r_bright);
break;
// etc...
}
irrecv.resume();
}
The errors are self-explanatory. You just have to read them carefully.
‘B54A3AC5’ was not declared in this scope
The compiler doesn't know what "B54A3AC5" means. Nor do I, but I gess
you may mean an hexadecimal 32-bit value. If that's the case, write it
with the prefix "0x", as in 0xb54a3ac5
.
‘redpin’ was not declared in this scope
Same thing, it doesn't know what "redpin" is. Oh, wait, you have
declared a variable named redPin
. Maybe that is what you meant:
"redPin", with an uppercase "P".
a function-definition is not allowed here before ‘{’ token
You tried to define setColor()
within the loop()
function. You
cannot define a function within a function in C++ (well, you can have
lambdas, but that's irrelevant to your problem).
Had you tried to indent your code properly (like I did in my edit), the
error would have been obvious: there is no closing brace to match the
opening one at the start of loop()
.
expected ‘}’ at end of input
Another symptom of the problem above.