Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Whats wrong with my code?

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

Answer*

Draft saved
Draft discarded
Cancel
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. Commented Nov 11, 2017 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 Commented Nov 11, 2017 at 22:35
  • @KeithBybee: Any book on C or C++ will tell you about switch/case/break. Commented Nov 11, 2017 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 " Commented Nov 12, 2017 at 3:28
  • @KeithBybee: That means that you forgot to write setup(). Commented Nov 12, 2017 at 9:43

AltStyle によって変換されたページ (->オリジナル) /