const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
int DELAY = 200;
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop() {
displayAllBasicColors();
showSpectrum();
}
void displayAllBasicColors() {
// RED light
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(DELAY);
// YELLOW light
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(DELAY);
// GREEN light
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(DELAY);
}
void showRGB(int color) {
int redPWM;
int greenPWM;
int bluePWM;
if (color <= 255) {
redPWM = 255 - color; // red from on -> off
greenPWM = color; // green from off -> on
bluePWM = 0; // blue always off
}
else if (color <= 511) {
redPWM = 0; // red always off
greenPWM = 255 - (color - 256); //gren from on -> off
bluePWM = (color - 256); //blue from off -> on
}
else {
redPWM = (color - 512); // red from off-> on
greenPWM = 0; //green always off
bluePWM = 255 - (color - 512); // blue from on->off
}
analogWrite(RED_PIN, redPWM);
analogWrite(GREEN_PIN, greenPWM);
analogWrite(BLUE_PIN, bluePWM);
}
void showSpectrum() {
for (int i=0; i < 768; i++) {
showRGB(i);
delay(10);
}
}