这是我的第一篇文章,因此,如果有任何不清楚之处,我会提前道歉,并且我将尝试快速学习。在编程方面,我还是一个新手。使用Arduino Uno,超声波传感器HC-SR04,Processing3.5.3
在处理和Arduino草图中,我提供了能够播放图像序列的功能,并且当超声传感器拾取到物体的距离时,处理控制台中将打印“ 1”。
我想知道是否可以使用此“ 1”来创建if语句。如果控制台打印的数字大于0,则将播放图像序列-否则,将只绘制一张图像(gif将暂停)。我已经尝试了几个版本,但是我不想假装自己知道自己在做什么。
任何让我关注的线索都将很棒!或在线教程!
我感觉好像有些简单的东西我只是想念而已...我想没有什么比这更简单了。感谢您的时间 :))
阿根廷代码:
#define trigPin 9
#define echoPin 10
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2); //CHANGE THIS??
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
distance = pulseIn(echoPin, HIGH);
if (distance <= 2000) { //raise this number to raise the distance to wave hand
Serial.print("1");
} else {
Serial.print("0");
}
delay(500);
}
处理方式
import processing.serial.*;
int numFrames = 22; //number of frames in animation
int currentFrame = 0;
PImage[] image = new PImage[22];
Serial myPort;
String instring = "";
void setup() {
myPort = new Serial(this, Serial.list()[1], 9600);
myPort.bufferUntil('\n');
size(1600, 900);
frameRate(30);
background(0);
//Load images below
for(int i = 0; i<image.length; i++) {
image[i] = loadImage("PatMovingFace" + i + ".png");
}
}
void draw() {
//ALL BELOW connects to arduino sensor
while (myPort.available () > 0) {
instring = myPort.readString();
println(instring);
int sensorAct = Integer.parseInt(instring, 10);
}
playGif();
}
// ALL BELOW will make the pic array animate! Image moves!
void playGif() {
currentFrame = (currentFrame+1) % numFrames; //use % to cycle through frames!
int offset = 0;
for (int i = 0; i < image.length; i++) {
//image(image[i] % numFrames), 0, 0);
image(image[(currentFrame + offset) % numFrames], 0, 0);
offset += 0;
image(image[(currentFrame + offset) % numFrames], 0, 0);
offset+= 0;
}
}
您可以先简化/清理playGif()
:
offset
目前似乎什么也没做(它从0“递增”到0)image()
被调用两次,具有相同的坐标,从而在其顶部覆盖相同的内容。一旦image()
打电话应该做currentFrame
是递增到下一帧并在的开头循环返回到起点playGif()
,但是,在渲染图像时,数组索引将offset
再次递增(0)。目前尚不清楚其意图是什么。你可以没有currentFrame
可以控制播放/暂停:如果递增,则播放,否则暂停。它的好,有更新数据之间(分离currentFrame
),并用更新的数据渲染(image(image[currentFrame],0,0)
)image
数组重命名为,images
以便更清楚地表示其含义,并且更难以混淆该数组和image()
函数例如,playGif()
可能变成:
void playGif(){
currentFrame = (currentFrame+1) % numFrames; //use % to cycle through frames!
image(image[currentFrame], 0, 0);
}
关于使用Arduino的控制,如上所述,只需检查您是否获得“ 1”来更新currentFrame
(否则它将保持(暂停)在相同的值):
void playGif(){
if(instring.equals("1")){
currentFrame = (currentFrame+1) % numFrames; //use % to cycle through frames!
}
image(image[currentFrame], 0, 0);
}
我正在equals()
上面使用,因为它是一个String
,即使您要发送一个字符。(或者比较第一个字符会起作用if(instring.charAt(0) == '1')
)
我也有一些注意事项:
print()
,不println()
,这意味着没有\n
将被发送,因此,没有必要myPort.bufferUntil('\n');
,也不instring = myPort.readString();
myPort.read();
(可以将其与==
(而不是String equals()
)进行比较(例如if(myPort.read() == '1'){...
)while
正在阻止,我建议不要使用它。因为您要发送一个字节,所以这不会对您造成很大的影响,但是在发送更多字节的更复杂的程序上,这将阻止Processing渲染单个帧,直到接收到所有数据为止这是一个例子:
import processing.serial.*;
int numFrames = 22; //number of frames in animation
int currentFrame = 0;
PImage[] images = new PImage[numFrames];
Serial myPort;
void setup() {
myPort = new Serial(this, Serial.list()[1], 9600);
size(1600, 900);
frameRate(30);
background(0);
//Load images below
for (int i = 0; i < numFrames; i++)
{
images[i] = loadImage("PatMovingFace" + i + ".png");
}
}
void draw() {
// if there's at least one byte to read and it's '1'
if(myPort.available() > 0 && myPort.read() == '1'){
// increment frame, looping to the start
currentFrame = (currentFrame+1) % numFrames; //use % to cycle through frames!
}
// render current frame
image(images[currentFrame], 0, 0);
}
一个更谨慎的版本,检查可能出问题的地方(串行连接,数据加载),如下所示:
import processing.serial.*;
int numFrames = 22; //number of frames in animation
int currentFrame = 0;
PImage[] images = new PImage[numFrames];
Serial myPort;
void setup() {
String[] ports = Serial.list();
int portIndex = 1;
if(ports.length <= portIndex){
println("serial ports index " + portIndex + " not found! check cable connection to Arduino");
println("total ports: " + ports.length);
printArray(ports);
exit();
}
try{
myPort = new Serial(this, ports[portIndex], 9600);
}catch(Exception e){
println("error connecting to serial port: double check the cable is connected and no other program (e.g. Serial Monitor) uses this port");
e.printStackTrace();
}
size(1600, 900);
frameRate(30);
background(0);
//Load images below
try{
for (int i = 0; i < numFrames; i++)
{
images[i] = loadImage("PatMovingFace" + i + ".png");
}
}catch(Exception e){
println("image loading error");
e.printStackTrace();
}
}
void draw() {
// if Arduino connection was successfull
if(myPort != null){
// if there's at least one byte to read and it's '1'
if(myPort.available() > 0 && myPort.read() == '1'){
// increment frame, looping to the start
currentFrame = (currentFrame+1) % numFrames; //use % to cycle through frames!
}
}else{
text("serial port not initialised", 10, 15);
}
if(images[currentFrame] != null){
// render current frame
image(images[currentFrame], 0, 0);
}else{
text("serial port not loaded", 10, 15);
}
}
或使用功能分组时具有相同的功能:
import processing.serial.*;
int numFrames = 22; //number of frames in animation
int currentFrame = 0;
PImage[] images = new PImage[numFrames];
final int SERIAL_PORT_INDEX = 1;
final int SERIAL_BAUD_RATE = 9600;
Serial myPort;
void setup() {
size(1600, 900);
frameRate(30);
background(0);
setupArduino();
//Load images below
loadImages();
}
void setupArduino(){
String[] ports = Serial.list();
int numSerialPorts = ports.length;
// optional debug prints: useful to double check serial connection
println("total ports: " + numSerialPorts);
printArray(ports);
// exit if requested port is not found
if(numSerialPorts <= SERIAL_PORT_INDEX){
println("serial ports index " + SERIAL_PORT_INDEX + " not found! check cable connection to Arduino");
//exit();
}
// try to open port, exit otherwise
try{
myPort = new Serial(this, ports[SERIAL_PORT_INDEX], SERIAL_BAUD_RATE);
}catch(Exception e){
println("error connecting to serial port: double check the cable is connected and no other program (e.g. Serial Monitor) uses this port");
e.printStackTrace();
//exit();
}
}
void loadImages(){
try{
for (int i = 0; i < numFrames; i++)
{
images[i] = loadImage("PatMovingFace" + i + ".png");
}
}catch(Exception e){
println("image loading error");
e.printStackTrace();
//exit();
}
}
void serialUpdateImage(){
// if Arduino connection was successfull
if(myPort != null){
// if there's at least one byte to read and it's '1'
if(myPort.available() > 0 && myPort.read() == '1'){
// increment frame, looping to the start
currentFrame = (currentFrame+1) % numFrames; //use % to cycle through frames!
}
}else{
text("serial port not initialised", 10, 15);
}
}
void displayCurrentImage(){
if(images[currentFrame] != null){
// render current frame
image(images[currentFrame], 0, 0);
}else{
text("image " + currentFrame + " not loaded", 10, 25);
}
}
void draw() {
serialUpdateImage();
displayCurrentImage();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句