[英]Image displaying using Arduino- Processing connection and ADXL345 sensor
I am working on an artistic project that includes an ADXL345 sensor (accelerometer), Arduino Uno R3 Board, Arduino IDE 2.0.3 and Processing 4.1.2.我正在从事一个艺术项目,其中包括一个 ADXL345 传感器(加速度计)、Arduino Uno R3 板、Arduino IDE 2.0.3 和 Processing 4.1.2。 I want Processing to display images randomly and continuously every time the values of the sensor that are received from the serial communication with the Arduino sketch, go x>5, x<-5, y.5, y.-5, z>1, z<-1.
我希望每次从与 Arduino sketch 的串行通信接收到的传感器值时,Processing 都随机且连续地显示图像,go x>5, x<-5, y.5, y.-5, z>1 , z<-1。
UPDATE: A friend helped me with some lines of code and now the image being displayed when I move the sensor.更新:一位朋友帮助我编写了一些代码行,现在当我移动传感器时显示图像。
CHALLENGE: What I want to be able to do now is run the processing sketch ONCE and let the windows containing the images pop up, close down, open new windows and display new random images from my folder.挑战:我现在想做的是一次运行处理草图,让包含图像的 windows 弹出、关闭、打开新的 windows 并显示我文件夹中的新随机图像。 For this process to repeat on itself so I don't have to run the sketch manually every time.
为了让这个过程自行重复,所以我不必每次都手动运行草图。
These are the codes that I am using in Arudino and Processing.这些是我在 Arudino 和 Processing 中使用的代码。
ARDUINO 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);
}
& PROCESSING & 加工
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);
}
}
}
}
Thank you!!谢谢!!
So many things are right with your code (and question)(+1):您的代码(和问题)有很多正确的地方(+1):
null
null
You are so close !你是如此接近!
There is one issue throwing off parsing:有一个问题会导致解析失败:
String data = mySerial.readStringUntil('\n');
String data = mySerial.readStringUntil('\n');
Serial.println(x);
instead of Serial.print(x)
)Serial.println(x);
而不是Serial.print(x)
) I suspect the intention was to use println()
only on the last line:我怀疑其目的是仅在最后一行使用
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);
}
(The above should print 3 values separated by "," on a single line. Double check if z should be a digitalRead(2)
(returning 1 or 0) reading on pin 2 on your Arduino or analogRead(A2)
(returning 0 to 1023) reading analog pin A2.) (上面应该在一行上打印 3 个由“,”分隔的值。仔细检查 z 是否应该是
digitalRead(2)
(返回 1 或 0)在 Arduino 或analogRead(A2)
上的引脚 2 上读取(返回 0 到1023) 读取模拟引脚 A2。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.