Examples > Communication
This sketch uses the Serial.parseInt() function to locate values separated by a non-alphanumeric character. Often people use a comma to indicate different pieces of information (this format is commonly referred to as comma-separated-values), but other characters like a space or a period will work too. The values are parsed into ints and used to determine the color of a RGB LED. You'll use the serial monitor to send strings like "5,220,70" to the Arduino to change the lights.
You'll need five wires to make the circuit above. Connect a red wire to one of the long vertical rows on your breadboard. Connect the other end to the 5V pin on your Arduino.
Place an RGB LED on your breadboard. Check the datasheet for your specific LED to verify the pins. Connect the power rail you just created to the common anode on the LED.
With your remaining wires, connect your red cathode to pin 3, green cathode to pin 5, and blue cathode to pin 6 in series with the resistors.
RGB LEDs with a common anode share a common power pin. Instead of turning a pin HIGH to illuminate the LED, you need to turn the pin LOW, to create a voltage difference across the diode. So sending 255 via analogWrite() turns the LED off, while a value of 0 turns it on at full brightness. In the code below, you'll use a little bit of math on the Arduino side, so you can send values which correspond to the expected brightness. Essentially, instead of using analogWrite(pin, brightness), you'll be calling analogWrite(pin, 255-brightness).
You'll first set up some global variables for the pins your LED will connect to. This will make it easier to differentiate which one is red, green, and blue in the main part of your program:
In your setup(), begin serial communication at 9600 bits of data per second between Arduino and your computer with the line:
Also in the setup, you'll want to configure the pins as outputs:
Next, declare some local variables for storing the serial information. This will be the brightness of the LEDs. Using Serial.parseInt() to separate the data by commas, read the information into your variables:
Once you've read the data into your variables, check for the newline character to proceed:
Using constrain(), you can keep the values in an acceptable range for PWM control. This way, if the value was outside the range of what PWM can send, it will be limited to a valid number. By subtracting this value from 255 you will be formatting the value to use with a common anode LED. As explained above, these LEDs will illuminate when there is a voltage difference between the anode and the pin connected to the Arduino:
Now that you have formatted the values for PWM, use analogWrite() to change the color of the LED. Because you subtracted your value from 255 in the step above:
Send the value of each LED back to the serial monitor in one string as HEX values :
Finally, close up your brackets from the if statement, while statement, and main loop :
Once you have programmed the Arduino, open your Serial minitor. Make sure you have chosen to send a newline character when sending a message. Enter values between 0-255 for the lights in the following format : Red,Green,Blue. Once you have sent the values to the Arduino, the attached LED will turn the color you specified, and you will receive the HEX values in the serial monitor.