Not declared in this scope.

Preamble: I'm a teacher and want to use an RGB LED to show colour mixing. I will have three swtiches, one for each colour. My code will light the LED with a mixture of RGB to give cyan, magenta, yellow and white. Ultimately I will have several of these for students to use.

Here is my code so far. I have tried declaring the variables before void setup() and, as is now, within it. It looks similar to other sketches I've seen so far, so I'm now confused.

void setup() {
  // put your setup code here, to run once:

// need to use pwm pins for the LEDs
int redLed = 11;
int greenLed = 10;
int blueLed = 9;

// Now, the switches
int redSwitch = 4;
int greenSwitch = 3;
int blueSwitch = 2;

//finally, the brightness
int redBright = 100;
int greenBright = 100;
int blueBright = 100;


pinMode (redLed, OUTPUT); // LED pinmodes
pinMode (greenLed, OUTPUT);
pinMode (blueLed, OUTPUT);

pinMode (redSwitch, INPUT); //Switches
pinMode (greenSwitch, INPUT);
pinMode (blueSwitch, INPUT);


}

void loop() {
  // put your main code here, to run repeatedly:

  if (digitalRead(redSwitch) == HIGH) {

analogWrite(redLED, redBright);

  }
}

redLed is not the same as redLED

you need to understand C++ Variable Scope

manor_royal:
redLed is not the same as redLED

Ooops! Thanks for that. The error was at redSwitch, though.

BulldogLowell:
you need to understand C++ Variable Scope

That too, but if OP puts the declarations back above setup() where he said they were to start with, it won't help until he sorts the led/LED thing.

MisterG:
The error was at redSwitch, though.

This compiles wit the Led/LED fix and all declarations above setup() which I thought is where you had them originally?

// need to use pwm pins for the LEDs
int redLed = 11;
int greenLed = 10;
int blueLed = 9;

// Now, the switches
int redSwitch = 4;
int greenSwitch = 3;
int blueSwitch = 2;

//finally, the brightness
int redBright = 100;
int greenBright = 100;
int blueBright = 100;

void setup() {
  // put your setup code here, to run once:

pinMode (redLed, OUTPUT); // LED pinmodes
pinMode (greenLed, OUTPUT);
pinMode (blueLed, OUTPUT);

pinMode (redSwitch, INPUT); //Switches
pinMode (greenSwitch, INPUT);
pinMode (blueSwitch, INPUT);


}

void loop() {
  // put your main code here, to run repeatedly:

  if (digitalRead(redSwitch) == HIGH) {

analogWrite(redLed, redBright);

  }
}

BulldogLowell:
you need to understand C++ Variable Scope

Thank you for that. I moved the declarations to the beginning again and it compiles without issue. I'm thinking the original error was the typo pointed out by @manor_royal!

Thank you

REPLY DELAYED DUE TO TOO MANY POSTS IN 5 MINUTES :frowning:

By the way, do you have a photo of your RGB LED? The only ones I could get recently were clear, so no matter what mix of RGB I send, I can see 3x separate LEDs and no nice mix of colour. I sandpapered the plastic which helped some but not much.

Are yours diffused?

manor_royal:
By the way, do you have a photo of your RGB LED? The only ones I could get recently were clear, so no matter what mix of RGB I send, I can see 3x separate LEDs and no nice mix of colour.

My idea comes from a YouTube video I saw. The person doing this had the LED inside a ping pong ball, giving a large, diffused light. That's the reason for my brightness variables - I can alter these to make the light brighter and mix properly (although I am considering cheating and having a statement such as if red and blue switches are both pressed light the LED with magenta, rather than adding the two colours as is).