// Define your PWM pins for the RGB channels
const int redPin = 0; // Example: PB0
const int greenPin = 1; // Example: PB1
const int bluePin = 4; // Example: PB4
// The wheel function maps a number (0-255) to an RGB color.
void wheel(uint8_t pos, uint8_t &r, uint8_t &g, uint8_t &b) {
if (pos < 85) {
r = pos * 3; // Increases red from 0 to 255
g = 255 - pos * 3; // Decreases green from 255 to 0
b = 0;
} else if (pos < 170) {
pos -= 85;
r = 255 - pos * 3; // Decreases red from 255 to 0
g = 0;
b = pos * 3; // Increases blue from 0 to 255
} else {
pos -= 170;
r = 0;
g = pos * 3; // Increases green from 0 to 255
b = 255 - pos * 3; // Decreases blue from 255 to 0
}
}
void setup() {
// Set the RGB pins as outputs.
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// Cycle through the 256 positions for a full rainbow cycle.
for (int i = 0; i < 256; i++) {
uint8_t r, g, b;
wheel(i, r, g, b); // Get the color for this position
analogWrite(redPin, r); // Set the red channel
analogWrite(greenPin, g); // Set the green channel
analogWrite(bluePin, b); // Set the blue channel
delay(10); // Adjust delay for speed of cycle
}
}