0

Basically what i am trying to do is use an ir remote to turn an rgb on/off and have a few preset colors as well as being able to increase/decrease red/green/blue independently. I don't know much about C/C++ so this is really hard to troubleshoot.

#include <IRremote.h>

int rbright;
int gbright;
int bbright;
int before;
int RECV_PIN=12; //data out of IR receiver connects to pin 12
int redPin=11;
int greenPin=10;
int bluePin=9;
int steps=5;
IRrecv irrecv(RECV_PIN);
decode_results results;

//uncomment this line if using a Common Anode LED
#define COMMON_ANODE

void setup()
{
    before=0;
    rbright=80;
    gbright=0;
    bbright=80;
    irrecv.enableIRIn(); // start the receiver
    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
    pinMode(bluePin, OUTPUT);
}

void loop()
{
    if (irrecv.decode(&results)) {
        if (results.value==B54A3AC5){ //Code to turn the LED ON/OFF
            if(before==0){ // if the LED was turned off, then we turn it on
                digitalWrite(redpin,HIGH);
                digitalWrite(greenpin,HIGH);
                digitalWrite(bluepin,HIGH);
                before=1; //LED is now turned on
            }
            else{
                digitalWrite(redpin,LOW);
                digitalWrite(greenpin,LOW);
                digitalWrite(bluepin,LOW); //if the LED was turned on, then we turn it off
                before=0;
            }
        }
        if (results.value==B54A8A75 && before==1){ //Code to decrease the red
            if(rbright-255/steps<0){
                analogWrite(redpin,rbright);
            }
            else{
                rbright=rbright-255/steps;
                analogWrite(redpin,rbright);
            }
        }
        if (results.value==B54A7A85 && before==1){ //Code to increase the red
            if(rbright+255/steps>255){
                analogWrite(redpin,rbright);
            }
            else{
                rbright=rbright+255/steps;
                analogWrite(redpin,rbright);
            }
        }
        if (results.value==B54AB24D && before==1){ //Code to decrease the green
            if(gbright-255/steps<0){
                analogWrite(greenpin,gbright);
            }
            else{
                gbright=gbright-255/steps;
                analogWrite(greenpin,gbright);
            }
        }
        if (results.value==B54A32CD && before==1){ //Code to increase the green
            if(gbright+255/steps>255){
                analogWrite(greenpin,gbright);
            }
            else{
                gbright=gbright+255/steps;
                analogWrite(greenpin,gbright);
            }
        }
        if (results.value==B54A0AF5 && before==1){ //Code to decrease the blue
            if(bbright-255/steps<0){
                analogWrite(bluepin,bbright);
            }
            else{
                bbright=bbright-255/steps;
                analogWrite(bluepin,bbright);
            }
        }
        if (results.value==B54ACA35 && before==1){ //Code to increase the blue
            if(bbright+255/steps>255){
                analogWrite(bluepin,bbright);
            }
            else{
                bbright=bbright+255/steps;
                analogWrite(bluepin,bbright);
            }
        }
        if (results.value==B54AA956 && before==1){ //Code yellow
            setColor(255, 255, 0);
        }
        if (results.value==B54A2AD5 && before==1){ //Code purple
            setColor(80, 0, 80);
        }
        if (results.value==B54AEA15 && before==1){ //Code aqua
            setColor(0, 255, 255);
        }
        irrecv.resume();
    }

void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
    red = 255 - red;
    green = 255 - green;
    blue = 255 - blue;
#endif
    analogWrite(redPin, red);
    analogWrite(greenPin, green);
    analogWrite(bluePin, blue);
}

The errors i get are

rgbcontrolir_ino.ino: In function ‘void loop()’:
rgbcontrolir_ino.ino:35:24: error: ‘B54A3AC5’ was not declared in this scope
rgbcontrolir_ino.ino:37:20: error: ‘redpin’ was not declared in this scope
rgbcontrolir_ino.ino:38:20: error: ‘greenpin’ was not declared in this scope
rgbcontrolir_ino.ino:39:20: error: ‘bluepin’ was not declared in this scope
rgbcontrolir_ino.ino:43:20: error: ‘redpin’ was not declared in this scope
rgbcontrolir_ino.ino:44:20: error: ‘greenpin’ was not declared in this scope
rgbcontrolir_ino.ino:45:20: error: ‘bluepin’ was not declared in this scope
rgbcontrolir_ino.ino:48:22: error: ‘B54A8A75’ was not declared in this scope
rgbcontrolir_ino.ino:50:19: error: ‘redpin’ was not declared in this scope
rgbcontrolir_ino.ino:54:17: error: ‘redpin’ was not declared in this scope
rgbcontrolir_ino.ino:56:22: error: ‘B54A7A85’ was not declared in this scope
rgbcontrolir_ino.ino:58:19: error: ‘redpin’ was not declared in this scope
rgbcontrolir_ino.ino:62:17: error: ‘redpin’ was not declared in this scope
rgbcontrolir_ino.ino:64:22: error: ‘B54AB24D’ was not declared in this scope
rgbcontrolir_ino.ino:66:19: error: ‘greenpin’ was not declared in this scope
rgbcontrolir_ino.ino:70:17: error: ‘greenpin’ was not declared in this scope
rgbcontrolir_ino.ino:72:22: error: ‘B54A32CD’ was not declared in this scope
rgbcontrolir_ino.ino:74:19: error: ‘greenpin’ was not declared in this scope
rgbcontrolir_ino.ino:78:17: error: ‘greenpin’ was not declared in this scope
rgbcontrolir_ino.ino:80:22: error: ‘B54A0AF5’ was not declared in this scope
rgbcontrolir_ino.ino:82:19: error: ‘bluepin’ was not declared in this scope
rgbcontrolir_ino.ino:86:17: error: ‘bluepin’ was not declared in this scope
rgbcontrolir_ino.ino:88:22: error: ‘B54ACA35’ was not declared in this scope
rgbcontrolir_ino.ino:90:19: error: ‘bluepin’ was not declared in this scope
rgbcontrolir_ino.ino:94:17: error: ‘bluepin’ was not declared in this scope
rgbcontrolir_ino.ino:96:22: error: ‘B54AA956’ was not declared in this scope
rgbcontrolir_ino.ino:100:24: error: ‘B54A2AD5’ was not declared in this scope
rgbcontrolir_ino.ino:104:24: error: ‘B54AEA15’ was not declared in this scope
rgbcontrolir_ino.ino:112:5: error: a function-definition is not allowed here before ‘{’ token
rgbcontrolir_ino.ino:121:5: error: expected ‘}’ at end of input

4
  • > The errors i get are it tells you that those macros are undefined. you need to figure out what they are first before you can fix the errors.
    – dannyf
    Nov 11 '17 at 20:08
  • redpin bluepin etc where defined in the beginning right? Or did i miss something.and the other alpha numeric codes refer to the ir codes from the remote i will use. Nov 11 '17 at 20:10
  • I used the arduino rgb tutorial and the tutorial from great scott on youtube. he used ir library to control led strips with varying brightness. I downloaded the ir libraries and used the irrecvdemo sketch to find the codes on the remote i plan to use Nov 11 '17 at 20:14
  • i feel like it has something to do with the brackets Nov 11 '17 at 20:20
3

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.


Edit: After reading your code, I have a few suggestions for making it clearer are more efficient:

  1. 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.
  2. Avoid "magic numbers" within your code (e.g. the IR codes), and use properly named constants instead.
  3. before is hard to understand, give it a name that makes sense. I suggest leds_are_on. And make it a bool, because it can only be either true or false.
  4. One way to avoid deeply nested loops is to use return statements to get out of loop() 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;
        }
        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();
}
7
  • Thanks I will try some editing and see what I can come up with. the Arduino ide isn't very friendly for writing code. The only code writing exp i have is with BASH scripting. Usually I use a text editor and save the file with the correct prefix and that helps. Nov 11 '17 at 20:50
  • Thanks again! This is a lot of help. After reading your first answer i got the code to compile without errors however the code didnt work as expected. it only turned on/off with full brightness not the purple i was expecting. Is there a good site that will define things for me? specifically I don't know what "case" does Nov 11 '17 at 22:35
  • @KeithBybee: Any book on C or C++ will tell you about switch/case/break. Nov 11 '17 at 23:01
  • After changing over to the formats you suggested I am getting the following error during compilation. "core.a(main.cpp.o): In function main': /usr/share/arduino/hardware/arduino/cores/arduino/main.cpp:11: undefined reference to setup' collect2: error: ld returned 1 exit status " Nov 12 '17 at 3:28
  • @KeithBybee: That means that you forgot to write setup(). Nov 12 '17 at 9:43
0

1) You are missing the braces for void loop()

2) Oops !! You have to double check - redpin and redPin etc. Case sensitive !

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .