[英]Reading Arduino int array with Processing
我有arduino處理通訊問題。 我有arduino代碼,可以通過觸摸屏為我提供X和/ Y坐標,並且運行良好,沒問題,我得到了X和Y坐標。 但是我需要想像一下,我正在嘗試在Processing 3.0中編寫代碼,以便能夠在我的計算機上看到touchFolie被觸摸的位置。 因此,我需要將XY從arduino發送到處理過程,以便能夠進行繪制。 有誰知道如何從arduino發送整數數組[X,Y]進行處理?
在Processing和SerialRead示例中使用Serial庫將很有幫助 (在Processing> File> Examples> Libraries> serial> SimpleRead中 )
假設您是從頭開始。 要從Arduino發送數據,您需要打開一個串行連接。 在此階段,唯一重要的細節是波特率:數據流的速度。
這是一個最小的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,則應該每秒看到一次測試打印。
要在處理中讀取相同的數據,除了指定波特率之外,還必須指定串行端口(在“工具”>“端口”中選擇的端口,並在當前Arduino草圖的右下方列出):
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);
}
請注意,我們告訴Processing進行緩沖,直到到達'\\n'
字符為止,因此我們不必擔心等待每個字符並將其手動附加到String。 取而代之的是使用bufferUntil() , serialEvent()和readString()為我們完成了大部分工作。
現在您可以從Arduino發送一個String並在Processing中讀取它,您可以執行一些String操作,例如通過split()函數使用分隔符將一個字符串拆分成多個字符串:
String xyValues = "12,13";
printArray(xyValues.split(","));
最后一部分是將分割值從String
轉換為int
:
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();
}
}
注意上面的Arduino代碼可能無法照原樣解決您的問題,您需要集成觸摸傳感器代碼,但希望它能提供一些有關如何解決此問題的提示。
發送字符串然后解析它是一種方法,但不是最有效的方法。 如果您的x,y值在0-255范圍內,則可以僅發送2個字節(每個坐標作為一個char
),而不是最多發送8個字節,但是就目前而言,使用String而不是String可以輕松得多立即跳入字節。
本教程將為您提供幫助。 5分鍾,您就可以彼此連接!
首先,看一下教程的第一部分(“從Arduino ...”“ ...到處理”)。 在arduino中,您只需在序列號中發送坐標即可。
Serial.println(coordX);
Serial.println(coordY);
在處理中,您將以文本形式接收此坐標,但是可以使用parseFloat()
函數在Float中對其進行轉換。
這是處理中需要的代碼,以接收您的坐標並將其存儲在變量中。
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
}
}
希望這可以幫助您解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.