[英]Image displaying using Arduino- Processing connection and ADXL345 sensor
我正在從事一個藝術項目,其中包括一個 ADXL345 傳感器(加速度計)、Arduino Uno R3 板、Arduino IDE 2.0.3 和 Processing 4.1.2。 我希望每次從與 Arduino sketch 的串行通信接收到的傳感器值時,Processing 都隨機且連續地顯示圖像,go x>5, x<-5, y.5, y.-5, z>1 , z<-1。
更新:一位朋友幫助我編寫了一些代碼行,現在當我移動傳感器時顯示圖像。
挑戰:我現在想做的是一次運行處理草圖,讓包含圖像的 windows 彈出、關閉、打開新的 windows 並顯示我文件夾中的新隨機圖像。 為了讓這個過程自行重復,所以我不必每次都手動運行草圖。
這些是我在 Arudino 和 Processing 中使用的代碼。
ARDUINO
void setup() {
// initialize serial communication at 9600 baud rate
Serial.begin(9600);
}
void loop() {
// send x, y, and z values over serial
int x = analogRead(A0);
int y = analogRead(A1);
int z = digitalRead(2);
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(",");
Serial.println(z);
delay(1000);
}
& 加工
import processing.serial.*;
Serial mySerial;
PImage fragment;
int rand;
void setup() {
size(1000, 500);
rand = int(random(0,133));
takerandomimage("C:/Users/user/Documents/Processing/Trial_300123/frag_" + nf(rand, 3) + ".jpg");
String portName = Serial.list()[0];
mySerial = new Serial(this, portName, 9600);
println("Serial port connected: " + portName);
loop();
}
void takerandomimage(String fn) {
fragment = loadImage(fn);
println(fragment);
}
void draw() {
background(255); //clears the screen
if (fragment.width>0 && fragment.height > 0){ //check if image has been loaded
String data = mySerial.readStringUntil('\n');
if (data != null && data != "\n" && data != " " && data != "\r" && data != "\t") {
println("Data received: " + data);
String[] values = data.split(" ",0);
int counter = 0;
int x = 0;
int y = 0;
int z = 0;
for(String w :values){
System.out.println(w);
if (counter == 1)
x = int(w);
if ( counter == 4)
y = int(w);
if ( counter == 7)
z = int(w);
counter++;
}
println(x);
println(y);
println(z);
if (x < 0 || y > 0 || z > 0) {
takerandomimage("C:/Users/user/Documents/Processing/Trial_300123/frag_" + nf(rand, 3) + ".jpg");
image(fragment, 0,0);
delay(1000);
}
}
}
}
謝謝!!
您的代碼(和問題)有很多正確的地方(+1):
null
你是如此接近!
有一個問題會導致解析失敗:
String data = mySerial.readStringUntil('\n');
Serial.println(x);
而不是Serial.print(x)
) 我懷疑其目的是僅在最后一行使用println()
:
void setup() {
// initialize serial communication at 9600 baud rate
Serial.begin(9600);
}
void loop() {
// send x, y, and z values over serial
int x = analogRead(A0);
int y = analogRead(A1);
int z = digitalRead(2);
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(",");
Serial.println(z);
delay(1000);
}
(上面應該在一行上打印 3 個由“,”分隔的值。仔細檢查 z 是否應該是digitalRead(2)
(返回 1 或 0)在 Arduino 或analogRead(A2)
上的引腳 2 上讀取(返回 0 到1023) 讀取模擬引腳 A2。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.