让's say you'从头开始 . 要从Arduino发送数据,您需要打开Serial连接 . 在这个阶段,唯一重要的细节是波特率:数据流动的速度 .
这是一个最小的Arduino数据示例:
void setup() {
//initialize the Serial library with baud rate 115200
Serial.begin(115200);
}
void loop() {
//write data to the serial port
Serial.println("test");
delay(1000);
}
如果您在Arduino软件中打开串行监视器并将波特率设置为115200,您应该每秒打印一次 test .
import processing.serial.*;
void setup() {
//update the serial port index based on your setup
println(Serial.list());
Serial arduino = new Serial(this, Serial.list()[0], 115200);
arduino.bufferUntil('\n');
}
void draw() {
}
void serialEvent(Serial p) {
//read the string from serial
String rawString = p.readString();
println(rawString);
}
String xyValues = "12,13";
String[] xy = xyValues.split(",");
printArray(xy);
int x = int(xy[0]);
int y = int(xy[1]);
println("integer values: " + x + " , " + y);
所以,理论上,你应该可以在Arduino上做这样的事情:
int x,y;
void setup() {
//initialize serial
Serial.begin(115200);
}
void loop() {
//simulating the x,y values from the touch screen,
//be sure to replace with the actual readings from
//NOTE! If the screen returns values above 255, scale them to be from 0 to 255
x = map(analogRead(A0),0,1024,0,255);
y = map(analogRead(A1),0,1024,0,255);
//write the data to serial
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print("\n");
}
然后在Arduino方面:
import processing.serial.*;
float x,y;
void setup() {
size(400,400);
//update the serial port index based on your setup
Serial arduino = new Serial(this, Serial.list()[0], 115200);
arduino.bufferUntil('\n');
}
void draw() {
background(0);
ellipse(x,y,10,10);
}
void serialEvent(Serial p) {
//read the string from serial
String rawString = p.readString();
//trim any unwanted empty spaces
rawString = rawString.trim();
try{
//split the string into an array of 2 value (e.g. "0,127" will become ["0","127"]
String[] values = rawString.split(",");
//convert strings to int
int serialX = int(values[0]);
int serialY = int(values[1]);
//map serial values to sketch coordinates if needed
x = map(serialX,0,255,0,width);
y = map(serialY,0,255,0,height);
}catch(Exception e){
println("Error parsing string from Serial:");
e.printStackTrace();
}
}
import processing.serial.*;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
float x = 0;
float y = 0;
boolean first = true;
setup() {
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
void draw() {
if ( myPort.available() > 0) { // If data is available,
val = myPort.readStringUntil('\n'); // read it and store it in val
if (first) {
x = parseFloat(val);
first = false;
println("x= "+val); //print it out in the console
}
else {
y = parseFloat(val);
first = true;
println("y= "+val); //print it out in the console
}
}
2 回答
在Processing和 SerialRead 示例中使用Serial library进行游戏会很有帮助(在Processing> File> Examples> Libraries> serial> SimpleRead中)
让's say you'从头开始 . 要从Arduino发送数据,您需要打开Serial连接 . 在这个阶段,唯一重要的细节是波特率:数据流动的速度 .
这是一个最小的Arduino数据示例:
如果您在Arduino软件中打开串行监视器并将波特率设置为115200,您应该每秒打印一次 test .
要在Processing中读取相同的数据,除了指定波特率之外,还必须指定串行端口(在工具>端口中选择的内容,并且还列在当前Arduino草图的右下角):
请注意,我们告诉Processing缓冲直到达到
'\n'
字符,因此我们不必担心等待每个字符并将其手动附加到String . 而是使用bufferUntil(),serialEvent()和readString()大部分工作都是为我们完成的 .现在您可以从Arduino发送一个String并在Processing中读取它,您可以执行一些String操作,例如使用split()函数使用分隔符将字符串拆分为多个:
最后一部分是将分割值从
String
转换为int
:所以,理论上,你应该可以在Arduino上做这样的事情:
然后在Arduino方面:
Note 上面的Arduino代码可能无法解决您的问题,您需要集成您的触摸传感器代码,但希望它提供了一些关于如何解决这个问题的提示 .
发送一个字符串,然后解析它是一种方法,但不是最有效的方法 . 如果你的x,y值在0-255范围内,你可以只发送2个字节(每个坐标作为单个
char
)而不是最多8个字节,但是现在使用字符串可能更容易,而不是直接跳到字节 .这tutorial会帮助你 . 5分钟,你可以互相连接!
编辑:
首先看一下教程的第一部分(“从Arduino ......”“......到处理”) . 在arduino中,您只需在Serial中发送坐标即可 .
在处理过程中,您将此坐标作为文本接收,但您可以使用
parseFloat()
函数将其转换为Float .这是Processing中接收坐标并将其存储在变量中所需的代码 .
我希望这能帮助您解决问题 .