| 1 |
/* |
| 2 |
This example reads three analog sensors (potentiometers are easiest) and sends |
| 3 |
their values serially. The Processing and Max/MSP programs at the bottom take |
| 4 |
those three values and use them to change the background color of the screen. |
| 5 |
|
| 6 |
The circuit: |
| 7 |
- potentiometers attached to analog inputs 0, 1, and 2 |
| 8 |
|
| 9 |
created 2 Dec 2006 |
| 10 |
by David A. Mellis |
| 11 |
modified 30 Aug 2011 |
| 12 |
by Tom Igoe and Scott Fitzgerald |
| 13 |
|
| 14 |
This example code is in the public domain. |
| 15 |
|
| 16 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/VirtualColorMixer |
| 17 |
*/ |
| 18 |
|
| 19 |
const int redPin = A0; // sensor to control red color |
| 20 |
const int greenPin = A1; // sensor to control green color |
| 21 |
const int bluePin = A2; // sensor to control blue color |
| 22 |
|
| 23 |
void setup() { |
| 24 |
Serial.begin(9600); |
| 25 |
} |
| 26 |
|
| 27 |
void loop() { |
| 28 |
Serial.print(analogRead(redPin)); |
| 29 |
Serial.print(","); |
| 30 |
Serial.print(analogRead(greenPin)); |
| 31 |
Serial.print(","); |
| 32 |
Serial.println(analogRead(bluePin)); |
| 33 |
} |
| 34 |
|
| 35 |
/* Processing code for this example |
| 36 |
|
| 37 |
// This example code is in the public domain. |
| 38 |
|
| 39 |
import processing.serial.*; |
| 40 |
|
| 41 |
float redValue = 0; // red value |
| 42 |
float greenValue = 0; // green value |
| 43 |
float blueValue = 0; // blue value |
| 44 |
|
| 45 |
Serial myPort; |
| 46 |
|
| 47 |
void setup() { |
| 48 |
size(200, 200); |
| 49 |
|
| 50 |
// List all the available serial ports |
| 51 |
// if using Processing 2.1 or later, use Serial.printArray() |
| 52 |
println(Serial.list()); |
| 53 |
|
| 54 |
// I know that the first port in the serial list on my Mac is always my |
| 55 |
// Arduino, so I open Serial.list()[0]. |
| 56 |
// Open whatever port is the one you're using. |
| 57 |
myPort = new Serial(this, Serial.list()[0], 9600); |
| 58 |
// don't generate a serialEvent() unless you get a newline character: |
| 59 |
myPort.bufferUntil('\n'); |
| 60 |
} |
| 61 |
|
| 62 |
void draw() { |
| 63 |
// set the background color with the color values: |
| 64 |
background(redValue, greenValue, blueValue); |
| 65 |
} |
| 66 |
|
| 67 |
void serialEvent(Serial myPort) { |
| 68 |
// get the ASCII string: |
| 69 |
String inString = myPort.readStringUntil('\n'); |
| 70 |
|
| 71 |
if (inString != null) { |
| 72 |
// trim off any whitespace: |
| 73 |
inString = trim(inString); |
| 74 |
// split the string on the commas and convert the resulting substrings |
| 75 |
// into an integer array: |
| 76 |
float[] colors = float(split(inString, ",")); |
| 77 |
// if the array has at least three elements, you know you got the whole |
| 78 |
// thing. Put the numbers in the color variables: |
| 79 |
if (colors.length >= 3) { |
| 80 |
// map them to the range 0-255: |
| 81 |
redValue = map(colors[0], 0, 1023, 0, 255); |
| 82 |
greenValue = map(colors[1], 0, 1023, 0, 255); |
| 83 |
blueValue = map(colors[2], 0, 1023, 0, 255); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
*/ |
| 89 |
|
| 90 |
/* Max/MSP patch for this example |
| 91 |
|
| 92 |
----------begin_max5_patcher---------- |
| 93 |
1512.3oc4Z00aaaCE8YmeED9ktB35xOjrj1aAsXX4g8xZQeYoXfVh1gqRjdT |
| 94 |
TsIsn+2K+PJUovVVJ1VMdCAvxThV7bO7b48dIyWtXxzkxaYkSA+J3u.Sl7kK |
| 95 |
lLwcK6MlT2dxzB5so4zRW2lJXeRt7elNy+HM6Vs61uDDzbOYkNmo02sg4euS |
| 96 |
4BSede8S2P0o2vEq+aEKU66PPP7b3LPHDauPvyCmAvv4v6+M7L2XXF2WfCaF |
| 97 |
lURgVPKbCxzKUbZdySDUEbgABN.ia08R9mccGYGn66qGutNir27qWbg8iY+7 |
| 98 |
HDRx.Hjf+OPHCQgPdpQHoxhBlwB+QF4cbkthlCRk4REnfeKScs3ZwaugWBbj |
| 99 |
.PS+.qDPAkZkgPlY5oPS4By2A5aTLFv9pounjsgp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Gzp20adYSmIOGjIABo1xZyAWJtCX9tg6+HMuhMCPyx76ao+Us |
| 116 |
UxmzUE79H8d2ZB1m1ztbnOa1mGeAq0awyK8a9UqBUc6pZolpzurTK232e5gp |
| 117 |
aInVw8QIIcpaiNSJfY4Z+92Cs+Mc+mgg2cEsvGlLY6V+1kMuioxnB5VM+fsY |
| 118 |
9vSu4WI1PMBGXye6KXvNuzmZTh7U9h5j6vvASdngPdgOFxycNL6ia1axUMmT |
| 119 |
JIzebXcQCn3SKMf+4QCMmOZung+6xBCPLfwO8ngcEI52YJ1y7mx3CN9xKUYU |
| 120 |
bg7Y1yXjlKW6SrZnguQdsSfOSSDItqv2jwJFjavc1vO7OigyBr2+gDYorRk1 |
| 121 |
HXZpVFfu2FxXkZtfp4RQqNkX5y2sya3YYL2iavWAOaizH+pw.Ibg8f1I9h3Z |
| 122 |
2B79sNeOHvBOtfEalWsvyu0KMf015.AaROvZ7vv5AhnndfHLbTgjcCK1KlHv |
| 123 |
gOk5B26OqrXjcJ005.QqCHn8fVTxnxfj93SfQiJlv8YV0VT9fVUwOOhSV3uD |
| 124 |
eeqCUClbBPa.j3vWDoMZssNTzRNEnE6gYPXazZaMF921syaLWyAeBXvCESA8 |
| 125 |
ASi6Zyw8.RQi65J8ZsNx3ho93OhGWENtWpowepae4YhCFeLErOLENtXJrOSc |
| 126 |
iadi39rf4hwc8xdhHz3gn3dBI7iDRlFe8huAfIZhq |
| 127 |
-----------end_max5_patcher----------- |
| 128 |
|
| 129 |
*/ |