Re: [问题] Arduino输出值到Processing如何辨别A0及A1,2,3

楼主: robrob99 (梁龙)   2020-11-18 19:31:34
===============================
Arduino
===============================
void setup()
{
Serial.begin( 9600 );
}
void loop()
{
/*
int value = analogRead( A0 );
Serial.write(value/4);
*/
long value1 = analogRead( A0 );
long value2 = analogRead( A1 );
long valueout = (value1*1000)+(value2);
//我这边假设两值最大小于1000 所以第一个值*1000 加上第二个值 值比较大用long
Serial.write(valueout);
delay(0);
}
===============================
Processing
===============================
/* Processing 范例:
* 读取从 Serial Port 传进来的 Sensor 读值
* 利用 Sensor 读值移动矩形 (Rectangle)
*/
import processing.serial.*;
Serial serial;
//int sensorValue;
long sensorValue;
long printValue1;
long printValue2;
void setup() {
// 设定画布大小为 305 x 200
size(610, 400);
// 开启 Serial port,通讯速率为 9600 bps
// 注意! 如果你 Arduino 不是接在 COM4,请做适当调整
serial = new Serial(this, "COM6", 9600);
}
void draw() {
if ( serial.available() > 0) {
// 读取从 Serial Port 传进来的 Sensor 读值
sensorValue = serial.read();
println(sensorValue);
printValue1 = (sensorValue/1000)/4;
printValue2 = (sensorValue%1000)/4;;
// 在 (x, y) 为 (sensorValue, 80) 的位置画一个 50x50 的矩形
background(255); // 白色背景
fill(255,0,0); // 填满颜色为红色
//rect(sensorValue, 80, 50, 50);
rect(printValue1, 80, 50, 50);
rect(printValue1, 140, 50, 50);
}
上色是我添加的片段 没有编译器也没有装置 请自行debug
作者: SHANDer (八面体)   2020-11-19 16:48:00
感谢你,我有用这个概念改了一下成功做出两个,但我其实是要十个,这个方法我就Fail了QQ

Links booklink

Contact Us: admin [ a t ] ucptt.com