Getting 'setColor' was not delaired in this scope error

I am using a RGB LED and can get it to light whatever color i want by itself but when I try to program it into this code it doesn't compile. I am trying to get it to light a certain color when the sensor reads a specific value. Thanks in advance for the help.

int speaker = 2;
int led[] = {
  9, 10, 11};
int pressure = A1;
const boolean ON = LOW;
const boolean OFF = HIGH;

const boolean RED[] = {
  ON, OFF, OFF};    
const boolean GREEN[] = {
  OFF, ON, OFF}; 
const boolean BLUE[] = {
  OFF, OFF, ON}; 
const boolean YELLOW[] = {
  ON, ON, OFF}; 
const boolean CYAN[] = {
  OFF, ON, ON}; 
const boolean MAGENTA[] = {
  ON, OFF, ON}; 
const boolean WHITE[] = {
  ON, ON, ON}; 
const boolean BLACK[] = {
  OFF, OFF, OFF};

const boolean* COLORS[] = {
  RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE, BLACK};

void setup() {
  Serial.begin(9600);
  for(int i = 0; i < 3; i++){
    pinMode(led[i], OUTPUT);
  } 
  pinMode(speaker, OUTPUT); 
}
void loop() {


  int value = analogRead(pressure) / 4;
  if((value >= 1) && (value <= 100))

  {
    setColor(led, GREEN);
    digitalWrite(speaker, HIGH);  
  }
  else if(value >= 101)
  {
    setColor(led, RED);
    digitalWrite(speaker, LOW);


  }
  else 
  {
    setColor(led, CYAN);
    digitalWrite(speaker, LOW);
  }
  Serial.println(value);
  delay(100);

}

I guess what I am asking is what I need to change "setColor" to. I tried "digitalWrite" but that didn't work either.

Is that the full code?
Are you using a normal RGB LED?

"setColor()" is part of a library (quite a few actually, most likely the LiquidCrystal library), so even though it is orange, it doesn't mean you can use it in your sketch like that.

What you need to use is the digitalWrite functions or analogWrite, if you want to have full control of the colors.

Yes that is the full code. I used setColor in a code for just the RGB LED and it worked correctly but when I try to use it with the sensor it wont compile the sketch. I have also tried to change it to digitalWrite and it didn't work either.

Show me how you were able to use setColor to change the color of the LED. Post the code.

This is an example sketch for the starter kit i got.

/*     ---------------------------------------------------------
 *     |  Experimentation Kit for Arduino Example Code         |
 *     |  CIRC-RGB .: Colourful Light :. (RGB LED)             |
 *     ---------------------------------------------------------
 * 
 * We've blinked an LED and controlled eight in sequence now it's time to 
 * control colour. Using an RGB LED (actual 3 LEDs in a single housing)  
 * we can generate any colour our heart desires.
 *
 * (we'll also use a few programming shortcuts to make the code 
 * more portable/readable)
 */


//RGB LED pins
int ledDigitalOne[] = {
  9, 10, 11}; //the three digital pins of the digital LED 
//9 = redPin, 10 = greenPin, 11 = bluePin

const boolean ON = LOW;     //Define on as LOW (this is because we use a common 
//Anode RGB LED (common pin is connected to +5 volts)
const boolean OFF = HIGH;   //Define off as HIGH

//Predefined Colors
const boolean RED[] = {
  ON, OFF, OFF};    
const boolean GREEN[] = {
  OFF, ON, OFF}; 
const boolean BLUE[] = {
  OFF, OFF, ON}; 
const boolean YELLOW[] = {
  ON, ON, OFF}; 
const boolean CYAN[] = {
  OFF, ON, ON}; 
const boolean MAGENTA[] = {
  ON, OFF, ON}; 
const boolean WHITE[] = {
  ON, ON, ON}; 
const boolean BLACK[] = {
  OFF, OFF, OFF}; 

//An Array that stores the predefined colors (allows us to later randomly display a color)
const boolean* COLORS[] = {
  RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE, BLACK};

void setup(){
  for(int i = 0; i < 3; i++){
    pinMode(ledDigitalOne[i], OUTPUT);   //Set the three LED pins as outputs
  }
}

void loop(){

  /* Example - 1 Set a color
   Set the three LEDs to any predefined color
   */
  setColor(ledDigitalOne, GREEN);    //Set the color of LED one

  /* Example - 2 Go through Random Colors
   Set the LEDs to a random color
   */
  //randomColor();

}

void randomColor(){
  int rand = random(0, sizeof(COLORS) / 2);  //get a random number within the range of colors
  setColor(ledDigitalOne, COLORS[rand]);  //Set the color of led one to a random color
  delay(1000);
}

/* Sets an led to any color
 led - a three element array defining the three color pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin)
 color - a three element boolean array (color[0] = red value (LOW = on, HIGH = off), color[1] = green value, color[2] =blue value)
 */
void setColor(int* led, boolean* color){
  for(int i = 0; i < 3; i++){
    digitalWrite(led[i], color[i]);
  }
}

/* A version of setColor that allows for using const boolean colors
 */
void setColor(int* led, const boolean* color){
  boolean tempColor[] = {
    color[0], color[1], color[2]  };
  setColor(led, tempColor);
}

setColor() is a function. You could not have set the LED colors with the first code you posted, because you have no function called setColor() in it. There is one in the second code you posted.

Since I am new to this I am a bit confused. I tried to add the function to the first code i posted from the second code but it still didn't work. Would there be an easier way to control the RGB LED and get the colors i want?

What do you mean by "it didn't work"?

It didn't compile, or when compiled, it didn't do what you wanted it to?

Can you show what you tried and what happened?

It didnt compile. I tried to copy the bottom section of the code with the three "void" statements and I got tons of errors when it tried to compile.

So, post the code that didn't compile, preferably with the errors you got.

int speaker = 2;
int led[] = {
  9, 10, 11};
int pressure = A1;
const boolean ON = LOW;
const boolean OFF = HIGH;

const boolean RED[] = {
  ON, OFF, OFF};    
const boolean GREEN[] = {
  OFF, ON, OFF}; 
const boolean BLUE[] = {
  OFF, OFF, ON}; 
//const boolean YELLOW[] = {
  //ON, ON, OFF}; 
//const boolean CYAN[] = {
  //OFF, ON, ON}; 
//const boolean MAGENTA[] = {
  //ON, OFF, ON}; 
//const boolean WHITE[] = {
  //ON, ON, ON}; 
//const boolean BLACK[] = {
  //OFF, OFF, OFF};

//const boolean* COLORS[] = {
  //RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE, BLACK};

void setup() {
  Serial.begin(9600);
  for(int i = 0; i < 3; i++){
    pinMode(led[i], OUTPUT);
  } 
  pinMode(speaker, OUTPUT); 
}
void loop() {


  int value = analogRead(pressure) / 4;
 
  if((value >= 1) && (value <= 100))

  {
    setColor(led, GREEN);
    digitalWrite(speaker, HIGH);  
  }
  else if(value >= 101)
  {
    setColor(led, RED);
    digitalWrite(speaker, LOW);


  }
  else 
  {
    setColor(led, BLUE);
    digitalWrite(speaker, LOW);
  }
  Serial.println(value);
  delay(100);
void randomColor(){
  int rand = random(0, sizeof(COLORS) / 2);  //get a random number within the range of colors
  setColor(ledDigitalOne, COLORS[rand]);  //Set the color of led one to a random color
  delay(1000);
}

/* Sets an led to any color
 led - a three element array defining the three color pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin)
 color - a three element boolean array (color[0] = red value (LOW = on, HIGH = off), color[1] = green value, color[2] =blue value)
 */
void setColor(int* led, boolean* color){
  for(int i = 0; i < 3; i++){
    digitalWrite(led[i], color[i]);
  }
}

/* A version of setColor that allows for using const boolean colors
 */
void setColor(int* led, const boolean* color){
  boolean tempColor[] = {
    color[0], color[1], color[2]  };
  setColor(led, tempColor);
}

work_project_with_multiple_leds_and_buzzer.ino: In function 'void loop()':
work_project_with_multiple_leds_and_buzzer:43: error: 'setColor' was not declared in this scope
work_project_with_multiple_leds_and_buzzer:48: error: 'setColor' was not declared in this scope
work_project_with_multiple_leds_and_buzzer:55: error: 'setColor' was not declared in this scope
work_project_with_multiple_leds_and_buzzer:60: error: a function-definition is not allowed here before '{' token
work_project_with_multiple_leds_and_buzzer:70: error: a function-definition is not allowed here before '{' token
work_project_with_multiple_leds_and_buzzer:83: error: expected `}' at end of input

  Serial.println(value);
  delay(100);
void randomColor(){

You're starting to define a new function before you've finished the old one.
You're probably missing a } there.

Thank you so much for the help. That was the problem. Like I have said before I am new to this so I miss the little things once in a while. But I am learning with all of the help.

I am having similar problems. I had the code working fine and then I came back this morning and it will not compile. I am new to this and I will work out some arrays later for concentrating the code. But here is the code. I am getting the "setColor" not declared in the second "if" statement. Thanks for any help.

int redLED1 = 2;
int redLED2 = 4;
int redLED3 = 6;
int redLED4 = 8;
int redLED5 = 10;
int redLED6 = 12;
int redLED7 = A0;
int redLED8 = A2;

int blueLED1 = 3;
int blueLED2 = 5;
int blueLED3 = 7;
int blueLED4 = 9;
int blueLED5 = 11;
int blueLED6 = 13;
int blueLED7 = A1;
int blueLED8 = A3;

int button =A5;
int button2= A4;

int i=25;
int count = 0;

void setup()
{
pinMode(redLED1, OUTPUT);
pinMode(redLED2, OUTPUT);
pinMode(redLED3, OUTPUT);
pinMode(redLED4, OUTPUT);
pinMode(redLED5, OUTPUT);
pinMode(redLED6, OUTPUT);
pinMode(redLED7, OUTPUT);
pinMode(redLED8, OUTPUT);

pinMode(blueLED1, OUTPUT);
pinMode(blueLED2, OUTPUT);
pinMode(blueLED3, OUTPUT);
pinMode(blueLED4, OUTPUT);
pinMode(blueLED5, OUTPUT);
pinMode(blueLED6, OUTPUT);
pinMode(blueLED7, OUTPUT);
pinMode(blueLED8, OUTPUT);

pinMode(button,INPUT);
pinMode(button2,INPUT);

}

void loop()
{

if(digitalRead(button) ==HIGH)
{
setColor(0, 0, 255);

}
else {
setColor(255, 255, 255);
}

if(digitalRead(button2) ==HIGH)
{
setColor(255,0,0);
}
else {
setColor(255,255,255);}

}

}

void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redLED1, redValue);
analogWrite(redLED2, redValue);
analogWrite(redLED3, redValue);
analogWrite(redLED4, redValue);
analogWrite(redLED5, redValue);
analogWrite(redLED6, redValue);
analogWrite(redLED7, redValue);
analogWrite(redLED8, redValue);

analogWrite(blueLED1, blueValue);
analogWrite(blueLED2, blueValue);
analogWrite(blueLED3, blueValue);
analogWrite(blueLED4, blueValue);
analogWrite(blueLED5, blueValue);
analogWrite(blueLED6, blueValue);
analogWrite(blueLED7, blueValue);
analogWrite(blueLED8, blueValue);

}

Try Auto Formatting your code in the IDE

You have an extra } at the end of the loop() function. It would be easier to see the problem if you deleted the excessive blank lines in your code

Count auto-format your code (ctrl-t in Arduino IDE). Then count your '{' '}' pairs.

OMG, Thank you guys. I had a bunch of code in there and I deleted it thinking that caused the error. Thats where the extra space came in. Learning curves suck, but I'm working on it. Next I get to learn and play with millis. I tried to get the LED to blink with delays but that was a wash. I want the red LED to blink 3 times then go solid. Any code you provide would be helpful.

An example to show you how you might do what you want

const byte ledPin = 3;
byte blinkCount = 6;
unsigned long ledStartTime;
unsigned long currentTime;
unsigned long period = 1000;

void setup()
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}

void loop()
{
  if (blinkCount != 0)
  {
    blinkLed();
  }
}

void blinkLed()
{
  currentTime = millis();
  if (currentTime - ledStartTime < period)
  {
    return;
  }
  else
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    ledStartTime = currentTime;
    blinkCount--;
  }
}

@irobodude

Glad you used a search instead of immediately opening a new thread.

However, this thread is 4.5 years old; it would therefore have been better if you would have started a new one.