/* Red Green Blue Blaster */
int redpin = 11; // select the pin for the red LED
int bluepin = 10; // select the pin for the blue LED
int greenpin = 9; // select the pin for the green LED
int val; // variable to store brightness level
void setup() {
pinMode(redpin, OUTPUT); // set red LED pin as output
pinMode(bluepin, OUTPUT); // set blue LED pin as output
pinMode(greenpin, OUTPUT);// set green LED pin as output
Serial.begin(9600); // initialize serial communication at 9600 baud rate
}
/*The value 255 is chosen because Arduino's analogWrite() uses 8-bit PWM, which ranges from 0 to 255.
It allows for 256 levels of brightness control, with 255 representing full brightness.*/
void loop() {
// Fade from fully bright to off
for(val = 255; val > 0; val--) // decrease brightness value from 255 to 0
{
analogWrite(11, val); // set the brightness of red LED
analogWrite(10, 255 - val); // set the brightness of blue LED (inversely proportional)
analogWrite(9, 128 - val); // set the brightness of green LED (range from 128 to 0)
Serial.println(val, DEC); // print the current brightness value in decimal format to Serial Monitor
delay(50); // short delay for smooth fading effect
}
// Fade from off to fully bright
for(val = 0; val < 255; val++) // increase brightness value from 0 to 255
{
analogWrite(11, val); // set the brightness of red LED
analogWrite(10, 255 - val); // set the brightness of blue LED (inversely proportional)
analogWrite(9, 128 - val); // set the brightness of green LED (range from 0 to 128)
Serial.println(val, DEC); // print the current brightness value in decimal format to Serial Monitor
delay(50); // short delay for smooth fading effect
}
}