/* Pin initialization, setting up pins 3, 5, 6 for connecting the anode of RGB LED, use any PWM pin to connect the anode of RGB but have to mention the correct pin number to initialize it for connecting it anode for RGB LED "Analog pins can also be used instead of PWM pins in digital side" the PWM pins in the digital i/o side will be marked with a symbol '~' */ const int RGB_red=3; const int RGB_green=5; const int RGB_blue=6; /* Initializing three empty variables to store the RGB value received from the Serial communication */ int red=0; int green=0; int blue=0; void setup() { Serial.begin(9600); //Begin the serial communication pinMode(RGB_red, OUTPUT); //Initialize RGB_red pin as output pinMode(RGB_green, OUTPUT); //Initialize RGB_green pin as output pinMode(RGB_blue, OUTPUT); //Initialize RGB_blue pin as output } void loop() { while(Serial.available()>0) // Until data is present in serial port this "while" statement executes { red=Serial.parseInt(); // detect and store the first longest variable green=Serial.parseInt(); // detect and store the first longest variable blue=Serial.parseInt(); // detect and store the first longest variable Serial.print(red); Serial.print(green); Serial.print(blue); if(Serial.read()!='\n') { red=255-constrain(red, 0, 255); // Include this code if your LED is Common Cathode green=255-constrain(green, 0, 255); // Include this code if your LED is Common Cathode blue=255-constrain(blue, 0, 255); // Include this code if your LED is common Cathode analogWrite(RGB_red, red); analogWrite(RGB_green, green); analogWrite(RGB_blue, blue); } } }