const int nColors = 3; // define the number of colors
const int red = 0; // position in the colorPin array for the red pin
const int grn = 1; // position in the colorPin array for the green pin
const int blu = 2; // position in the colorPin array for the blue pin
// Define pin numbers for the RGB LED
const int redPin = 9;
const int grnPin = 10;
const int bluPin = 11;
// Declare the colorPin array globally
const int colorPin[nColors] = {redPin, grnPin, bluPin};
void coloron(int color, int intensity) {
switch (color) {
case red:
analogWrite(colorPin[red], intensity);
break;
case grn:
analogWrite(colorPin[grn], intensity);
break;
case blu:
analogWrite(colorPin[blu], intensity);
break;
}
}
void coloroff(int color) {
analogWrite(colorPin[color], 0);
}
void allColorsOff() {
for (int color = 0; color < nColors; color++) {
coloroff(color);
}
}
void setup() {
pinMode(redPin, OUTPUT);
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
}
void loop() {
// Start turning on and off lights
/*for (int color = 0; color < nColors; color++) {
for (int i = 0; i < 256; i++) { // slowly increase the color
coloron(color, i);
delay(5);
}
for (int i = 255; i > 0; i--) { // slowly decrease the color
coloron(color, i);
delay(5);
}
coloroff(color);
}
delay(500);*/
// Generate custom light
coloron(red, 250);
coloron(grn, 250);
coloron(blu, 23);
delay(2000); // Display cyan for 2 seconds
// Turn off all colors
allColorsOff();
}