HELP! RGB Fade thorugh color spectrum..

Okay, I have this awesome idea for a project (i dont want to spoil the surprise) but what i want is a switch to control whether an RGB LED fades through colors slowly and smoothly or if it flashes randomly. I can do the random flash portion but i dont know how to do the RGB Fade to work.

I Have this sketch that im trying to incoorporate into a different sketch:

But what im trying to do is to add this into my other sketch by doing an if statement, like if(digitalRead(switch1) == LOW) { and then the fade sketch goes here. But when i try to compile it it says the "
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue)"
cannot be put in before the "{" which i dont understand.

If anyone has any help on this, then i would greatly appreciate it, or if you could fix it for me and then message me the code or something, i dont care. I know how to get the random flashing working, just not the fade part.

Oh and below the first sketch is the sketch i tried to incoorporate the fade sketch into when i got that error...

Thanks
Macke

const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;

void setup() {
  // Start off with the LED off.
  setColourRgb(0,0,0);
}

void loop() {
  unsigned int rgbColour[3];

  // Start off with red.
  rgbColour[0] = 255;
  rgbColour[1] = 0;
  rgbColour[2] = 0;  

  // Choose the colours to increment and decrement.
  for (int decColour = 0; decColour < 3; decColour += 1) {
    int incColour = decColour == 2 ? 0 : decColour + 1;

    // cross-fade the two colours.
    for(int i = 0; i < 255; i += 1) {
      rgbColour[decColour] -= 1;
      rgbColour[incColour] += 1;
      
      setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
      delay(5);
    }
  }
}

void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
 }
const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;
const int s = 1;

void setup() {
  // Start off with the LED off.
  setColourRgb(0,0,0);
  pinMode(s, INPUT);
  digitalWrite(s, HIGH);
}

void loop() 
{
  if(digitalRead(s) == HIGH) {  
  
  unsigned int rgbColour[3];

  // Start off with red.
  rgbColour[0] = 255;
  rgbColour[1] = 0;
  rgbColour[2] = 0;  

  // Choose the colours to increment and decrement.
  for (int decColour = 0; decColour < 3; decColour += 1) {
    int incColour = decColour == 2 ? 0 : decColour + 1;

    // cross-fade the two colours.
    for(int i = 0; i < 255; i += 1) {
      rgbColour[decColour] -= 1;
      rgbColour[incColour] += 1;
      
      setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
      delay(5);
    }
  }
}

void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
 }

s is the switch BTW.

You added an if statement with a left curly brace, but didn't add the corresponding right curly brace....

Wow i feel stupid.... But now it says "setColourRgb" was not declared in this scope. I know what that means but how should i declare it?

tmacke17:
Wow i feel stupid.... But now it says "setColourRgb" was not declared in this scope. I know what that means but how should i declare it?

It is declared. It sounds like there was a copy/paste error with whatever you were trying to compile and you didn't copy a section of the code, or you put the curly brace in the wrong spot.

Post your updated code.

Just found a different code that had a lot of like defining but not a lot of void loop so it was easier to insert an if statement. Got is compliled correctly, now just gotta wait for another arduino to come in, got my current one running with christmas lights!

Thanks for all your help anyways!
Macke

check - Color changing RGB based on number of emails - #4 by robtillaart - Project Guidance - Arduino Forum -