Ariyaeyeee 发表于 2015-11-1 23:28:11

processing与arduino之间相互感应的问题

arduino中设定超声波传感器检测一段距离,在processing中编写如果这段距离别打破(缩小到一定的距离),会出现播放视频的效果,再去打破这段距离就会切换这段视频。之后就是再去触碰这段距离,两个视频相互切换。在if语句中出现了一些问题。求解!!!谢谢!!!
arduino代码:
const int TrigPin = 8;
const int EchoPin = 9;
float cm;
void setup()
{
Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop()
{
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);

cm = pulseIn(EchoPin, HIGH) / 58.0;
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(1000);
}

processing代码:
mport processing.serial.*;
import processing.video.*;

Serial port;
int val;

Movie movie1;
Movie movie2;

void setup(){
   size(1280,720);
   background(0);
   
port = new Serial(this, "COM3", 9600);
   
   movie1 = new Movie(this,"12345.mov");
    movie2 = new Movie(this,"54321.mov");
   movie1.loop();
    movie2.loop();
}

void movieEvent(Movie m) {
m.read();
}

void draw(){
if(port.available() >0 ) {
    val = port.read();
}
if(val ==0 ) {
image(movie2,0,0,width,height);
}
else {
image(movie1,0,0,width,height);
}
}

164335413 发表于 2015-11-2 08:49:04

Serial.print(cm);
Serial.print("cm");
Serial.println();
尽量不要向processing发送这么多无用的字符,如果可以的话,当条件满足时仅仅向processing发送0或者1就可以。超声波数据由arduino判断,而不是processing。

林定祥 发表于 2015-11-2 10:34:33

根据要求,进入检测范围就对Processing的视频1和视频2交换一次,因此,程序修改为
//Arduino
intsw=0;          //开关状态
int valume=30;       //可设置距离临界点
const int TrigPin = 8;
const int EchoPin = 9;
float cm;
void setup()
{
Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop()
{
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);

cm = pulseIn(EchoPin, HIGH) / 58.0;
Serial.print(cm);
Serial.print("cm");
Serial.println();
if (cm<valume){         //进入范围检测
    if (sw=0){sw=1;          //状态变化转换
   }
    else sw=0;
    Serial.write(sw);         //向Processing发状态
}
delay(1000);
}
1、建议调试是可以用Serial.print,但是在和Processing通讯时把他去掉,避免干扰。
2、和Processing通讯时Arduino这边用Serial.Write()语句。

林定祥 发表于 2015-11-2 11:08:30

关于传感器和Processing的交互,通常传感器通过Aruino和Processing通讯,传递传感器的参数或状态,在Processing\file\example\libaries\serial\SimpleRead案例,解释了Arduino和Processing该怎么做:
/**
* Simple Read
*
* Read data from the serial port and change the color of a rectangle
* when a switch connected to a Wiring or Arduino board is pressed and released.
* This example works with the Wiring / Arduino program that follows below.
*/


import processing.serial.*;

Serial myPort;// Create object from Serial class
int val;      // Data received from the serial port

void setup()
{
size(200, 200);
// I know that the first port in the serial list on my mac
// is always myFTDI adaptor, so I open Serial.list().
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list();
myPort = new Serial(this, portName, 9600);
}

void draw()
{
if ( myPort.available() > 0) {// If data is available,
    val = myPort.read();         // read it and store it in val
}
background(255);             // Set background to white
if (val == 0) {            // If the serial value is 0,
    fill(0);                   // set fill to black
}
else {                     // If the serial value is not 0,
    fill(204);               // set fill to light gray
}
rect(50, 50, 100, 100);
}



/*

// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.

int switchPin = 4;                     // Switch connected to pin 4

void setup() {
pinMode(switchPin, INPUT);             // Set pin 0 as an input
Serial.begin(9600);                  // Start serial communication at 9600 bps
}

void loop() {
if (digitalRead(switchPin) == HIGH) {// If switch is ON,
    Serial.write(1);               // send 1 to Processing
} else {                               // If the switch is not ON,
    Serial.write(0);               // send 0 to Processing
}
delay(100);                            // Wait 100 milliseconds
}

*/
供参考

Ariyaeyeee 发表于 2015-11-2 13:03:32

感谢!!!

gthrtg 发表于 2015-11-2 15:27:15

666666666666

mts950326 发表于 2016-4-7 18:56:54

lz能把完整的代码发给我吗
页: [1]
查看完整版本: processing与arduino之间相互感应的问题