An Arduino RGB Color Mixer Controlled from WPF

2010-04-26 08.46.26

This example will facilitate three LED’s, each connected with same-colored wiring to PWM pins on my Arduino. The PWM pins allow for analog-like communication with any LED connected to them, thus giving Arduno the ability to fade in and out an individual color.

This example will make use of each LED using the analogWrite method, supplying a value in the range of 0-255, where 255 turns the light all the way up as bright as it will go.

Rather than supply the values via the serial port window, which is relatively obtuse, this example will make use of the C# programming language and a WPF user inteface to control the LED lights. The effect is a physical RGB color mixer you could use for creating neat lighting effects.

The code for the Arduino will make use of Serial communication, and will parse messages using an external library named – aptly – String. The first thing done by Arduino is to set the pinMode for each of the LED’s connected (see the photograph above).

image

 

The loop method will watch for the incoming serial messages. Whenever it finds any incoming characters it appends them to an instance of a String object (provided via the String reference mentioned earlier) and continues watching.

image

Within the processMessage method the LED to fire is determined, as well as is it’s value. The correct light is activated at the value requested. The result is an individual increase or decrease in the affect of the specific LED on the overall light output.

image

Obviously, we’ll have a reset method in the Arduino code to make sure it can clear the String when the flag is observed.

image

Once the Arduino has the code and is running and connected to a PC the WPF code can also communicate with the Arduino. The GUI will be super-simple; the user will need to control only the RGB values so they’ll have three sliders ranging from 0-255. Each of these slides will control a respective LED on the Arduino. In turn, the LED colors will fade in and out according to the slider values and the color mixing functionality will be achieved.

The XAML code for this is quite simple. It could be made simpler with a WPF style, but for the sake of this example the elegance isn’t what we’re after. Note particularly how the ValueChanged event is bound to an event handler. This code will be examined next.

image

 

Now that the GUI is complete it’ll need to be wired up to the Arduino via a Serial connection. The code for the Window XAML above does most of the work. When the window loads a connection is established. The Event Handler pointed out earlier – Slider_ValueChanged – then performs the work of sending messages to the Arduino whenever a user changes the value of one the sliders.

image

The result is a fully controlled RGB color mixer controlled via a software GUI. Happy coding!

Add a Comment