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.