Hardware Reference
In-Depth Information
Listing 6-8: Processing Sketch to Set Arduino RGB Colors— processing_control_RGB/
processing_control_RGB
import processing.serial.*; //Import serial library
PImage img; //Image object
Serial port; //Serial port object
void setup()
{
size(640,256); //Size of HSV image
img = loadImage("hsv.jpg"); //Load in background image
port = new Serial(this, "COM9", 9600); //Open serial port
}
void draw()
{
background(0); //Black background
image(img,0,0); //Overlay image
}
void mousePressed()
{
color c = get(mouseX, mouseY); //Get the RGB color where mouse was
pressed
String colors = int(red(c))+","+int(green(c))+","+int(blue(c))+"\n"; //
extract
values from color
print(colors); //Print colors for debugging
port.write(colors); //Send values to Arduino
}
When you execute the program, you should see a screen like the one shown in
Figure 6-15 pop up. Click different colors and the RGB values will be transmitted
to the Arduino to control the RGB LED's color. Note that the serial console also
displays the commands being sent to assist you in any debugging.
After you've finished staring at all the pretty colors, look back at the code and
consider how it's working. As before, the serial library is imported and a serial
object called port is created. A PImage object call img is also created. This will
hold the background image. In the setup() , the serial port is initialized, the
display window is set to the size of the image, and image is imported into the
image object by calling img = loadImage(“hsv.jpg”) .
In the draw() function, the image is loaded in the window with image(img,0,0) .
img is the image you want to draw in the window, and 0, 0 are coordinates where
the image will start to be drawn. 0,0 is the top left of the application window.
Search WWH ::




Custom Search