// Define the pins for the RGB LED (common anode)
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
// Variables to store the current RGB values
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
// Time interval between color transitions (in milliseconds)
const int transitionInterval = 10;
void setup() {
// Initialize PWM pins for the RGB LED
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// Smoothly transition through all hues of the color wheel
for (int hue = 0; hue < 360; hue++) {
// Calculate RGB values based on current hue
calculateRGBfromHue(hue);
// Update the RGB LED with the new values
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
// Delay for smooth transition
delay(transitionInterval);
}
}
// Function to calculate RGB values from hue
void calculateRGBfromHue(int hue) {
float saturation = 1.0; // Saturation and value are set to 1 for full intensity
float value = 1.0;
int i = int(hue / 60.0);
float f = (hue / 60.0) - i;
float p = value * (1.0 - saturation);
float q = value * (1.0 - saturation * f);
float t = value * (1.0 - saturation * (1.0 - f));
switch(i) {
case 0:
redValue = int(value * 255);
greenValue = int(t * 255);
blueValue = int(p * 255);
break;
case 1:
redValue = int(q * 255);
greenValue = int(value * 255);
blueValue = int(p * 255);
break;
case 2:
redValue = int(p * 255);
greenValue = int(value * 255);
blueValue = int(t * 255);
break;
case 3:
redValue = int(p * 255);
greenValue = int(q * 255);
blueValue = int(value * 255);
break;
case 4:
redValue = int(t * 255);
greenValue = int(p * 255);
blueValue = int(value * 255);
break;
default:
redValue = int(value * 255);
greenValue = int(p * 255);
blueValue = int(q * 255);
break;
}
}