## **虚拟调色盘**
> 本例向你展示如何从Arduino发送多个值到电脑。从三个电位器上获取的值将被用来给Processing工程或Max/MSP工程设置背景色。
### **所需硬件**
* Arduino板或Genuino板
* 3个模拟信号传感器 (电位器,光敏电阻,压力传感器等均可)
* 3个10kΩ电阻
* 跳线
* 面包板
* 连接线
### **所需软件**
* Processing 或 Max/MSP 5.x
### **电路**
![图片来自官网](http://img.blog.csdn.net/20160520125037683)
将输出模拟信号的传感器分别连接到A0,A1,A2口。
电路在压力传感器(压敏电阻)上使用三个分压电路。分压电路含有两个串联的电阻,根据电学知识,分压电路能够根据两个电阻的阻值进行分压。
### **原理图**
![图片来自官网](http://img.blog.csdn.net/20160520125054321)
### **代码**
传感器值将被以ASCII十进制的类型从Arduino发送到电脑。这就意味着每个值都是用ASCII字符0-9表示的。比如“234”这个数字是这样被发送的:
1. 发送ASCII 的“2”(二进制值50)
2. 发送ASCII 的“3”(二进制值51)
3. 发送ASCII 的“4”(二进制值52)
~~~
/*
本例读取三个模拟信号传感器(用电位器最简单)的值并且用串口发送他们到电脑。下面呈现的Processing和Max/MSP程序会读取Arduino用串口发来的值并且改变屏幕的颜色。
电路搭建:
* (译者注:上文用的是压敏电阻。原文写的器材有冲突,不过其实用压敏电阻和电位器一模一样)
* 电位器分别连接到A0,A1,和A2
代码是公开的。
*/
const int redPin = A0; // 控制红色的传感器
const int greenPin = A1; // 控制绿色的传感器
const int bluePin = A2; // 控制蓝色的传感器
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(analogRead(redPin));
Serial.print(",");
Serial.print(analogRead(greenPin));
Serial.print(",");
Serial.println(analogRead(bluePin));
}
~~~
#### **Processing代码**
![图片来自官网](http://img.blog.csdn.net/20160520125120869)
复制下列代码到 Processing。当你通过某种方式改变模拟信号传感器的值时背景色会改变:
~~~
// 代码公开。
import processing.serial.*;
float redValue = 0; // 红色值
float greenValue = 0; // 绿色值
float blueValue = 0; // 蓝色值
Serial myPort;
void setup() {
size(200, 200);
// 列举所有串口
// 如果用Processing 2.1或其更早版本,将下面代码替换为 Serial.printArray()
println(Serial.list());
// 在我的mac上第一个总归是Arduino,因此我直接打开了Serial.list()[0]。
// 下列代码根据你使用的串口进行调整。
myPort = new Serial(this, Serial.list()[0], 9600);
// 告诉系统:收到换行符后才能调用serialEvent():
myPort.bufferUntil('\n');
}
void draw() {
// 更改背景颜色:
background(redValue, greenValue, blueValue);
}
void serialEvent(Serial myPort) {
// 获取ASCII字符串:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// 去除空格:
inString = trim(inString);
// 解析用逗号分隔的字符串,并且将其存为浮点型数组:
float[] colors = float(split(inString, ","));
// 如果colors数组的成员>=3,颜色数据就至少是完整的了:
if (colors.length >=3) {
// 将值映射到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补丁**
![图片来自官网](http://img.blog.csdn.net/20160520125136181)
请复制下面这一段到Max
~~~
----------begin_max5_patcher----------
1512.3oc4Z00aaaCE8YmeED9ktB35xOjrj1aAsXX4g8xZQeYoXfVh1gqRjdT
TsIsn+2K+PJUovVVJ1VMdCAvxThV7bO7b48dIyWtXxzkxaYkSA+J3u.Sl7kK
lLwcK6MlT2dxzB5so4zRW2lJXeRt7elNy+HM6Vs61uDDzbOYkNmo02sg4euS
4BSede8S2P0o2vEq+aEKU66PPP7b3LPHDauPvyCmAvv4v6+M7L2XXF2WfCaF
lURgVPKbCxzKUbZdySDUEbgABN.ia08R9mccGYGn66qGutNir27qWbg8iY+7
HDRx.Hjf+OPHCQgPdpQHoxhBlwB+QF4cbkthlCRk4REnfeKScs3ZwaugWBbj
.PS+.qDPAkZkgPlY5oPS4By2A5aTLFv9pounjsgpnZVF3x27pqtBrRpJnZaa
C3WxTkfUJYA.BzR.BhIy.ehquw7dSoJCsrlATLckR.nhLPNWvVwL+Vp1LHL.
SjMG.tRaG7OxT5R2c8Hx9B8.w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
JIzebXcQCn3SKMf+4QCMmOZung+6xBCPLfwO8ngcEI52YJ1y7mx3CN9xKUYU
bg7Y1yXjlKW6SrZnguQdsSfOSSDItqv2jwJFjavc1vO7OigyBr2+gDYorRk1
HXZpVFfu2FxXkZtfp4RQqNkX5y2sya3YYL2iavWAOaizH+pw.Ibg8f1I9h3Z
2B79sNeOHvBOtfEalWsvyu0KMf015.AaROvZ7vv5AhnndfHLbTgjcCK1KlHv
gOk5B26OqrXjcJ005.QqCHn8fVTxnxfj93SfQiJlv8YV0VT9fVUwOOhSV3uD
eeqCUClbBPa.j3vWDoMZssNTzRNEnE6gYPXazZaMF921syaLWyAeBXvCESA8
ASi6Zyw8.RQi65J8ZsNx3ho93OhGWENtWpowepae4YhCFeLErOLENtXJrOSc
iadi39rf4hwc8xdhHz3gn3dBI7iDRlFe8huAfIZhq
-----------end_max5_patcher-----------
~~~
### **相关资料**
[serial.begin()](https://www.arduino.cc/en/Serial/Begin)
[serial.print()](https://www.arduino.cc/en/Serial/Print)
[analogRead()](https://www.arduino.cc/en/Reference/AnalogRead)
- 说明
- 系统示例文件目录结构及说明
- 01.Basics
- AnalogReadSerial
- BareMinimum
- Blink
- DigitalReadSerial
- Fade
- ReadAnalogVoltage
- 02.Digital
- BlinkWithoutDelay
- Button
- Debounce
- DigitalInputPullup
- StateChangeDetection
- toneKeyboard
- toneMelody
- toneMultiple
- tonePitchFollower
- 03.Analog
- AnalogInOutSerial
- AnalogInput
- AnalogWriteMega
- Calibration
- Fading
- Smoothing
- 04.Communication
- ASCIITable
- Dimmer
- Graph
- Midi
- MultiSerial
- PhysicalPixel
- ReadASCIIString
- SerialCallResponse
- SerialCallResponseASCII
- SerialEvent
- SerialPassthrough
- VirtualColorMixer
- 05.Control
- Arrays
- ForLoopIteration
- IfStatementConditional
- switchCase
- switchCase2
- WhileStatementConditional
- 06.Sensors
- ADXL3xx
- Knock
- Memsic2125
- Ping
- 07.Display
- barGraph
- RowColumnScanning
- 08.Strings
- CharacterAnalysis
- StringAdditionOperator
- StringAppendOperator
- StringCaseChanges
- StringCharacters
- StringComparisonOperators
- StringConstructors
- StringIndexOf
- StringLength
- StringLengthTrim
- StringReplace
- StringStartsWithEndsWith
- StringSubstring
- StringToInt
- 09.USB
- Keyboard
- KeyboardLogout
- KeyboardMessage
- KeyboardReprogram
- KeyboardSerial
- KeyboardAndMouseControl
- Mouse
- ButtonMouseControl
- JoystickMouseControl
- 10.StarterKit_BasicKit (与特定硬件相关,暂无)
- p02_SpaceshipInterface
- p03_LoveOMeter
- p04_ColorMixingLamp
- p05_ServoMoodIndicator
- p06_LightTheremin
- p07_Keyboard
- p08_DigitalHourglass
- p09_MotorizedPinwheel
- p10_Zoetrope
- p11_CrystalBall
- p12_KnockLock
- p13_TouchSensorLamp
- p14_TweakTheArduinoLogo
- p15_HackingButtons
- 11.ArduinoISP(暂无)
- ArduinoISP