Devices & Components
Arduino Nano
Slider potentiometer
IRLZ44N
Analog RGB led strip
Software & Tools
Arduino IDE
Project description
Code
Code to Cycle Primary Colors
cpp
Cycle through red, green, and blue, one
1const int redPin = 6; 2const int greenPin = 9; 3const int bluePin = 3; 4 5void setup() { 6pinMode(redPin, OUTPUT); 7pinMode(greenPin, OUTPUT); 8pinMode(bluePin, OUTPUT); 9 10// Turn all channels off (common anode: HIGH = off) 11digitalWrite(redPin, HIGH); 12digitalWrite(greenPin, HIGH); 13digitalWrite(bluePin, HIGH); 14} 15 16void loop() { 17// Red 18digitalWrite(redPin, LOW); 19digitalWrite(greenPin, HIGH); 20digitalWrite(bluePin, HIGH); 21delay(1000); 22 23// Green 24digitalWrite(redPin, HIGH); 25digitalWrite(greenPin, LOW); 26digitalWrite(bluePin, HIGH); 27delay(1000); 28 29// Blue 30digitalWrite(redPin, HIGH); 31digitalWrite(greenPin, HIGH); 32digitalWrite(bluePin, LOW); 33delay(1000); 34}
Cycle through 9 defined colors
cpp
Loops through color table changing color every 2s
1#define RED_PIN 6 2#define GREEN_PIN 3 // Controls actual green 3#define BLUE_PIN 9 // Controls actual blue 4 5// Array of RGB color values 6const byte colors[9][3] = { 7 {255, 0, 0}, // Red 8 { 0, 255, 0}, // Green 9 { 0, 0, 255}, // Blue 10 {255, 160, 16}, // Orange 11 {255, 255, 0}, // Yellow 12 { 0, 255, 255}, // Cyan 13 {255, 0, 255}, // Magenta 14 {255, 105, 180}, // Hot Pink 15 {255, 255, 255} // White 16}; 17 18void setup() { 19 pinMode(RED_PIN, OUTPUT); 20 pinMode(GREEN_PIN, OUTPUT); 21 pinMode(BLUE_PIN, OUTPUT); 22} 23 24void setColorPWM(byte r, byte g, byte b) { 25 // Invert values for common anode strip 26 analogWrite(RED_PIN, 255 - r); 27 analogWrite(GREEN_PIN, 255 - g); 28 analogWrite(BLUE_PIN, 255 - b); 29} 30 31void loop() { 32 for (int i = 0; i < 9; i++) { 33 setColorPWM(colors[i][0], 34 colors[i][1], 35 colors[i][2]); 36 delay(2000); // Show each color for 2 seconds 37 } 38}
Code creating smooth color fades
cpp
Smoothly transition between each color
1#define RED_PIN 6 2#define GREEN_PIN 3 // Controls actual green 3#define BLUE_PIN 9 // Controls actual blue 4 5// Array of RGB color values 6const byte colors[9][3] = { 7 {255, 0, 0}, // Red 8 { 0, 255, 0}, // Green 9 { 0, 0, 255}, // Blue 10 {255, 160, 16}, // Orange 11 {255, 255, 0}, // Yellow 12 { 0, 255, 255}, // Cyan 13 {255, 0, 255}, // Magenta 14 {255, 105, 180}, // Hot Pink 15 {255, 255, 255} // White 16}; 17 18void setup() { 19 pinMode(RED_PIN, OUTPUT); 20 pinMode(GREEN_PIN, OUTPUT); 21 pinMode(BLUE_PIN, OUTPUT); 22} 23 24void setColorPWM(byte r, byte g, byte b) { 25 // Invert values for common anode strip 26 analogWrite(RED_PIN, 255 - r); 27 analogWrite(GREEN_PIN, 255 - g); 28 analogWrite(BLUE_PIN, 255 - b); 29} 30 31void loop() { 32 for (int i = 0; i < 9; i++) { 33 setColorPWM(colors[i][0], 34 colors[i][1], 35 colors[i][2]); 36 delay(2000); // Show each color for 2 seconds 37 } 38}
Comments
Only logged in users can leave comments