VirtualColorMixer - /* This example reads three analog...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
/* This example reads three analog sensors (potentiometers are easiest) and sends their values serially. The Processing and Max/MSP programs at the bottom take those three values and use them to change the background color of the screen. The circuit: * potentiometers attached to analog inputs 0, 1, and 2 http://www.arduino.cc/en/Tutorial/VirtualColorMixer created 2 Dec 2006 by David A. Mellis modified 4 Sep 2010 by Tom Igoe and Scott Fitzgerald This example code is in the public domain. */ const int redPin = A0; // sensor to control red color const int greenPin = A1; // sensor to control green color const int bluePin = A2; // sensor to control blue color / void setup() { Serial.begin(9600); } void loop() { Serial.print(analogRead(redPin)); Serial.print(","); Serial.print(analogRead(greenPin)); Serial.print(","); Serial.println(analogRead(bluePin)); } /* Processing code for this example // This example code is in the public domain. import processing.serial.*; float redValue = 0; // red value float greenValue = 0; // green value float blueValue = 0; // blue value Serial myPort; void setup() { size(200, 200); // List all the available serial ports println(Serial.list()); // I know that the first port in the serial list on my mac // is always my Arduino, so I open Serial.list()[0]. // Open whatever port is the one you're using. myPort = new Serial(this, Serial.list()[0], 9600); // don't generate a serialEvent() unless you get a newline character: myPort.bufferUntil('\n');
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
} void draw() { // set the background color with the color values: background(redValue, greenValue, blueValue); } void serialEvent(Serial myPort) { // get the ASCII string: String inString = myPort.readStringUntil('\n'); if (inString != null) { // trim off any whitespace: inString = trim(inString); // split the string on the commas and convert the // resulting substrings into an integer array: float[] colors = float(split(inString, ",")); // if the array has at least three elements, you know // you got the whole thing. Put the numbers in the // color variables: if (colors.length >=3) { // map them to the range 0-255: redValue = map(colors[0], 0, 1023, 0, 255); greenValue = map(colors[1], 0, 1023, 0, 255); blueValue = map(colors[2], 0, 1023, 0, 255); } } } */ /* Max/MSP patch for this example { "boxes" : [ { "box" : { "maxclass" : "newobj", "text" : "/ 4", "patching_rect" : [ 448.0, 502.0, 32.5, 20.0 ], "numoutlets" : 1, "fontsize" : 12.0, "outlettype" : [ "int" ], "id" : "obj-25", "fontname" : "Arial", "numinlets" : 2 } } , { "box" : { "maxclass" : "newobj", "text" : "/ 4", "patching_rect" : [ 398.0, 502.0, 32.5, 20.0 ], "numoutlets" : 1, "fontsize" : 12.0, "outlettype" : [ "int" ], "id" : "obj-24", "fontname" : "Arial", "numinlets" : 2 }
Background image of page 2
} , { "box" : { "maxclass" : "newobj", "text" : "/ 4", "patching_rect" : [ 348.0, 502.0, 32.5, 20.0 ], "numoutlets" : 1, "fontsize" : 12.0, "outlettype" : [ "int" ], "id" : "obj-23", "fontname" : "Arial", "numinlets" : 2 } } , { "box" : {
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/31/2011 for the course CS 302 taught by Professor J.parker during the Spring '08 term at University of Texas at Austin.

Page1 / 12

VirtualColorMixer - /* This example reads three analog...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online